




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































ii
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
Warning: Notice that this is the prompt_toolkit 3.0 documentation. It is mostly compatible with the 2.0 branch. The difference is that prompt_toolkit 3.0 requires at least Python 3.6. On the plus side, it uses asyncio natively (rather than it’s own event loop), and we have type annotations everywhere.
prompt_toolkit is a library for building powerful interactive command line and terminal applications in Python.
It can be a very advanced pure Python replacement for GNU readline, but it can also be used for building full screen applications.
Some features:
Like readline:
Works everywhere:
ONE
GETTING STARTED
Go to getting started and build your first prompt. Issues are tracked on the Github project.
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
4 Chapter 1. Getting started
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
6 Chapter 2. Thanks to:
THREE
TABLE OF CONTENTS
3.1 Gallery
Showcase, demonstrating the possibilities of prompt_toolkit.
The prompt:
The configuration menu of ptpython.
The history page with its help. (This is a full-screen layout.)
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
3.1. Gallery 9
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
3.2 Getting started
pip install prompt_toolkit
For Conda, do:
conda install -c https://conda.anaconda.org/conda-forge prompt_toolkit
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 prompt_toolkit is very capable of handling many use situations. Pyvim and pymux are examples of full screen applications.
10 Chapter 3. Table of contents
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
In order to learn and understand prompt_toolkit , it is best to go through the all sections in the order below. Also don’t forget to have a look at all the examples in the repository.
3.3 Upgrading
Prompt_toolkit 2.0 is not compatible with 1.0, however you probably want to upgrade your applications. This page explains why we have these differences and how to upgrade.
If you experience some difficulties or you feel that some information is missing from this page, don’t hesitate to open a GitHub issue for help.
Why all these breaking changes?
After more and more custom prompt_toolkit applications were developed, it became clear that prompt_toolkit 1.0 was not flexible enough for certain use cases. Mostly, the development of full screen applications was not really natural. All the important components, like the rendering, key bindings, input and output handling were present, but the API was in the first place designed for simple command line prompts. This was mostly notably in the following two places:
For simple prompts, the changes wouldn’t be that invasive, but given that there would be some, I took the opportunity to fix a couple of other things. For instance:
Given that we were going to break compatibility anyway, we changed a couple of other important things that effect both simple prompt applications and full screen applications. These are the most important:
12 Chapter 3. Table of contents
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
Instead we created something similar to CSS. All UI components can attach classnames to themselves, as well as define an inline style. The final style is then computed by combining the inline styles, the classnames and the style sheet. There are still adaptors available for using Pygments lexers as well as for Pygments styles.
All of these changes have been tested for many months, and I can say with confidence that prompt_toolkit 2.0 is a better prompt_toolkit.
Some new features
Apart from the breaking changes above, there are also some exciting new features.
Upgrading
More guidelines on how to upgrade will follow.
3.3. Upgrading 13
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
Filters
We don’t distiguish anymore between CLIFilter and SimpleFilter , because the application object is no longer passed around. This means that all filters are a Filter from now on.
All filters have been turned into functions. For instance, IsDone became is_done and HasCompletions became has_completions.
This was done because almost all classes were called without any arguments in the init causing additional braces everywhere. This means that HasCompletions() has to be replaced by has_completions (without parenthesis).
The few filters that took arguments as input, became functions, but still have to be called with the given arguments.
For new filters, it is recommended to use the @Condition decorator, rather then inheriting from Filter. For instance:
from prompt_toolkit.filters import Condition
@Condition def my_filter(); return True # Or False
There are two major changes in 3.0 to be aware of:
There are some minor breaking changes:
Detecting the prompt_toolkit version
Detecting whether version 3 is being used can be done as follows:
from prompt_toolkit import version as ptk_version
PTK3 = ptk_version.startswith('3.')
Fixing calls to get_event_loop
Every usage of get_event_loop has to be fixed. An easy way to do this is by changing the imports like this:
if PTK3: from asyncio import get_event_loop else: from prompt_toolkit.eventloop import get_event_loop
3.3. Upgrading 15
prompt 𝑡𝑜𝑜𝑙𝑘𝑖𝑡𝐷𝑜𝑐𝑢𝑚𝑒𝑛𝑡𝑎𝑡𝑖𝑜𝑛, 𝑅𝑒𝑙𝑒𝑎𝑠𝑒 3. 0. 29
Notice that for prompt_toolkit 2.0, get_event_loop returns a prompt_toolkit EventLoop object. This is not an asyncio eventloop, but the API is similar.
There are some changes to the eventloop API:
version 2.0 version 3.0 (asyncio) loop.run_in_executor(callback) loop.run_in_executor(None, callback) loop.call_from_executor(callback) loop.call_soon_threadsafe(callback)
Running on top of asyncio
For 2.0, you had tell prompt_toolkit to run on top of the asyncio event loop. Now it’s the default. So, you can simply remove the following two lines:
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop use_asyncio_event_loop()
There is a few little breaking changes though. The following:
result = await PromptSession().prompt('Say something: ', async_=True)
has to be changed into:
result = await PromptSession().prompt_async('Say something: ')
Further, it’s impossible to call the prompt() function within an asyncio application (within a coroutine), because it will try to run the event loop again. In that case, always use prompt_async().
Changes to the dialog functions
The original way of using dialog boxes looked like this:
from prompt_toolkit.shortcuts import input_dialog
result = input_dialog(title='...', text='...')
Now, the dialog functions return a prompt_toolkit Application object. You have to call either its run or run_async method to display the dialog. The async_ parameter has been removed everywhere.
if PTK3: result = input_dialog(title='...', text='...').run() else: result = input_dialog(title='...', text='...')
if PTK3: result = await input_dialog(title='...', text='...').run_async() else: result = await input_dialog(title='...', text='...', async_=True)
16 Chapter 3. Table of contents