rich.prompt

class rich.prompt.Confirm(*args, **kwds)[source]

A yes / no confirmation prompt.

Example

>>> if Confirm.ask("Continue"):
        run_job()
process_response(value: str) → bool[source]

Convert choices to a bool.

render_default(default: DefaultType)rich.text.Text[source]

Render the default as (y) or (n) rather than True/False.

response_type

alias of builtins.bool

class rich.prompt.FloatPrompt(*args, **kwds)[source]

A prompt that returns a float.

Example

>>> temperature = FloatPrompt.ask("Enter desired temperature")
response_type

alias of builtins.float

class rich.prompt.IntPrompt(*args, **kwds)[source]

A prompt that returns an integer.

Example

>>> burrito_count = IntPrompt.ask("How many burritos do you want to order", prompt_suffix="? ")
response_type

alias of builtins.int

exception rich.prompt.InvalidResponse(message: Union[str, Text])[source]

Exception to indicate a response was invalid. Raise this within process_response() to indicate an error and provide an error message.

Args:

message (Union[str, Text]): Error message.

class rich.prompt.Prompt(*args, **kwds)[source]

A prompt that returns a str.

Example

>>> name = Prompt.ask("Enter your name")
response_type

alias of builtins.str

class rich.prompt.PromptBase(*args, **kwds)[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: rich.console.Console = 'None', password: bool = 'False', choices: List[str] = 'None', show_default: bool = 'True', show_choices: bool = 'True', default: DefaultType, stream: TextIO = 'None') → Union[DefaultType, PromptType][source]
classmethod ask(prompt: Union[str, Text] = "''", *, console: rich.console.Console = 'None', password: bool = 'False', choices: List[str] = 'None', show_default: bool = 'True', show_choices: bool = 'True', stream: 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.

check_choice(value: str) → bool[source]

Check value is in the list of valid choices.

Parameters

value (str) – Value entered by user.

Returns

True if choice was valid, otherwise False.

Return type

bool

classmethod get_input(console: rich.console.Console, prompt: Union[str, Text], password: bool, stream: TextIO = None) → str[source]

Get input from user.

Parameters
  • console (Console) – Console instance.

  • prompt (TextType) – Prompt text.

  • password (bool) – Enable password entry.

Returns

String from user.

Return type

str

make_prompt(default: DefaultType)rich.text.Text[source]

Make prompt text.

Parameters

default (DefaultType) – Default value.

Returns

Text to display in prompt.

Return type

Text

on_validate_error(value: str, error: rich.prompt.InvalidResponse) → None[source]

Called to handle validation error.

Parameters
  • value (str) – String entered by user.

  • error (InvalidResponse) – Exception instance the initiated the error.

pre_prompt() → None[source]

Hook to display something before the prompt.

process_response(value: str) → PromptType[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

render_default(default: DefaultType)rich.text.Text[source]

Turn the supplied default in to a Text instance.

Parameters

default (DefaultType) – Default value.

Returns

Text containing rendering of default value.

Return type

Text

response_type

alias of builtins.str

exception rich.prompt.PromptError[source]

Exception base class for prompt related errors.