text¶
A text prompt that accepts user input.
Example¶
Classic Syntax (PyInquirer)
from InquirerPy import prompt
from InquirerPy.validator import NumberValidator
def main():
"""Classic syntax example."""
questions = [
{"type": "input", "message": "Enter your name:"},
{
"type": "input",
"message": "Which company would you like to apply:",
"completer": {
"Google": None,
"Facebook": None,
"Amazon": None,
"Netflix": None,
"Apple": None,
"Microsoft": None,
},
"multicolumn_complete": True,
},
{
"type": "input",
"message": "What's your salary expectation(k):",
"transformer": lambda result: "%sk" % result,
"filter": lambda result: int(result) * 1000,
"validate": NumberValidator(),
},
]
result = prompt(questions)
if __name__ == "__main__":
main()
Alternate Syntax
from InquirerPy import inquirer
from InquirerPy.validator import NumberValidator
def main():
"""Alternate syntax example."""
name = inquirer.text(message="Enter your name:").execute()
company = inquirer.text(
message="Which company would you like to apply:",
completer={
"Google": None,
"Facebook": None,
"Amazon": None,
"Netflix": None,
"Apple": None,
"Microsoft": None,
},
multicolumn_complete=True,
).execute()
salary = inquirer.text(
message="What's your salary expectation(k):",
transformer=lambda result: "%sk" % result,
filter=lambda result: int(result) * 1000,
validate=NumberValidator(),
).execute()
if __name__ == "__main__":
main()
Keybindings¶
See also
{
"answer": [{"key": "enter"}], # answer the prompt
"interrupt": [{"key": "c-c"}], # raise KeyboardInterrupt
"skip": [{"key": "c-z"}], # skip the prompt
}
Besides the default keybindings and input buffer keybindings, if you have autocompletion enabled, you can use
ctrl-space
to trigger completion window popup.
{
"completion": [{"key": "c-space"}] # force completion popup
}
Auto Completion¶
Tip
Use ctrl-space
to force completion window popup.
You can add auto completion to the prompt via the parameter/key completer
. Provide a Completer
class or a dictionary of words to enable auto-completion of the prompt.
Below is a simple dict
completer.
Classic Syntax
from InquirerPy import prompt
completer = {
"hello": {
"world": None
},
"foo": {
"boo": None
},
"fizz": {
"bazz": None
}
}
questions = [
{
"type": "input",
"message": "FooBoo:",
"completer": completer
}
]
result = prompt(questions=questions)
Alternate Syntax
from InquirerPy import inquirer
completer = {
"hello": {
"world": None
},
"foo": {
"boo": None
},
"fizz": {
"bazz": None
}
}
result = inquirer.text(message="FooBoo:", completer=completer).execute()
Checkout prompt_toolkit
documentation
for more examples and information on how to create more dynamic/complex completer.
Multi-line Input¶
By setting the parameter multiline
to True
, the prompt will change from single line input to multiple line input.
While multiline
is True
, enter
will causing a new line to be used instead of finish answering the question. Press
esc
and then press enter
to finish answer the question.
from InquirerPy import inquirer
result = inquirer.text(message="FooBoo:", multiline=True).execute()
Reference¶
- class InquirerPy.prompts.input.InputPrompt(message, style=None, vi_mode=False, default='', qmark='?', amark='?', instruction='', long_instruction='', completer=None, multicolumn_complete=False, multiline=False, validate=None, invalid_message='Invalid input', transformer=None, filter=None, keybindings=None, wrap_lines=True, raise_keyboard_interrupt=True, is_password=False, mandatory=True, mandatory_message='Mandatory prompt', session_result=None, input=None, output=None)[source]
Create a text prompt that accepts user input.
A wrapper class around
PromptSession
.- Parameters
message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message 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 text value of the prompt. Refer to default documentation for more details.
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.
instruction (str) – Short instruction to display next to the question.
long_instruction (str) – Long instructions to display at the bottom of the prompt.
completer (Optional[Union[Dict[str, Optional[str]], Completer]]) – Add auto completion to the prompt. Refer to Auto Completion documentation for more details.
multicolumn_complete (bool) – Change the auto-completion UI to a multi column display.
multiline (bool) – Enable multiline edit. While multiline edit is active, pressing enter won’t complete the answer. and will create a new line. Use esc followd by enter to complete the question.
validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. 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[[str], 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[[str], 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.keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.
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.is_password (bool) – Used internally for
SecretPrompt
.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).
input (Optional[Input]) – Used internally and will be removed in future updates.
output (Optional[Output]) – Used internally and will be removed in future updates.
- Return type
None
Examples
>>> from InquirerPy import inquirer >>> result = inquirer.text(message="Enter your name:").execute() >>> print(f"Name: {result}") Name: Michael