A library of elements for interactive TUIs in Python
Project description
beaupy
:star: A library of interactive CLI elements you have been looking for :keyboard:
beaupy implements a number of common interactive elements:
| Function | Functionality |
|---|---|
select |
Prompt to pick a choice from a list |
select_multiple (checkboxes) |
Prompt to select one or multiple choices from a list |
confirm |
Prompt with a question and yes/no options |
prompt |
Prompt that takes input with optional validation, type conversion and masking |
Usage
TUI elements shown in the above gif are the result of the follwing code:
import beaupy
def main():
"""Main."""
if beaupy.confirm("Are you brave enough to continue?"):
names = [
"Arthur, King of the Britons",
"Sir Lancelot the Brave",
"Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot",
"Sir Bedevere the Wise",
"Sir Galahad the Pure",
]
name = names[beaupy.select(names, cursor_index=3)]
print(f"Welcome, {name}")
# Get an integer greater or equal to 0
age = beaupy.prompt("What is your age?", type=int, validator=lambda val: val > 0)
nemeses_options = [
"The French",
"The Police",
"The Knights Who Say Ni",
"Women",
"The Black Knight",
"The Bridge Keeper",
"The Rabbit of Caerbannog",
]
print("Choose your nemeses")
# Choose multiple options from a list
nemeses_indices = beaupy.select_multiple(nemeses_options)
nemeses = [
nemesis
for nemesis_index, nemesis in enumerate(nemeses_options)
if nemesis_index in nemeses_indices
]
# Get input without showing it being typed
quest = beaupy.prompt("What is your quest?", secure=True)
print(f"{name}'s quest (who is {age}) is {quest}.")
if nemeses:
if len(nemeses) == 1:
print(f"His nemesis is {nemeses[0]}.")
else:
print(f'His nemeses are {" and ".join(nemeses)}.')
else:
print("He has no nemesis.")
Installation
From source:
git clone https://github.com/petereon/beaupy.git
poetry build
pip install ./dist/beaupy-{{some-version}}-py3-none-any.whl
Roadmap
- Achieve >90% test coverage
- PyPI Release
- Extend
selectwith filterability - ...
Docmentation
beaupy is a library of interactive TUI elements for CLI applications. It is based on another library built for the same purpose, cutie.
In comparison, beaupy is
API Doc
prompt
def prompt(prompt: str,
type: Union[Type[T], Type[str]] = str,
validator: Callable[[Any], bool] = lambda input: True,
secure: bool = False) -> Union[T, str]
Function that prompts the user for written input
Arguments:
promptstr - The prompt that will be displayedtypeUnion[Type[T], Type[str]], optional - Type to convert the answer to. Defaults to str.validatorCallable[[Any], bool], optional - Optional function to validate the input. Defaults to lambdainput:True.securebool, optional - If True, input will be hidden. Defaults to False.
Raises:
ValidationError- Raised if validation with provided validator failsConversionError- Raised if the value cannot be converted to provided typeKeyboardInterrupt- Raised when keyboard interrupt is encountered
Returns:
Union[T, str]: Returns a value formatted as provided type or string if no type is provided
select
def select(options: List[str],
cursor: str = "> ",
cursor_style="pink1",
cursor_index: int = 0,
strict: bool = False) -> Union[int, None]
A prompt that allows selecting one option from a list of options
Arguments:
optionsList[str] - A list of options to select fromcursorstr, optional - Cursor that is going to appear in front of currently selected option. Defaults to '> '.cursor_stylestr, optional - Rich friendly style for the cursor. Defaults to 'pink1'.cursor_indexint, optional - Option can be preselected based on its list index. Defaults to 0.strictbool, optional - If emptyoptionsis provided and strict isFalse, None will be returned, if it'sTrue,ValueErrorwill be thrown. Defaults to False.
Raises:
ValueError- Thrown if nooptionsare povided and strict isTrue
Returns:
Union[int, None]: Index of a selected option or None
select_multiple
def select_multiple(options: List[str],
tick_character: str = "x",
tick_style: str = "cyan1",
cursor_style: str = "pink1",
ticked_indices: Optional[List[int]] = None,
cursor_index: int = 0,
minimal_count: int = 0,
maximal_count: Optional[int] = None,
strict: bool = False) -> List[int]
A prompt that allows selecting multiple options from a list of options
Arguments:
optionsList[str] - A list of options to select fromtick_characterstr, optional - Character that will be used as a tick in a checkbox. Defaults to 'x'.tick_stylestr, optional - Rich friendly style for the tick character. Defaults to 'cyan1'.cursor_stylestr, optional - Rich friendly style for the option when the cursor is currently on it. Defaults to 'pink1'.ticked_indicesOptional[List[int]], optional - Indices of options that are pre-ticked when the prompt appears. Defaults to None.cursor_indexint, optional - Index of the option cursor starts at. Defaults to 0.minimal_countint, optional - Minimal count of options that need to be selected. Defaults to 0.maximal_countOptional[int], optional - Maximal count of options that need to be selected. Defaults to None.strictbool, optional - If emptyoptionsis provided and strict isFalse, None will be returned, if it'sTrue,ValueErrorwill be thrown. Defaults to False.
Raises:
KeyboardInterrupt- Raised when Ctrl+C is encountered
Returns:
List[int]- A list of selected indices
confirm
def confirm(question: str,
yes_text: str = "Yes",
no_text: str = "No",
has_to_match_case: bool = False,
enter_empty_confirms: bool = True,
default_is_yes: bool = False,
cursor: str = "> ",
cursor_style: str = "magenta1",
char_prompt: bool = True) -> Optional[bool]
A prompt that asks a question and offers two responses
Arguments:
questionstr - Question to be askedyes_textstr, optional - Text of the positive response. Defaults to 'Yes'.no_textstr, optional - Text of the negative response. Defaults to 'No'.has_to_match_casebool, optional - Check if typed response matches case. Defaults to False.enter_empty_confirmsbool, optional - No response is confirmation. Defaults to True.default_is_yesbool, optional - Default is Yes. Defaults to False.cursorstr, optional - What character(s) to use as a cursor. Defaults to '> '.cursor_stylestr, optional - Rich friendly style for the cursor. Defaults to 'magenta1'.char_promptbool, optional - Print [Y/n] after the question. Defaults to True.
Raises:
KeyboardInterrupt- Raised when Ctrl+C is encountered
Returns:
Optional[bool]
Contributing
If you want to contribute, please feel free to suggest features or implement them yourself.
Also please report any issues and bugs you might find!
License
The project is licensed under the MIT-License.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file beaupy-0.1.7.tar.gz.
File metadata
- Download URL: beaupy-0.1.7.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
813631b9bb17ed961da3e71fe4ee0529cb1f8731eb33ff1132f39e18968ed4b6
|
|
| MD5 |
e60d75e9711a8ef346b6acda532cff46
|
|
| BLAKE2b-256 |
447620c178d4d2d58d060ad4db51b2894eda18cd23f580631e0de4c2e1a21f4e
|
File details
Details for the file beaupy-0.1.7-py3-none-any.whl.
File metadata
- Download URL: beaupy-0.1.7-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fb7bdbea339d6cb6cc40dd13c443f7072754ab33635515395f046da8ded1a34
|
|
| MD5 |
f8f5b72ad5a000546250795e90f34c6b
|
|
| BLAKE2b-256 |
1b8eac1baa5488bdef2e2f21fa330301a9e7949498722c0d0c0f9d7267ae10b3
|