rich.prompt¶
- class rich.prompt.Confirm(prompt='', *, console=None, password=False, choices=None, show_default=True, show_choices=True)[source]¶
A yes / no confirmation prompt.
Example
>>> if Confirm.ask("Continue"): run_job()
- class rich.prompt.FloatPrompt(prompt='', *, console=None, password=False, choices=None, show_default=True, show_choices=True)[source]¶
A prompt that returns a float.
Example
>>> temperature = FloatPrompt.ask("Enter desired temperature")
- class rich.prompt.IntPrompt(prompt='', *, console=None, password=False, choices=None, show_default=True, show_choices=True)[source]¶
A prompt that returns an integer.
Example
>>> burrito_count = IntPrompt.ask("How many burritos do you want to order")
- exception rich.prompt.InvalidResponse(message)[source]¶
Exception to indicate a response was invalid. Raise this within process_response() to indicate an error and provide an error message.
- class rich.prompt.Prompt(prompt='', *, console=None, password=False, choices=None, show_default=True, show_choices=True)[source]¶
A prompt that returns a str.
Example
>>> name = Prompt.ask("Enter your name")
- class rich.prompt.PromptBase(prompt='', *, console=None, password=False, choices=None, show_default=True, show_choices=True)[source]¶
Ask the user for input until a valid response is received. This is the base class, see one of the concrete classes for examples.
- Parameters
prompt (TextType, optional) – Prompt text. Defaults to “”.
console (Console, optional) – A Console instance or None to use global console. Defaults to None.
password (bool, optional) – Enable password input. Defaults to False.
choices (List[str], optional) – A list of valid choices. Defaults to None.
show_default (bool, optional) – Show default in prompt. Defaults to True.
show_choices (bool, optional) – Show choices in prompt. Defaults to True.
- classmethod ask(prompt: Union[str, Text] = '', *, console: Optional[Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True, default: DefaultType, stream: Optional[TextIO] = None) Union[DefaultType, PromptType] [source]¶
- classmethod ask(prompt: Union[str, Text] = '', *, console: Optional[Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True, stream: Optional[TextIO] = None) PromptType
Shortcut to construct and run a prompt loop and return the result.
Example
>>> filename = Prompt.ask("Enter a filename")
- Parameters
prompt (TextType, optional) – Prompt text. Defaults to “”.
console (Console, optional) – A Console instance or None to use global console. Defaults to None.
password (bool, optional) – Enable password input. Defaults to False.
choices (List[str], optional) – A list of valid choices. Defaults to None.
show_default (bool, optional) – Show default in prompt. Defaults to True.
show_choices (bool, optional) – Show choices in prompt. Defaults to True.
stream (TextIO, optional) – Optional text file open for reading to get input. Defaults to None.
- make_prompt(default)[source]¶
Make prompt text.
- Parameters
default (DefaultType) – Default value.
- Returns
Text to display in prompt.
- Return type
- on_validate_error(value, error)[source]¶
Called to handle validation error.
- Parameters
value (str) – String entered by user.
error (InvalidResponse) – Exception instance the initiated the error.
- Return type
None
- process_response(value)[source]¶
Process response from user, convert to prompt type.
- Parameters
value (str) – String typed by user.
- Raises
InvalidResponse – If
value
is invalid.- Returns
The value to be returned from ask method.
- Return type
PromptType