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.
Python Prompt Toolkit
Install the library:
pip install prompt_toolkitBasic 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
