Library for building powerful interactive command lines in Python
Project description
prompt_toolkit is a Library for building powerful interactive command lines in Python. It ships with a nice interactive Python shell (called ptpython) built on top of the library.
prompt_toolkit could be a replacement for readline, but it can be much more than that.
Features:
Pure Python.
Syntax highlighting of the input while typing. (For instance, with a Pygments lexer.)
Multi-line input editing.
Advanced code completion.
Both Emacs and Vi key bindings. (Similar to readline.)
Reverse and forward incremental search.
Both Python 3 and Python 2.7 support.
Works well with Unicode double width characters. (Chinese input.)
Selecting text for copy/paste. (Both Emacs and Vi style.)
No global state.
Code written with love.
Runs on Linux, OS X, OpenBSD and Windows systems.
Feel free to create tickets for bugs and feature requests, and create pull requests if you have a nice patches that you would like to share with others.
About Windows support
prompt_toolkit is cross platform, and everything that you build on top should run fine on both Unix and Windows systems. On Windows, it uses a different event loop (WaitForMultipleObjects instead of select), and another input and output system. (Win32 APIs instead of pseudo-terminals and VT100.)
That should work fine, however the library is currently much more tested on Linux and Mac os X systems. So, if you find any bugs in the Windows implementation, or you have an idea how to make the experience better, please create a Github issue.
It’s worth noting that the implementation is a “best effort of what is possible”. Both Unix and Windows terminals have their limitations. But in general, the Unix experience will still be a little better.
Installation
pip install prompt-toolkit
The Python repl
Run ptpython to get an interactive Python prompt with syntax highlighting, code completion, etc…
If you prefer to have Vi key bindings (which currently are more completely implemented than the Emacs bindings), run ptpython --vi.
If you want to embed the REPL inside your application at one point, do:
from prompt_toolkit.contrib.repl import embed
embed(globals(), locals(), vi_mode=False, history_filename=None)
Autocompletion
Tab and shift+tab complete the input. (Thanks to the Jedi autocompletion library.) In Vi-mode, you can also use Ctrl+N and Ctrl+P.
Multiline editing
Usually, multi-line editing mode will automatically turn on when you press enter after a colon, however you can always turn it on by pressing F7.
To execute the input in multi-line mode, you can either press Alt+Enter, or Esc followed by Enter. (If you want the first to work in the OS X terminal, you have to check the “Use option as meta key” checkbox in your terminal settings. For iTerm2, you have to check “Left option acts as +Esc” in the options.)
Other features
Running system commands: Press Meta-! in Emacs mode or just ! in Vi navigation mode to see the “Shell command” prompt. There you can enter system commands without leaving the REPL.
Selecting text: Press Control+Space in Emacs mode on V (major V) in Vi navigation mode.
You love IPython?
Run ptipython (prompt_toolkit - IPython), to get a nice interactive shell with all the power that IPython has to offer, like magic functions and shell integration.
Using as a library
This is a library which allows you to build highly customizable input prompts. Every step (key bindings, layout, etc..) can be customized.
Note that this is work in progress. Many things work, but code is still refactored a lot and APIs can change (they will become even better), so be prepared to handle these changes.
Certainly look in the examples directory to see what is possible.
A very simple example:
from prompt_toolkit import CommandLineInterface, AbortAction
from prompt_toolkit import Exit
def main():
cli = CommandLineInterface()
try:
while True:
code_obj = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
print('You said: ' + code_obj.text)
except Exit: # Quit on Ctrl-D keypress
return
if __name__ == '__main__':
main()
FAQ
- Q
The Ctrl-S forward search doesn’t work and freezes my terminal.
- A
Try to run stty -ixon in your terminal to disable flow control.
- Q
The Meta-key doesn’t work.
- A
For some terminals you have to enable the Alt-key to act as meta key, but you can also type Escape before any key instead.
Special thanks to
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file prompt_toolkit-0.21.tar.gz
.
File metadata
- Download URL: prompt_toolkit-0.21.tar.gz
- Upload date:
- Size: 80.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d71d48260ea75ef35eaa90e197112445cc37aa092cb4f476e6716a4673777c8a |
|
MD5 | 939d26f5cafe606ef2c9313561c4e4e3 |
|
BLAKE2b-256 | 780b481bbde7cad9fda15a1fc143d8ae4f5d2f8dd74e73395fe8eb5004fe02e8 |