Skip to content

Commit 3403456

Browse files
Docs.
1 parent 48e44ab commit 3403456

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Table of contents
6969
pages/upgrading/index
7070
pages/printing_text
7171
pages/asking_for_input
72+
pages/choice_prompts
7273
pages/dialogs
7374
pages/progress_bars
7475
pages/full_screen_apps

docs/pages/choice_prompts.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. _asking_for_input:
2+
3+
Asking for a choice
4+
===================
5+
6+
Similar to how the :func:`~prompt_toolkit.shortcuts.prompt` function allows for
7+
text input, prompt_toolkit has a
8+
:func:`~prompt_toolkit.shortcuts.prompt_choice` function to ask for a choice
9+
from a list of options:
10+
11+
.. code:: python
12+
13+
from prompt_toolkit.shortcuts import prompt_choice
14+
15+
result = prompt_choice(
16+
message="Please choose a dish",
17+
options=[
18+
("pizza", "Pizza with mushrooms"),
19+
("salad", "Salad with tomatoes"),
20+
("sushi", "Sushi"),
21+
],
22+
default="salad",
23+
)
24+
25+
print(f"You have chosen: {text}")
26+
27+
28+
Coloring the options
29+
--------------------
30+
31+
Similar to text input prompts, it is possible to customize the colors and
32+
styles. The ``message`` parameter takes any :ref:`formatted text
33+
<formatted_text>`, and the labels (2nd argument of the options) can also be
34+
:ref:`formatted text <formatted_text>`. Further, we can pass a
35+
:class:`~prompt_toolkit.styles.Style` instance using the
36+
:meth:`~prompt_toolkit.styles.Style.from_dict` function:
37+
38+
.. code:: python
39+
40+
from __future__ import annotations
41+
42+
from prompt_toolkit.formatted_text import HTML
43+
from prompt_toolkit.shortcuts import prompt_choice
44+
from prompt_toolkit.styles import Style
45+
46+
style = Style.from_dict(
47+
{
48+
"input-selection": "fg:#ff0000",
49+
"number": "fg:#884444 bold",
50+
"selected-option": "underline",
51+
"frame.border": "#884444",
52+
}
53+
)
54+
55+
result = prompt_choice(
56+
message=HTML("<u>Please select a dish</u>:"),
57+
options=[
58+
("pizza", "Pizza with mushrooms"),
59+
(
60+
"salad",
61+
HTML("<ansigreen>Salad</ansigreen> with <ansired>tomatoes</ansired>"),
62+
),
63+
("sushi", "Sushi"),
64+
],
65+
style=style,
66+
)
67+
print(result)
68+
69+
70+
Adding a frame
71+
--------------
72+
73+
The :func:`~prompt_toolkit.shortcuts.prompt_choice` function takes a
74+
``show_frame`` argument. When set to ``True``, the options will be shown
75+
inside a frame. It is also possible to pass a :ref:`filter <filters>`, like
76+
``~is_done``, so that the frame is only displayed when asking for input, but
77+
hidden once the input is accepted.
78+
79+
.. code:: python
80+
81+
from prompt_toolkit.shortcuts import prompt_choice
82+
from prompt_toolkit.filters import is_done
83+
from prompt_toolkit.styles import Style
84+
85+
style = Style.from_dict(
86+
{
87+
"frame.border": "#884444",
88+
"selected-option": "bold underline",
89+
}
90+
)
91+
result = prompt_choice(
92+
message="Please select a dish:",
93+
options=[
94+
("pizza", "Pizza with mushrooms"),
95+
("salad", "Salad with tomatoes"),
96+
("sushi", "Sushi"),
97+
],
98+
style=style,
99+
show_frame=~is_done,
100+
)
101+
print(result)
102+
103+

0 commit comments

Comments
 (0)