rawlist¶
A prompt that displays a list of choices and can use index numbers as key jump shortcuts.
Example¶
Classic Syntax (PyInquirer)
from InquirerPy import prompt
from InquirerPy.base.control import Choice
from InquirerPy.separator import Separator
def main():
questions = [
{
"type": "rawlist",
"choices": [
"Apple",
"Orange",
"Peach",
"Cherry",
"Melon",
"Strawberry",
"Grapes",
],
"message": "Pick your favourites:",
"default": 3,
"multiselect": True,
"transformer": lambda result: ", ".join(result),
"validate": lambda result: len(result) > 1,
"invalid_message": "Minimum 2 selections",
},
{
"type": "rawlist",
"choices": [
Choice(name="Delivery", value="dl"),
Choice(name="Pick Up", value="pk"),
Separator(line=15 * "*"),
Choice(name="Car Park", value="cp"),
Choice(name="Third Party", value="tp"),
],
"message": "Select your preferred method:",
},
]
result = prompt(questions)
if __name__ == "__main__":
main()
Alternate Syntax
from InquirerPy import inquirer
from InquirerPy.base.control import Choice
from InquirerPy.separator import Separator
def main():
fruit = inquirer.rawlist(
message="Pick your favourites:",
choices=[
"Apple",
"Orange",
"Peach",
"Cherry",
"Melon",
"Strawberry",
"Grapes",
],
default=3,
multiselect=True,
transformer=lambda result: ", ".join(result),
validate=lambda result: len(result) > 1,
invalid_message="Minimum 2 selections",
).execute()
method = inquirer.rawlist(
message="Select your preferred method:",
choices=[
Choice(name="Delivery", value="dl"),
Choice(name="Pick Up", value="pk"),
Separator(line=15 * "*"),
Choice(name="Car Park", value="cp"),
Choice(name="Third Party", value="tp"),
],
).execute()
if __name__ == "__main__":
main()
Choices¶
See also
For this specific prompt, due to the shortcut being created is between 1-9, the total length of choices cannot exceed 10.
Keybindings¶
See also
Hint
In addition to the keybindings mentioned below, keybindings are created for keys 1-9 to jump to the target index choices.
{
"answer": [{"key": "enter"}], # answer the prompt
"interrupt": [{"key": "c-c"}], # raise KeyboardInterrupt
"skip": [{"key": "c-z"}], # skip the prompt
}
The following dictionary contains the additional keybindings created by this prompt.
{
"down": [
{"key": "down"},
{"key": "c-n"}, # move down
],
"up": [
{"key": "up"},
{"key": "c-p"}, # move up
],
"toggle": [
{"key": "space"}, # toggle choices
],
"toggle-down": [
{"key": "c-i"}, # toggle choice and move down (tab)
],
"toggle-up": [
{"key": "s-tab"}, # toggle choice and move up (shift+tab)
],
"toggle-all": [
{"key": "alt-r"}, # toggle all choices
{"key": "c-r"},
],
"toggle-all-true": [
{"key": "alt-a"}, # toggle all choices true
{"key": "c-a"}.
],
"toggle-all-false": [], # toggle all choices false
}
When vi_mode
is True, the “up” and “down” navigation key will be changed.
{
"down": [
{"key": "down"},
{"key": "j"},
],
"up": [
{"key": "up"},
{"key": "k"},
],
}
Multiple Selection¶
See also
Default Value¶
See also
The default
parameter for rawlist can be three types of values:
shortcut index (int): an
int
value between 1-9 and the default value index choice will be highlighted.choice value (Any): default value could the value of one of the choices.
Reference¶
- class InquirerPy.prompts.rawlist.RawlistPrompt(message, choices, default=None, separator=') ', style=None, vi_mode=False, qmark='?', amark='?', pointer=' ', instruction='', long_instruction='', transformer=None, filter=None, height=None, max_height=None, multiselect=False, marker='❯', marker_pl=' ', border=False, validate=None, invalid_message='Invalid input', keybindings=None, show_cursor=True, cycle=True, wrap_lines=True, raise_keyboard_interrupt=True, mandatory=True, mandatory_message='Mandatory prompt', session_result=None)[source]
Create a prompt that displays a list of choices with index number as shortcuts.
A wrapper class around
Application
.Each choice will have an index number infront of them with keybinding created for that index number. Enables user to use number to jump to different choices using number.
- Parameters
message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message documentation for more details.
choices (Union[Callable[[InquirerPySessionResult], Union[List[Any], List[Choice], List[Dict[str, Any]]]], List[Any], List[Choice], List[Dict[str, Any]]]) – List of choices to display and select. Refer to Choices documentation for more details.
style (Optional[InquirerPy.utils.InquirerPyStyle]) – An
InquirerPyStyle
instance. Refer to Style documentation for more details.vi_mode (bool) – Use vim keybinding for the prompt. Refer to keybindings documentation for more details.
default (Union[Any, Callable[[InquirerPySessionResult], Any]]) – Set the default value of the prompt. This will be used to determine which choice is highlighted (current selection), The default value should be the value of one of the choices. For
RawlistPrompt
specifically, default value can also be value between 0-9. Refer to default documentation for more details.separator (str) – Separator symbol. Custom symbol that will be used as a separator between the choice index number and the choices.
qmark (str) – Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.
amark (str) – Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.
pointer (str) – Pointer symbol. Customer symbol that will be used to indicate the current choice selection.
instruction (str) – Short instruction to display next to the question.
long_instruction (str) – Long instructions to display at the bottom of the prompt.
validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. The main use case for this prompt would be when multiselect is True, you can enforce a min/max selection. Refer to Validator documentation for more details.
invalid_message (str) – Error message to display when user input is invalid. Refer to Validator documentation for more details.
transformer (Optional[Callable[[Any], Any]]) – A function which performs additional transformation on the value that gets printed to the terminal. Different than filter parameter, this is only visual effect and won’t affect the actual value returned by
execute()
. Refer to transformer documentation for more details.filter (Optional[Callable[[Any], Any]]) – A function which performs additional transformation on the result. This affects the actual value returned by
execute()
. Refer to filter documentation for more details.height (Optional[Union[int, str]]) – Preferred height of the prompt. Refer to height documentation for more details.
max_height (Optional[Union[int, str]]) – Max height of the prompt. Refer to height documentation for more details.
multiselect (bool) – Enable multi-selection on choices. You can use validate parameter to control min/max selections. Setting to True will also change the result from a single value to a list of values.
marker (str) – Marker Symbol. Custom symbol to indicate if a choice is selected. This will take effects when multiselect is True.
marker_pl (str) – Marker place holder when the choice is not selected. This is empty space by default.
border (bool) – Create border around the choice window.
keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.
show_cursor (bool) – Display cursor at the end of the prompt. Set to False to hide the cursor.
cycle (bool) – Return to top item if hit bottom during navigation or vice versa.
wrap_lines (bool) – Soft wrap question lines when question exceeds the terminal width.
raise_keyboard_interrupt (bool) – Raise the
KeyboardInterrupt
exception when ctrl-c is pressed. If false, the result will be None and the question is skiped.mandatory (bool) – Indicate if the prompt is mandatory. If True, then the question cannot be skipped.
mandatory_message (str) – Error message to show when user attempts to skip mandatory prompt.
session_result (Optional[Dict[Union[str, int], Optional[Union[str, bool, List[Any]]]]]) – Used internally for Classic Syntax (PyInquirer).
- Return type
None
Examples
>>> from InquirerPy import inquirer >>> result = inquirer.rawlist(message="Select one:", choices=[1, 2, 3]).execute() >>> print(result) 1