PyInquirer
A Python module for common interactive command line user interfaces
Install / Use
/learn @CITGuru/PyInquirerREADME
PythonInquirer
.. image:: https://opencollective.com/pyinquirer/backers/badge.svg :alt: Backers on Open Collective :target: #backers
.. image:: https://opencollective.com/pyinquirer/sponsors/badge.svg :alt: Sponsors on Open Collective :target: #sponsors
.. image:: https://travis-ci.org/expobrain/PyInquirer.svg?branch=master :target: https://travis-ci.org/expobrain/PyInquirer
Update: We are looking for a successor/maintainance. Here: https://github.com/CITGuru/PyInquirer/issues/159 Github is starting to invite developers in from the GitHub Sponsors waitlist!
Your recommendations matter, so be sure to nominate me and other contributors <https://github.com/CITGuru/PyInquirer/graphs/contributors>__ of this project you'd like to see on GitHub Sponsors next by using the form in this link: http://github.co/2IHdeOw
PyInquirer is a collection of common interactive command line user interfaces.
PyInquirer 1.0.3 Bugfix Update
PyInquirer 1.0.3 <https://github.com/CITGuru/PyInquirer/releases/tag/1.0.3>
Table of Contents
-
Documentation <#documentation>__Installation <#installation>__Examples <#examples>__Quickstart <#quickstart>__Question Types <#types>__Question Properties <#properties>__User Interfaces and Styles <#styles>__
-
Windows Platform <#windows>__ -
Support <#support>__ -
Contribution <#contribution>__ -
Acknowledgments <#acknowledgements>__ -
License <#license>__
Goal and Philosophy
PyInquirer strives to be an easily embeddable and beautiful
command line interface for Python <https://python.org/>__.
PyInquirer wants to make it easy for existing Inquirer.js users
to write immersive command line applications in Python. We are convinced
that its feature-set is the most complete for building immersive CLI
applications. We also hope that PyInquirer proves itself useful
to Python users.
PyInquirer should ease the process of - providing error feedback - asking questions - parsing input - validating answers
- managing *hierarchical prompts
Note: PyInquirer provides the user interface and the inquiry session flow. >
Documentation
Installation
Like most Python packages PyInquirer is available on `PyPi <https://pypi.org/project/PyInquirer/>`__.
Simply use pip to install the PyInquirer package
.. code:: shell
pip install PyInquirer
In case you encounter any prompt\_toolkit error, that means you've the
wrong prompt\_toolkit version.
You can correct that by doing
.. code:: shell
pip install prompt_toolkit==1.0.14
or download the wheel file from here:
.. code:: shell
https://pypi.org/project/prompt_toolkit/1.0.14/#files
Quickstart
~~~~~~~~~~
Like Inquirer.js, using inquirer is structured into two simple steps:
- you define a **list of questions** and hand them to **prompt**
- prompt returns a **list of answers**
.. code:: python
from __future__ import print_function, unicode_literals
from PyInquirer import prompt, print_json
questions = [
{
'type': 'input',
'name': 'first_name',
'message': 'What\'s your first name',
}
]
answers = prompt(questions)
print_json(answers) # use the answers as input for your app
A good starting point from here is probably the examples section.
Examples
~~~~~~~~
Most of the examples intend to demonstrate a single question type or
feature:
- `editor.py <./examples/editor.py>`_
- `expand.py <./examples/expand.py>`_
- `list.py <./examples/list.py>`_
- `password.py <./examples/password.py>`_
- `when.py <./examples/when.py>`_
- `checkbox.py <./examples/checkbox.py>`_
- `confirm.py <./examples/confirm.py>`_
- `hierarchical.py <./examples/hierarchical.py>`_
- `pizza.py <./examples/pizza.py>`_ - demonstrate using different question types
- `input.py <./examples/input.py>`_
- `rawlist.py <./examples/rawlist.py>`_
If you want to launch examples with the code from repository instead of
installing a package you need to execute ``pip install -e .`` within project
directory.
Question Types
questions is a list of questions. Each question has a type.
List - {type: 'list'}
^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message, choices\ [, default,
filter] properties. (Note that default must be the choice index
in the array or a choice value)
|List prompt| s ---
Raw List - {type: 'rawlist'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message, choices\ [, default,
filter] properties. (Note that default must the choice index in
the array)
.. figure:: https://raw.githubusercontent.com/citguru/PyInquirer/master/docs/images/raw-list.png :alt: Raw list prompt
Raw list prompt
Expand - {type: 'expand'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message, choices\ [, default]
properties. (Note that default must be the choice index in the
array. If default key not provided, then help will be used as
default choice)
Note that the choices object will take an extra parameter called
key for the expand prompt. This parameter must be a single
(lowercased) character. The h option is added by the prompt and
shouldn't be defined by the user.
See examples/expand.py for a running example.
|Expand prompt closed| |Expand prompt expanded|
Checkbox - {type: 'checkbox'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message, choices\ [, filter,
validate] properties.
Choices marked as {'checked': True} will be checked by default.
Choices whose property disabled is truthy will be unselectable. If
disabled is a string, then the string will be outputted next to the
disabled choice, otherwise it'll default to "Disabled". The
disabled property can also be a synchronous function receiving the
current answers as argument and returning a boolean or a string.
The pointer_index kwarg can be used to specify initial pointer position.
.. figure:: https://raw.githubusercontent.com/citguru/PyInquirer/master/docs/images/checkbox-prompt.png :alt: Checkbox prompt
Checkbox prompt
Confirm - {type: 'confirm'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message\ [, default] properties.
default is expected to be a boolean if used.
.. figure:: https://raw.githubusercontent.com/citguru/PyInquirer/master/docs/images/confirm-prompt.png :alt: Confirm prompt
Confirm prompt
Input - {type: 'input'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message\ [, default, filter,
validate] properties.
.. figure:: https://raw.githubusercontent.com/citguru/PyInquirer/master/docs/images/input-prompt.png :alt: Input prompt
Input prompt
Password - {type: 'password'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message\ [, default, filter,
validate] properties.
.. figure:: https://raw.githubusercontent.com/citguru/PyInquirer/master/docs/images/password-prompt.png :alt: Password prompt
Password prompt
Editor - {type: 'editor'}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take type, name, message\ [, default, filter,
validate, eargs] properties
Editor Arguments - eargs
Opens an empty or edits the default text in the defined editor. If an editor is given
(should be the full path to the executable but the regular operating
system search path is used for finding the executable) it overrides
the detected editor. Optionally, some environment variables can be
used. If the editor is closed without changes, ``None`` is returned. In
case a file is edited directly the return value is always ``None`` and
``save`` and ``ext`` are ignored.
Takes:
- editor: accepts ``default`` to get the default platform editor. But
you can also provide the path to an editor e.g ``vi``.
- ext: the extension to tell the editor about. This defaults to `.txt`
but changing this might change syntax highlighting e.g ".py"
- save: accepts ``True`` or ``False`` to determine to save a file.
- filename: accepts the path of a file you'd like to edit.
- env: accepts any given environment variables to pass to the editor
Launches an instance of the users preferred editor on a temporary file.
Once the user exits their editor, the contents of the temporary file are
read in as the result. The editor to use is determined by reading the
:math:``VISUAL or ``\ EDITOR environment variables. If neither of those
are present, notepad (on Windows) or vim (Linux or Mac) is used.
Question Properties
~~~~~~~~~~~~~~~~~~~
A question is a dictionary containing question related values:
- ``type``: (String) Type of the prompt. Defaults: input - Possible values:
input, confirm, list, rawlist, expand, checkbox, password, editor
- ``name``: (String) The name to use when storing the answer in the answers
hash. If the name contains periods, it will define a path in the
answers hash.
- ``message``: (String\|Function) The question to print. If defined as a
function, the first parameter will be the current inquirer session
answers.
- ``default``: (String\|Number\|Array\|Function) Default value(s) to use if
nothing is entered, or a function that returns the default value(s).
If defined as a function, the first parameter will be the current
inquirer session answers.
- ``choices``: (Array\|Function) Choices array or a function returning a
choices array. If defined as a function, the first parameter will be
the current inquirer session answers. Array values can be simple
strings, or objects containing a name (to display in list),
