Building full screen applications¶
prompt_toolkit can be used to create complex full screen terminal applications. Typically, an application consists of a layout (to describe the graphical part) and a set of key bindings.
The sections below describe the components required for full screen applications (or custom, non full screen applications), and how to assemble them together.
Before going through this page, it could be helpful to go through asking for input (prompts) first. Many things that apply to an input prompt, like styling, key bindings and so on, also apply to full screen applications.
Also remember that the
examples directory of the prompt_toolkit
repository contains plenty of examples. Each example is supposed to explain
one idea. So, this as well should help you get started.
Don’t hesitate to open a GitHub issue if you feel that a certain example is missing.
A simple application¶
Every prompt_toolkit application is an instance of an
Application object. The simplest full
screen example would look like this:
from prompt_toolkit import Application app = Application(full_screen=True) app.run()
This will display a dummy application that says “No layout specified. Press ENTER to quit.”.
If we wouldn’t set the
full_screen option, the application would
not run in the alternate screen buffer, and only consume the least
amount of space required for the layout.
An application consists of several components. The most important are:
- I/O objects: the input and output device.
- The layout: this defines the graphical structure of the application. For instance, a text box on the left side, and a button on the right side. You can also think of the layout as a collection of ‘widgets’.
- A style: this defines what colors and underline/bold/italic styles are used everywhere.
- A set of key bindings.
We will discuss all of these in more detail below.
Application instance requires an I/O
objects for input and output:
Both are optional and normally not needed to pass explicitly. Usually, the default works fine.
There is a third I/O object which is also required by the application, but not
passed inside. This is the event loop, an
EventLoop instance. This is basically a
while-true loop that waits for user input, and when it receives something (like
a key press), it will send that to the the appropriate handler, like for
instance, a key binding.
A layered layout architecture¶
There are several ways to create a prompt_toolkit layout, depending on how customizable you want things to be. In fact, there are several layers of abstraction.
HSplit(horizontal split) and
FloatContainer. These containers arrange the layout and can split it in multiple regions. Each container can recursively contain multiple other containers. They can be combined in any way to define the “shape” of the layout.
Windowobject is a special kind of container that can contain a
UIControlobject is responsible for the generation of the actual content. The
Windowobject acts as an adaptor between the
UIControland other containers, but it’s also responsible for the scrolling and line wrapping of the content.
A higher level abstraction of building a layout is by using “widgets”. A widget is a reusable layout component that can contain multiple containers and controls. It should have a
__pt__container__function, which is supposed to return the root container for this widget. Prompt_toolkit contains a couple of widgets like
VerticalLineand so on.
The highest level abstractions can be found in the
shortcutsmodule. There we don’t have to think about the layout, controls and containers at all. This is the simplest way to use prompt_toolkit, but is only meant for specific use cases, like a prompt or a simple dialog window.
Containers and controls¶
The biggest difference between containers and controls is that containers arrange the layout by splitting the screen in many regions, while controls are responsible for generating the actual content.
Under the hood, the difference is:
- containers use absolute coordinates, and paint on a
- user controls create a
UIContentinstance. This is a collection of lines that represent the actual content. A
UIControlis not aware of the screen.
|Abstract base class||Examples|
Window class itself is
particular: it is a
can contain a
UIControl. Thus, it’s the adaptor
between the two. The
Window class also takes
care of scrolling the content and wrapping the lines if needed.
Finally, there is the
Layout class which wraps
the whole layout. This is responsible for keeping track of which window has the
Here is an example of a layout that displays the content of the default buffer
on the left, and displays
"Hello world" on the right. In between it shows a
from prompt_toolkit import Application from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import VSplit, Window from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl from prompt_toolkit.layout.layout import Layout buffer1 = Buffer() # Editable buffer. root_container = VSplit([ # One window that holds the BufferControl with the default buffer on # the left. Window(content=BufferControl(buffer=buffer1)), # A vertical line in the middle. We explicitly specify the width, to # make sure that the layout engine will not try to divide the whole # width by three for all these windows. The window will simply fill its # content by repeating this character. Window(width=1, char='|'), # Display the text 'Hello world' on the right. Window(content=FormattedTextControl(text='Hello world')), ]) layout = Layout(root_container) app = Application(layout=layout, full_screen=True) app.run() # You won't be able to Exit this app
Notice that if you execute this right now, there is no way to quit this application yet. This is something we explain in the next section below.
If you want to make some part of the layout only visible when a certain
condition is satisfied, use a
In the following example, we use
for getting the active application.
from prompt_toolkit.application import get_app # This window was created earlier. w = Window() # ... # Now focus it. get_app().layout.focus(w)
Changing the focus is something which is typically done in a key binding, so read on to see how to define key bindings.
There are two kinds of key bindings:
- Global key bindings, which are always active.
- Key bindings that belong to a certain
UIControland are only active when this control is focused. Both
Global key bindings¶
Key bindings can be passed to the application as follows:
from prompt_toolkit import Application from prompt_toolkit.key_binding import KeyBindings kb = KeyBindings() app = Application(key_bindings=kb) app.run()
To register a new keyboard shortcut, we can use the
add() method as a decorator of
the key handler:
from prompt_toolkit import Application from prompt_toolkit.key_binding import KeyBindings kb = KeyBindings() @kb.add('c-q') def exit_(event): """ Pressing Ctrl-Q will exit the user interface. Setting a return value means: quit the event loop that drives the user interface and return this value from the `CommandLineInterface.run()` call. """ event.app.exit() app = Application(key_bindings=kb, full_screen=True) app.run()
The callback function is named
exit_ for clarity, but it could have been
_ (underscore) as well, because the we won’t refer to this name.
If this flag has been set, then key bindings from the parent account are not taken into account if one of the children windows has the focus.
This is useful in a complex layout, where many controls have their own key bindings, but you only want to enable the key bindings for a certain region of the layout.
The global key bindings are always active.
More about the Window class¶
Basically, windows are the leafs in the tree structure that represent the UI.
Window provides a “view” on the
UIControl, which provides lines of content. The
window is in the first place responsible for the line wrapping and scrolling of
the content, but there are much more options.
- Adding left or right margins. These are used for displaying scroll bars or line numbers.
- There are the cursorline and cursorcolumn options. These allow highlighting the line or column of the cursor position.
- Alignment of the content. The content can be left aligned, right aligned or centered.
- Finally, the background can be filled with a default character.
More about buffers and BufferControl¶
Processor operates on individual
lines. Basically, it takes a (formatted) line and produces a new (formatted)
Some build-in processors:
||Highlight the current search results.|
||Highlight the selection.|
||Display input as asterisks. (
||Highlight open/close mismatches for brackets.|
||Insert some text before.|
||Insert some text after.|
||Append auto suggestion text.|
||Visualise leading whitespace.|
||Visualise trailing whitespace.|
||Visualise tabs as n spaces, or some symbols.|