Debian Codegraph @

docs/pages/quickstart.rst

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