Skip to main content

questionary

version license Build Status Coverage Status Supported Python Versions FOSSA Status

Python library to build pretty command line user prompts ✨

You need input from a user, e.g. how an output file should be named or if he really wants to execute that dangerous operation? This library will help you make the input prompts easy to read and answer for the user.

Used and Supported by:

Quickstart

To install questionary, simply use pipenv (or pip, of course):

$ pipenv install questionary
✨🎂✨

Satisfaction guaranteed. Let's create a first question:

import questionary

questionary.select(
    "What do you want to do?",
    choices=[
        'Order a pizza',
        'Make a reservation',
        'Ask for opening hours'
    ]).ask()  # returns value of selection

This will create a list selection on the command line for the user to select from. But there are even more question types:

Documentation

Different question types

text

A free text input for the user.

questionary.text("What's your first name").ask()
password

A free text input for the user where the input is not shown but replaced with ***.

questionary.password("What's your secret?").ask()
confirm

A yes or no question. The user can either confirm or deny.

questionary.confirm("Are you amazed?").ask()
select

A list of items to select a choice from. The user can pick one option and confirm it.

questionary.select(
    "What do you want to do?",
    choices=[
        "Order a pizza",
        "Make a reservation",
        "Ask for opening hours"
    ]).ask()
rawselect

A list of items to select a choice from. The user can pick one option using shortcuts and confirm it.

questionary.rawselect(
    "What do you want to do?",
    choices=[
        "Order a pizza",
        "Make a reservation",
        "Ask for opening hours"
    ]).ask()
checkbox

A list of items to select multiple choices from. The user can pick none, one or multiple options and confirm the selection.

questionary.checkbox(
    'Select toppings',
    choices=[
        "foo",
        "bar",
        "bazz"
    ]).ask()
autocomplete

Text input with autocomplete help.

questionary.autocomplete(
    'Choose ant specie',
    choices=[
         'Camponotus pennsylvanicus',
         'Linepithema humile',
         'Eciton burchellii',
         "Atta colombica",
         'Polyergus lucidus',
         'Polyergus rufescens',
    ]).ask()

Additional Features

Skipping questions using conditions

Sometimes it is helpfull to e.g. provide a command line flag to your app to skip any prompts, to avoid the need for an if around any question you can pass that flag when you create the question:

DISABLED = True

response = questionary.confirm("Are you amazed?").skip_if(DISABLED, default=True).ask()

If the condition (in this case DISABLED) is True, the question will be skipped and the default value gets returned, otherwise the user will be prompted as usual and the default value will be ignored.

Alterative style to create questions using a configuration dictionary

Instead of creating questions using the python functions, you can also create them using a configuration dictionary.

questions = [
    {
        'type': 'text',
        'name': 'phone',
        'message': "What's your phone number",
    },
    {
        'type': 'confirm',
        'message': 'Do you want to continue?',
        'name': 'continue',
        'default': True,
    }
]

answers = prompt(questions)

The returned answers will be a dict containing the responses, e.g. {"phone": "0123123", "continue": False, ""}. The questions will be prompted one after another and prompt will return once all of them are answered.

Styling your prompts with your favorite colors

You can customize all the colors used for the prompts. Every part of the prompt has an identifier, which you can use to style it. Let's create our own custom style:

from prompt_toolkit.styles import Style

custom_style_fancy = Style([
    ('qmark', 'fg:#673ab7 bold'),       # token in front of the question
    ('question', 'bold'),               # question text
    ('answer', 'fg:#f44336 bold'),      # submitted answer text behind the question
    ('pointer', 'fg:#673ab7 bold'),     # pointer used in select and checkbox prompts
    ('highlighted', 'fg:#673ab7 bold'), # pointed-at choice in select and checkbox prompts
    ('selected', 'fg:#cc5454'),         # style for a selected item of a checkbox
    ('separator', 'fg:#cc5454'),        # separator in lists
    ('instruction', ''),                # user instructions for select, rawselect, checkbox
    ('text', ''),                       # plain text
    ('disabled', 'fg:#858585 italic')   # disabled choices for select and checkbox prompts
])

To use our custom style, we need to pass it to the question type:

questionary.text("What's your phone number", style=custom_style_fancy).ask()

It is also possible to use a list of token tuples as a Choice title. This example assumes there is a style token named bold in the custom style you are using:

Choice(
    title=[
        ('class:text', 'plain text '),
        ('class:bold', 'bold text')
    ]
)

As you can see it is possible to use custom style tokens for this purpose as well. Note that Choices with token tuple titles will not be styled by the selected or highlighted tokens. If not provided, the value of the Choice will be the text concatenated ('plain text bold text' in the above example).

How to Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. There is a Contributor Friendly tag for issues that should be ideal for people who are not very familiar with the codebase yet.
  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).
  3. Write a test which shows that the bug was fixed or that the feature works as expected.
  4. Send a pull request and bug the maintainer until it gets merged and published. 🙂

Contributors

questionary is written and maintained by Tom Bocklisch.

It is based on the great work of Oyetoke Toby as well as the work from Mark Fink.

Changelog

unreleased (master branch)
1.5.1 (22.01.2020)

Bug fix release.

  • Fixed .ask_async for questions on prompt_toolkit==2.*. Added tests for it.
1.5.0 (22.01.2020)

Feature release.

  • Added support for prompt_toolkit 3
  • All tests will be run against prompt_toolkit 2 and 3
  • Removed support for python 3.5 (prompt_toolkit 3 does not support that anymore)
1.4.0 (10.11.2019)

Feature release.

  • Added additional question type autocomplete
  • Allow pointer and highlight in select question type
1.3.0 (25.08.2019)

Feature release.

1.2.1 (19.08.2019)

Bug fix release.

  • Fixed compatibility with python 3.5.2 by removing Type annotation (this time for real)
1.2.0 (30.07.2019)

Feature release.

1.1.1 (21.04.2019)

Bug fix release.

  • Fixed compatibility with python 3.5.2 by removing Type annotation
1.1.0 (10.03.2019)

Feature release.

  • Added skip_if to questions to allow skipping questions using a flag
1.0.2 (23.01.2019)

Bug fix release.

  • Fixed odd behaviour if select is created without providing any choices instead, we will raise a ValueError now. (#6)
1.0.1 (12.01.2019)

Bug fix release, adding some convenience shortcuts.

  • Added shortcut keys j (move down^ the list) and k (move up) to the prompts select and checkbox (fixes #2)
  • Fixed unclosed file handle in setup.py
  • Fixed unecessary empty lines moving selections to far down (fixes #3)
1.0.0 (14.12.2018)

Initial public release of the library

  • Added python interface
  • Added dict style question creation
  • Improved the documentation
  • More tests and automatic travis test execution

License

Licensed under the MIT License. Copyright 2020 Tom Bocklisch. Copy of the license.

FOSSA Status

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

questionary-1.5.1.tar.gz (639.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

questionary-1.5.1-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file questionary-1.5.1.tar.gz.

File metadata

  • Download URL: questionary-1.5.1.tar.gz
  • Upload date:
  • Size: 639.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for questionary-1.5.1.tar.gz
Algorithm Hash digest
SHA256 f199d4a780183679725f10a209b68be27f07cfd2852d6d7ea9e4a31fb45fb0c0
MD5 fb969e87b0134cec58f5de38780532a0
BLAKE2b-256 ed08d0aee8ce665461b38d98ca4e3fa1fe6c6a0936f5d85c0df61d8605073140

See more details on using hashes here.

File details

Details for the file questionary-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: questionary-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for questionary-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5314bfb746321997a9dc457e5e0a533e577a661347f3a89c63277c6746e9a8b9
MD5 ce7087caf66b4c7edade8507d82b84eb
BLAKE2b-256 09031a1020649ab0cf91b289ba980c07d48bea54ece53a94530a6e2ae0bb5ebf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page