Build a Powerful Python REPL in Under 20 Lines with Prompt Toolkit, Click, and More

This tutorial shows how to create a feature‑rich command‑line REPL using Python libraries such as Prompt Toolkit, Click, fuzzyfinder and Pygments, adding history, auto‑suggest, auto‑completion, paging, editor integration and syntax highlighting—all with fewer than twenty lines of code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Powerful Python REPL in Under 20 Lines with Prompt Toolkit, Click, and More

Python Prompt Toolkit

Install the library:

pip install prompt_toolkit

Basic Interactive Interpreter

A minimal REPL that echoes user input:

from prompt_toolkit import prompt
while 1:
    user_input = prompt('> ')
    print(user_input)

History Support

Persist command history using FileHistory:

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
history = FileHistory('history.txt')
while 1:
    user_input = prompt('>', history=history)
    print(user_input)

Auto‑Suggest from History

Add fish‑style suggestions based on previous commands:

from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
history = FileHistory('history.txt')
while 1:
    user_input = prompt('>', history=history, auto_suggest=AutoSuggestFromHistory())
    print(user_input)

Auto‑Completion

Use WordCompleter to provide tab‑completion for SQL keywords:

from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
SQLCompleter = WordCompleter(['select','from','insert','update','delete','drop'], ignore_case=True)
while 1:
    user_input = prompt('SQL>', completer=SQLCompleter)
    print(user_input)

Click – Command‑Line Interface Toolkit

Install Click: pip install click Use Click’s pager to display long output:

import click
click.echo_via_pager(user_input)

Launch an editor and capture its content:

import click
message = click.edit()

Fuzzy Search

Install fuzzyfinder and use it to suggest completions based on partial input:

pip install fuzzyfinder
from fuzzyfinder import fuzzyfinder
suggestions = fuzzyfinder('abc', ['abcd','defabca','aagbec','xyz','qux'])
list(suggestions)

Pygments – Syntax Highlighting

Install Pygments and apply the SQL lexer to colourise user input:

pip install pygments
from pygments.lexers import SqlLexer
# Pass SqlLexer to Prompt Toolkit’s lexer argument (omitted for brevity)

Conclusion

By combining Prompt Toolkit, Click, fuzzyfinder and Pygments you can build a full‑featured REPL that offers history navigation, key bindings, intelligent auto‑completion, fuzzy search, paging, editor integration and colourful syntax highlighting—all with fewer than twenty lines of Python code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIPythonclickreplfuzzyfinderprompt_toolkitpygments
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.