Introduction
Python Prompt Toolkit is a library for building sophisticated interactive command-line interfaces and terminal applications. It is the foundation behind many popular CLI tools including IPython, pgcli, mycli, and AWS Shell. The library provides a full terminal UI toolkit with async support, making it possible to build anything from simple input prompts to full-screen terminal applications.
What Python Prompt Toolkit Does
- Provides rich input prompts with syntax highlighting, autocompletion, and multiline editing
- Supports vi and emacs key binding modes out of the box
- Includes a full-screen application framework for building complex terminal UIs
- Handles Unicode, mouse input, clipboard operations, and 24-bit color rendering
- Works with asyncio for non-blocking terminal applications
Architecture Overview
The library is organized around a rendering pipeline: the layout engine composes UI controls into a screen buffer, the renderer outputs the buffer to the terminal using ANSI escape sequences, and the input processor handles keyboard and mouse events through configurable key binding registries. The buffer component manages text editing with undo/redo, and the completion engine provides pluggable autocompletion backends. Everything runs on an event loop compatible with Python's asyncio.
Self-Hosting & Configuration
- Install with
pip install prompt_toolkiton Python 3.7+ - No external dependencies; it is a pure Python package
- Customize key bindings by creating a
KeyBindingsinstance and registering handlers - Configure autocompletion with built-in completers (word, path, filesystem) or write custom ones
- Style prompts and applications using Pygments-compatible style classes
Key Features
- Syntax highlighting in the input line using Pygments lexers or custom highlighters
- Pluggable autocompletion with popup menus, multi-column display, and fuzzy matching
- Vi and Emacs editing modes with full motion and text object support
- Full-screen application mode for building terminal dashboards and TUIs
- Async-native architecture that integrates with asyncio event loops
Comparison with Similar Tools
- readline — the standard C library for line editing; Prompt Toolkit is a more featureful pure-Python replacement
- Click — a CLI argument parser, not an interactive input library; the two are complementary
- Rich — focuses on output formatting and rendering; Prompt Toolkit focuses on input and interaction
- Textual — full TUI framework by the Rich author; heavier but better for complex widget-based UIs
- Inquirer.py — pre-built prompt types (select, confirm, checkbox); simpler but less customizable
FAQ
Q: How does it compare to GNU readline? A: Prompt Toolkit offers more features (syntax highlighting, multiline editing, mouse support, async) and is pure Python, so it works identically across all platforms without native library dependencies.
Q: Can I build full-screen terminal apps? A: Yes. The library includes a layout system with containers, windows, and controls for building complex full-screen interfaces similar to what you see in htop or tig.
Q: Does it work on Windows? A: Yes. It supports Windows Terminal, cmd.exe, and PowerShell with full feature parity.
Q: Which projects use Prompt Toolkit? A: IPython/Jupyter, pgcli, mycli, litecli, AWS Shell, xonsh, and many other interactive CLI tools.