Wonderful Python Libraries series Nº 1 :: Prompt Toolkit

cr0hn
2 min readAug 20, 2022

Today I start a new series of posts with my favorite Python libraries. I’ll share with you those that, I think, is most interesting.

In this post, I’ll present the library: Prompt Toolkit.

If you like CLI and terminals for writing your tools, You’ll like this library.

https://python-prompt-toolkit.readthedocs.io/en/master/index.html

This library, in a few words

With Prompt toolkit, you can create a rich console UI. Some of their features are:

  • Integrated auto-completion and auto-suggestion.
  • Integrated syntax highlighting support.
  • Integrated session history.
  • Key binding
  • Ncurses abstraction in Python.
  • Real-time progress bars with ncurses.
  • Parallel progress bars.
  • Create full-screen console apps.
  • Hooks.
  • Asyncio support.
  • Deadline support.
  • And more!

Some examples

A REPL with steroids

Creating a powerful REPL is straightforward. You can add syntax highlighting in the prompt in real time! Just great.

from pygments.lexers.html import HtmlLexer
from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.lexers import PygmentsLexer

text = prompt('Hi Medium! Enter HTML: ', lexer=PygmentsLexer(HtmlLexer))
print('You wrote: %s' % text)

Ncurses dialogs and more

With Prompt toolkit, you can do excellent command line interfaces without the need to write ncurses code. It has shortcuts for creating dialogs, checkboxes, radio boxes, and more.

Here is an easy example:

from prompt_toolkit.shortcuts import radiolist_dialog

result = radiolist_dialog(
title="Hi Medium!",
text="What's your favorite IDE?",
values=[
("pycharm", "PyCharm"),
("vscode", "Vistual Studio Code"),
("wingide", "WingIDE")
]
).run()

VIM clone

I recommend you to visit their Gallery section on its website:

You can find great demos, even a VIM clone!

--

--

cr0hn

Cybersecurity is a tricky business. I’m a freelancer helping companies avoid nasty surprises