Build a Powerful Python REPL in Under 20 Lines with Prompt Toolkit & Click

This tutorial shows how to create a feature‑rich interactive Python REPL using Prompt Toolkit, Click, fuzzyfinder and Pygments, adding echo functionality, persistent history, auto‑suggest, tab 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

Command‑line programs can be enhanced with fun features such as prompt toolkits, auto‑suggest, fuzzy search and syntax highlighting. This guide demonstrates how to build a compact REPL using several Python libraries.

Prompt Toolkit

Install the library: pip install prompt_toolkit Start with a simple echo REPL:

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

The REPL reads user input and prints it back.

Persistent History

Add command history support:

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

Now the up/down arrows navigate previous commands and Ctrl+R searches history.

Auto‑Suggest

Enable history‑based suggestions similar to the Fish shell:

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

Auto‑Completion

Provide tab‑completion using a dictionary of possible words. For a SQL‑style REPL, define a list of keywords and use WordCompleter:

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

The completer suggests matching keywords as the user types.

Click Integration

Use Click to handle paging and editor launching: pip install click Replace print with click.echo_via_pager for long output, and invoke the system editor with click.edit() when needed.

Fuzzy Finder

Install fuzzyfinder and use it to implement fuzzy matching for completions:

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

Integrate the fuzzy matcher into a custom Completer class that yields Completion objects.

Pygments Syntax Highlighting

Install Pygments and apply the SqlLexer to color SQL input:

pip install pygments
from pygments.lexers import SqlLexer
lexer = SqlLexer()
# Pass lexer to Prompt Toolkit's prompt() API (example omitted for brevity)

The REPL now displays colored SQL statements, helping users spot errors.

Conclusion

By combining Prompt Toolkit, Click, fuzzyfinder and Pygments, you can create a full‑featured interactive interpreter with history, key bindings, auto‑completion, fuzzy search, paging, editor support and syntax highlighting—all in 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.

Pythonclickcommand-linereplfuzzyfinderprompt_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.