Python Prompt Toolkit¶
prompt_toolkit is a library for building powerful interactive command lines and terminal applications in Python.
It can be a pure Python replacement for GNU readline, but it can be much more than that.
- Syntax highlighting of the input while typing. (For instance, with a Pygments lexer.)
- Multi-line input editing.
- Advanced code completion.
- Selecting text for copy/paste. (Both Emacs and Vi style.)
- Mouse support for cursor positioning and scrolling.
- Auto suggestions. (Like fish shell.)
- No global state.
- Both Emacs and Vi key bindings.
- Reverse and forward incremental search.
- Works well with Unicode double width characters. (Chinese input.)
- Pure Python. Runs on all Python versions from 2.6 up to 3.4.
- Runs on Linux, OS X, OpenBSD and Windows systems.
- Lightweight, the only dependencies are Pygments, six and wcwidth.
- No assumptions about I/O are made. Every prompt_toolkit application should also run in a telnet/ssh server or an asyncio process.
Two use cases: prompts versus full screen terminal applications¶
prompt_toolkit was in the first place meant to be a replacement for
readline. However, when it became more mature, we realised that all the
components for full screen applications are there and
very capable of handling many use cases. Pyvim and pymux are examples of full screen
Basically, at the core,
prompt_toolkit has a layout engine, that supports
horizontal and vertical splits as well as floats, where each “window” can
display a user control. The API for user controls is simple yet powerful.
prompt_toolkit is used to simply read some input from the user, it
uses a rather simple built-in layout. One that displays the default input
buffer and the prompt, a float for the autocompletions and a toolbar for input
validation which is hidden by default.
For full screen applications, usually we build the layout ourself, because it’s very custom.
Further, there is a very flexible key binding system that can be programmed for all the needs of full screen applications.
pip install prompt_toolkit
For Conda, do:
conda install -c https://conda.anaconda.org/conda-forge prompt_toolkit
The following snippet is the most simple example, it uses the
prompt() function to asks the user for input
and returns the text. Just like
from __future__ import unicode_literals from prompt_toolkit import prompt text = prompt('Give me some input: ') print('You said: %s' % text)
For more information, start reading the building prompts section.
Thanks to all the contributors for making prompt_toolkit possible.
Table of contents¶
- Building prompts
- Hello world
- Syntax highlighting
- Input validation
- Auto suggestion
- Adding a bottom toolbar
- Adding a right prompt
- Vi input mode
- Adding custom key bindings
- Other prompt options
- Prompt in an
- Building full screen applications