|
| 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.choice` function to ask for a choice |
| 9 | +from a list of options: |
| 10 | + |
| 11 | +.. code:: python |
| 12 | +
|
| 13 | + from prompt_toolkit.shortcuts import choice |
| 14 | +
|
| 15 | + result = 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 | + print(f"You have chosen: {result}") |
| 25 | +
|
| 26 | +.. image:: ../images/choice-input.png |
| 27 | + |
| 28 | + |
| 29 | +Coloring the options |
| 30 | +-------------------- |
| 31 | + |
| 32 | +It is possible to customize the colors and styles. The ``message`` parameter |
| 33 | +takes any :ref:`formatted text <formatted_text>`, and the labels (2nd argument |
| 34 | +from the options) can be :ref:`formatted text <formatted_text>` as well. |
| 35 | +Further, we can pass a :class:`~prompt_toolkit.styles.Style` instance using the |
| 36 | +:meth:`~prompt_toolkit.styles.Style.from_dict` function: |
| 37 | + |
| 38 | +.. code:: python |
| 39 | +
|
| 40 | + from prompt_toolkit.formatted_text import HTML |
| 41 | + from prompt_toolkit.shortcuts import choice |
| 42 | + from prompt_toolkit.styles import Style |
| 43 | +
|
| 44 | + style = Style.from_dict( |
| 45 | + { |
| 46 | + "input-selection": "fg:#ff0000", |
| 47 | + "number": "fg:#884444 bold", |
| 48 | + "selected-option": "underline", |
| 49 | + } |
| 50 | + ) |
| 51 | +
|
| 52 | + result = choice( |
| 53 | + message=HTML("<u>Please select a dish</u>:"), |
| 54 | + options=[ |
| 55 | + ("pizza", "Pizza with mushrooms"), |
| 56 | + ( |
| 57 | + "salad", |
| 58 | + HTML("<ansigreen>Salad</ansigreen> with <ansired>tomatoes</ansired>"), |
| 59 | + ), |
| 60 | + ("sushi", "Sushi"), |
| 61 | + ], |
| 62 | + style=style, |
| 63 | + ) |
| 64 | + print(f"You have chosen: {result}") |
| 65 | +
|
| 66 | +.. image:: ../images/colored-choice.png |
| 67 | + |
| 68 | + |
| 69 | +Adding a frame |
| 70 | +-------------- |
| 71 | + |
| 72 | +The :func:`~prompt_toolkit.shortcuts.choice` function takes a |
| 73 | +``show_frame`` argument. When ``True``, the input is displayed within a frame. |
| 74 | +It is also possible to pass a :ref:`filter <filters>`, like ``~is_done``, so |
| 75 | +that the frame is only displayed when asking for input, but hidden once the |
| 76 | +input is accepted. |
| 77 | + |
| 78 | +.. code:: python |
| 79 | +
|
| 80 | + from prompt_toolkit.shortcuts import choice |
| 81 | + from prompt_toolkit.filters import is_done |
| 82 | + from prompt_toolkit.styles import Style |
| 83 | +
|
| 84 | + style = Style.from_dict( |
| 85 | + { |
| 86 | + "frame.border": "#884444", |
| 87 | + "selected-option": "bold", |
| 88 | + } |
| 89 | + ) |
| 90 | + result = choice( |
| 91 | + message="Please select a dish:", |
| 92 | + options=[ |
| 93 | + ("pizza", "Pizza with mushrooms"), |
| 94 | + ("salad", "Salad with tomatoes"), |
| 95 | + ("sushi", "Sushi"), |
| 96 | + ], |
| 97 | + style=style, |
| 98 | + show_frame=~is_done, |
| 99 | + ) |
| 100 | + print(f"You have chosen: {result}") |
| 101 | +
|
| 102 | +.. image:: ../images/choice-with-frame.png |
| 103 | + |
| 104 | + |
| 105 | +Adding a bottom toolbar |
| 106 | +----------------------- |
| 107 | + |
| 108 | +Adding a bottom toolbar can be done by passing a ``bottom_toolbar`` argument to |
| 109 | +:func:`~prompt_toolkit.shortcuts.choice`. This argument can be plain text, |
| 110 | +:ref:`formatted text <formatted_text>` or a callable that returns plain or |
| 111 | +formatted text. |
| 112 | + |
| 113 | + |
| 114 | +.. code:: python |
| 115 | +
|
| 116 | + from prompt_toolkit.filters import is_done |
| 117 | + from prompt_toolkit.formatted_text import HTML |
| 118 | + from prompt_toolkit.shortcuts import choice |
| 119 | + from prompt_toolkit.styles import Style |
| 120 | +
|
| 121 | + style = Style.from_dict( |
| 122 | + { |
| 123 | + "frame.border": "#ff4444", |
| 124 | + "selected-option": "bold", |
| 125 | + # ('noreverse' because the default toolbar style uses 'reverse') |
| 126 | + "bottom-toolbar": "#ffffff bg:#333333 noreverse", |
| 127 | + } |
| 128 | + ) |
| 129 | +
|
| 130 | + result = choice( |
| 131 | + message=HTML("<u>Please select a dish</u>:"), |
| 132 | + options=[ |
| 133 | + ("pizza", "Pizza with mushrooms"), |
| 134 | + ("salad", "Salad with tomatoes"), |
| 135 | + ("sushi", "Sushi"), |
| 136 | + ], |
| 137 | + style=style, |
| 138 | + bottom_toolbar=HTML( |
| 139 | + " Press <b>[Up]</b>/<b>[Down]</b> to select, <b>[Enter]</b> to accept." |
| 140 | + ), |
| 141 | + show_frame=~is_done, |
| 142 | + ) |
| 143 | + print(f"You have chosen: {result}") |
| 144 | +
|
| 145 | +.. image:: ../images/choice-with-frame-and-bottom-toolbar.png |
0 commit comments