Introduction to prompt-toolkit
prompt-toolkit is a powerful, library written in Python designed to create rich command-line interfaces (CLIs) with ease. Whether you are developing a simple application or a complex, multi-functional CLI, prompt-toolkit provides dozens of useful APIs and customization options to enhance the user experience. In this blog post, we will explore some of the most notable APIs and features of the prompt-toolkit library with code snippets and examples.
Key Features and API Examples
1. Basic Prompt
The most basic function of prompt-toolkit is to display a prompt to the user and capture the input.
from prompt_toolkit import prompt
user_input = prompt('Enter something: ') print(f'You entered: {user_input}')
2. Custom Key Bindings
Customize key bindings for additional control over user interactions.
from prompt_toolkit import prompt from prompt_toolkit.key_binding import KeyBindings
bindings = KeyBindings()
@bindings.add('c-c') def _(event):
" Pressing Ctrl-C will exit the application. "
event.app.exit()
user_input = prompt('Enter something: ', key_bindings=bindings) print(f'You entered: {user_input}')
3. Input Validation
Validate user input with custom validation functions.
from prompt_toolkit import prompt from prompt_toolkit.validation import Validator, ValidationError
def is_number(text):
if not text.isdigit():
raise ValidationError(message='Input must be a number', cursor_position=len(text))
user_input = prompt('Enter a number: ', validator=Validator.from_callable(is_number)) print(f'You entered a number: {user_input}')
4. Autocompletion
Provide dynamic autocompletion functionality to improve the user input experience.
from prompt_toolkit import prompt from prompt_toolkit.completion import WordCompleter
fruits = ['apple', 'banana', 'cherry', 'date'] fruit_completer = WordCompleter(fruits)
user_input = prompt('Enter a fruit: ', completer=fruit_completer) print(f'You entered: {user_input}')
5. Styling Customization
Customize the look and feel of the prompt interface with styles.
from prompt_toolkit import prompt from prompt_toolkit.styles import Style
style = Style.from_dict({
'': '#ff0066',
'prompt': 'bold #ffee00',
})
user_input = prompt('[prompt]Enter styled text: ', style=style) print(f'You entered: {user_input}')
Application Example
Let’s see a complete application that integrates multiple APIs from prompt-toolkit.
from prompt_toolkit import PromptSession from prompt_toolkit.completion import WordCompleter from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.styles import Style from prompt_toolkit.validation import Validator, ValidationError
fruits = ['apple', 'banana', 'cherry', 'date'] fruit_completer = WordCompleter(fruits)
bindings = KeyBindings() @bindings.add('c-c') def _(event):
event.app.exit()
def is_fruit(text):
if text not in fruits:
raise ValidationError(message='Input must be a known fruit', cursor_position=len(text))
style = Style.from_dict({
'': '#ff0066',
'prompt': 'bold #ffee00',
})
session = PromptSession('Enter a fruit: ', validator=Validator.from_callable(is_fruit), completer=fruit_completer,
key_bindings=bindings, style=style)
while True:
try:
user_input = session.prompt()
print(f'You entered: {user_input}')
except KeyboardInterrupt:
break
By mastering these APIs, you’ll be able to develop intuitive and powerful command-line interfaces with prompt-toolkit.
Hash: dc4f2e5d20c4aad7d4cf2b8e35003155ca87c1320a564ff0c53e338db0b4e096