Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 19, 2026·3 min de lecture

prompt_toolkit — Build Powerful Interactive Command Lines in Python

prompt_toolkit is a Python library for building interactive command-line applications with multi-line editing, syntax highlighting, auto-completion, and dialog-based UIs.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 96/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Prompt
Installation
Single
Confiance
Confiance : Established
Point d'entrée
prompt_toolkit CLI Library
Commande CLI universelle
npx tokrepo install a3405ecc-5339-11f1-9bc6-00163e2b0d79

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.PromptSession with prompt_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.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires