Introduction
prompt_toolkit is the foundation behind many popular Python CLI tools including IPython, pgcli, mycli, and AWS CLI v2. It provides everything needed to build interactive terminal applications: multi-line editing, syntax highlighting, completion menus, mouse support, and full-screen layouts.
What prompt_toolkit Does
- Implements a multi-line text editor in the terminal with Emacs and Vi key bindings
- Provides auto-completion with dropdown menus and fuzzy matching
- Supports syntax highlighting via Pygments integration or custom lexers
- Enables full-screen terminal UIs with split layouts, floating windows, and dialogs
- Handles input validation, history, and clipboard operations
Architecture Overview
prompt_toolkit uses an event loop that processes key presses through a configurable key-binding pipeline. The rendering layer maintains a virtual screen buffer and diffs it against the previous frame to minimize terminal output. Layouts are built from composable containers (HSplit, VSplit, Float) that hold Buffer and Window controls, similar to how GUI frameworks compose widgets.
Self-Hosting & Configuration
- Install via pip:
pip install prompt_toolkit - No external dependencies beyond Python 3.7+
- Integrate with asyncio using
prompt_toolkit.shortcuts.PromptSessionwithprompt_async() - Style output using
Style.from_dict()or built-in named styles - Combine with Click or argparse for hybrid CLI + interactive applications
Key Features
- Full Vi and Emacs editing modes with configurable key bindings
- Mouse support for click-to-position, selection, and scrolling in terminals that support it
- Unicode-aware rendering with correct handling of CJK wide characters
- Built-in dialog components: message boxes, input dialogs, radio lists, checkboxes
- Progress bars with ETA, percentage, and custom formatters
Comparison with Similar Tools
- readline — C library with Python bindings; single-line only, no highlighting, no completion menus
- curses/ncurses — low-level terminal control; prompt_toolkit provides higher-level abstractions
- Rich — focused on output formatting; prompt_toolkit focuses on interactive input
- Textual — TUI framework by the Rich author; heavier, targets full applications rather than prompts
- inquirer.py — quick interactive prompts but limited customization compared to prompt_toolkit
FAQ
Q: Is prompt_toolkit what IPython uses internally? A: Yes. Since IPython 5.0, the interactive shell is built on prompt_toolkit for editing, completion, and syntax highlighting.
Q: Can I build a full TUI application with prompt_toolkit? A: Yes. Its full-screen mode supports layouts, menus, toolbars, and mouse input, making it suitable for terminal applications like text editors and database clients.
Q: Does it work on Windows? A: Yes. prompt_toolkit includes a Windows-compatible input and output layer that works in cmd.exe, PowerShell, and Windows Terminal.
Q: How does it handle async applications?
A: prompt_toolkit integrates with asyncio natively. Use PromptSession.prompt_async() to run prompts without blocking the event loop.