1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
.. _quickstart:
**********
Quickstart
**********
Questionary supports two different concepts:
- creating a **single question** for the user
.. code-block:: python3
questionary.password("What's your secret?").ask()
- creating a **form with multiple questions** asked one after another
.. code-block:: python3
answers = questionary.form(
first = questionary.confirm("Would you like the next question?", default=True),
second = questionary.select("Select item", choices=["item1", "item2", "item3"])
).ask()
Asking a Single Question
========================
Questionary ships with a lot of different :ref:`question_types` to provide
the right prompt for the right question. All of them work in the same way though.
Firstly, you create a question:
.. code:: python3
import questionary
question = questionary.text("What's your first name")
and secondly, you need to prompt the user to answer it:
.. code:: python3
answer = question.ask()
Since our question is a ``text`` prompt, ``answer`` will
contain the text the user typed after they submitted it.
You can concatenate creating and asking the question in a single
line if you like, e.g.
.. code:: python3
import questionary
answer = questionary.text("What's your first name").ask()
.. note::
There are a lot more question types apart from ``text``.
For a description of the different question types, head
over to the :ref:`question_types`.
Asking Multiple Questions
=========================
You can use the :meth:`~questionary.form` function to ask a collection
of :class:`Questions <questionary.Question>`. The questions will be asked in
the order they are passed to `:meth:`~questionary.form``.
.. code:: python3
import questionary
answers = questionary.form(
first = questionary.confirm("Would you like the next question?", default=True),
second = questionary.select("Select item", choices=["item1", "item2", "item3"])
).ask()
print(answers)
The printed output will have the following format:
.. code-block:: python3
{'first': True, 'second': 'item2'}
The :meth:`~questionary.prompt` function also allows you to ask a
collection of questions, however instead of taking :class:`~questionary.Question`
instances, it takes a dictionary:
.. code:: python3
import questionary
questions = [
{
"type": "confirm",
"name": "first",
"message": "Would you like the next question?",
"default": True,
},
{
"type": "select",
"name": "second",
"message": "Select item",
"choices": ["item1", "item2", "item3"],
},
]
questionary.prompt(questions)
The format of the returned answers is the same as the one for
:meth:`~questionary.form`. You can find more details on the configuration
dictionaries in :ref:`question_dictionaries`.
|