Skip to main content

questionary

version license Build Status Coverage Status Supported Python Versions

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.

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 the following list, allowing the user to choose an option:

Documentation

  1. Different question types

    The available question types are text, password, confirm, select, rawselect and checkbox.

  2. Dict style question formulation

    Alterative style to create questions using a configuration dictionary.

  3. Styling your prompts

    Customize how your questions look.

1. 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()

2. Dict style question formulation

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.

3. Styling your prompts

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
    ('selected', 'fg:#cc5454'),       # style for a selected item of a checkbox
    ('separator', 'fg:#cc5454'),      # separator in lists
    ('instruction', '')               # user instructions for select, rawselect, checkbox
])

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()

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.

Changelog

1.0.0

Initial public release of the library

Release Date: 14.12.18

  • 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 2018 Tom Bocklisch. Copy of the license.

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.0.0.tar.gz (17.9 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.0.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: questionary-1.0.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for questionary-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3f81ef0ca0c9ebe17806e8382fd3caa377087a969b7a7ee033862e2510a3de02
MD5 03ab45fbd9acd1e2d8c4d06c2b6676b2
BLAKE2b-256 93631dc78ebeb16f116ad076c149c7f475095e33f3caa2e72d409506fcd0e301

See more details on using hashes here.

File details

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

File metadata

  • Download URL: questionary-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for questionary-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db044343249813afe9620c2f2dea853ebc28c5cea8042477a687df511910f24e
MD5 b9b4991d2ba7803b30a2907f4d1b6cb3
BLAKE2b-256 93b23ed1d3bf1ca5615025b4fa27b6cc831d5000d7882785fcaf4a59301f2833

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 Sentry Error logging StatusPage Status page