Python rich 库 - Pretty Printing

Prompt

Rich 有许多 Prompt 类,它们用于要求用户输入信息并循环,直到收到有效的响应(它们都在内部使用 Console API

1
2
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name")

The prompt may be given as a string (which may contain Console Markup and emoji code) or as a Text instance.

You can set a default value which will be returned if the user presses return without entering any text:

1
2
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", default="Paul Atreides")

If you supply a list of choices, the prompt will loop until the user enters one of the choices:

1
2
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul")

In addition to Prompt which returns strings, you can also use IntPrompt which asks the user for an integer, and FloatPrompt for floats.

The Confirm class is a specialized prompt which may be used to ask the user a simple yes / no question. Here’s an example:

1
2
3
>>> from rich.prompt import Confirm
>>> is_rich_great = Confirm.ask("Do you like rich?")
>>> assert is_rich_great

The Prompt class was designed to be customizable via inheritance. See prompt.py for examples.

To see some of the prompts in action, run the following command from the command line:

1
python -m rich.prompt