Prompts2026年5月19日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 96/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Prompt
安装
Single
信任
信任等级:Established
入口
prompt_toolkit CLI Library
直接安装命令
npx -y tokrepo@latest install a3405ecc-5339-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产