rich.prompt¶
-
class
rich.prompt.
Confirm
(prompt: Union[str, rich.text.Text] = '', *, console: Optional[rich.console.Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True)[source]¶ A yes / no confirmation prompt.
Example
>>> if Confirm.ask("Continue"): run_job()
-
render_default
(default: DefaultType) → rich.text.Text[source]¶ Render the default as (y) or (n) rather than True/False.
-
response_type
¶ alias of
bool
-
-
class
rich.prompt.
FloatPrompt
(prompt: Union[str, rich.text.Text] = '', *, console: Optional[rich.console.Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True)[source]¶ A prompt that returns a float.
Example
>>> temperature = FloatPrompt.ask("Enter desired temperature")
-
response_type
¶ alias of
float
-
-
class
rich.prompt.
IntPrompt
(prompt: Union[str, rich.text.Text] = '', *, console: Optional[rich.console.Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True)[source]¶ A prompt that returns an integer.
Example
>>> burrito_count = IntPrompt.ask("How many burritos do you want to order")
-
response_type
¶ alias of
int
-
-
exception
rich.prompt.
InvalidResponse
(message: Union[str, rich.text.Text])[source]¶ Exception to indicate a response was invalid. Raise this within process_response() to indicate an error and provide an error message.
- Parameters
message (Union[str, Text]) – Error message.
-
class
rich.prompt.
Prompt
(prompt: Union[str, rich.text.Text] = '', *, console: Optional[rich.console.Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = True)[source]¶ A prompt that returns a str.
Example
>>> name = Prompt.ask("Enter your name")
-
response_type
¶ alias of
str
-
-
class
rich.prompt.
PromptBase
(prompt: Union[str, rich.text.Text] = '', *, console: Optional[rich.console.Console] = None, password: bool = False, choices: Optional[List[str]] = None, show_default: bool = True, show_choices: bool = 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: 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, rich.text.Text], password: bool, stream: Optional[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
-
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.
-
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
-
response_type
¶ alias of
str