gitmyhub

python-prompt-toolkit

Python ★ 11k updated 1mo ago

Library for building powerful interactive command line applications in Python

A Python library for building interactive terminal programs with autocomplete, syntax highlighting, history search, and mouse support, like building a mini shell in Python.

PythonPygmentssetup: easycomplexity 2/5

python-prompt-toolkit is a Python library for building interactive command-line programs, meaning programs that run in a terminal and let users type input in a rich, responsive way. If you have ever used a terminal shell that shows syntax coloring as you type, offers autocomplete suggestions, or lets you scroll through history, those features are the kind of thing this library makes possible in your own Python program.

The library is described as a potential replacement for GNU readline, which is the longstanding Unix tool that adds line-editing capabilities (like pressing the up arrow to recall the previous command) to terminal programs. prompt_toolkit can do everything readline does, and more. Features listed in the README include: syntax highlighting as the user types, multi-line input editing, code completion, both Emacs and Vi keyboard shortcuts, search through input history, mouse support, auto-suggestions similar to the fish shell, and support for Unicode characters including double-width Chinese text.

Installation is a single pip command. The library works on Linux, macOS, FreeBSD, OpenBSD, and Windows. The README notes that Windows support is best on recent Windows 10 where the terminal understands standard escape codes, with a fallback for older Windows versions.

The simplest usage is just a few lines: import the prompt function, call it with a message string, and it returns whatever the user typed. More complex uses involve customizing the completion logic, adding syntax highlighting, or building a full interactive shell. The repository ships an examples directory with small programs that each demonstrate one feature. ptpython, an interactive Python shell, is listed as a real-world application built on top of this library.

The library has only two external dependencies (Pygments for syntax highlighting and wcwidth for character width calculations), keeping it lightweight relative to what it provides.

Where it fits