PyInquirer Documentation

repository·master·Indexed 24 days ago

https://github.com/citguru/pyinquirer

A collection of interactive command line user interfaces for Python, providing a feature-set similar to Inquirer.js. It includes various prompt types such as input, confirm, list, rawlist, expand, checkbox, password, and editor for creating embeddable and beautiful CLI interactions.

Tokens
1.3K
Snippets
2
Records
7
Agent score
34%

What's inside PyInquirer

  1. Quickstart with prompt()

    master

    Using PyInquirer follows a two-step pattern similar to Inquirer.js:

    1. Define a list of questions.
    2. Pass them to the prompt function to receive a list of answers.

    Use print_json from PyInquirer to easily inspect the resulting answers dictionary.

    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
  2. Install PyInquirer

    master

    Install the PyInquirer package using pip.

    If you encounter prompt_toolkit errors, it is likely due to an incompatible version. You can resolve this by installing the specific version 1.0.14.

    pip install PyInquirer
    
    # If you encounter prompt_toolkit errors:
    pip install prompt_toolkit==1.0.14
  3. Question Types: Confirm, Input, and Password

    master

    Confirm

    {type: 'confirm'} Asks a yes/no question.

    • Properties: type, name, message [, default]
    • Note: default should be a boolean.

    Input

    {type: 'input'} Standard text input.

    • Properties: type, name, message [, default, filter, validate]

    Password

    {type: 'password'} Text input that masks characters.

    • Properties: type, name, message [, default, filter, validate]
  4. Question Types: List and Raw List

    master

    List

    {type: 'list'} Used for selecting a single option from a list.

    • Properties: type, name, message, choices [, default, filter]
    • Note: default must be the choice index in the array or a choice value.

    Raw List

    {type: 'rawlist'} Similar to list, but displays the raw values.

    • Properties: type, name, message, choices [, default, filter]
    • Note: default must be the choice index in the array.
  5. Question Types: Expand and Checkbox

    master

    Expand

    {type: 'expand'} Allows selecting an option by typing a single character.

    • Properties: type, name, message, choices [, default]
    • Note: default must be the choice index. If not provided, the help property is used as the default.
    • Choices: Each choice object must include a key property (a single lowercased character).

    Checkbox

    {type: 'checkbox'} Allows selecting multiple options.

    • Properties: type, name, message, choices [, filter, validate]
    • Default Selection: Set {'checked': True} within a choice object to select it by default.
    • Disabling Choices: Use the disabled property. It can be a truthy value (defaults to "Disabled") or a synchronous function that receives the current answers and returns a boolean or a string.
  6. Question Types: Editor

    master

    {type: 'editor'} Opens the user's preferred editor to allow for multi-line or complex text input.

    Editor Behavior:

    • It uses the VISUAL or EDITOR environment variables. If neither is set, it defaults to notepad (Windows) or vim (Linux/Mac).
    • If the editor is closed without changes, None is returned.

    Properties:

    • type, name, message, default, filter, validate, eargs

    eargs (Editor Arguments):

    • editor: Path to the executable (overrides auto-detection).
    • ext: File extension for the temporary file (e.g., .py, defaults to .txt).
    • save: Boolean determining whether to save the file.
    • filename: Path of the file to edit.
    • env: Dictionary of environment variables to pass to the editor.
  7. Configure Question Properties

    master

    Every question is a dictionary. The following properties can be used to control behavior:

    PropertyTypeDescription
    typeStringPrompt type (e.g., input, confirm, list, rawlist, expand, checkbox, password, editor). Defaults to input.
    nameStringKey used to store the answer. If it contains periods, it defines a path in the answers hash.
    messageString/FunctionThe question text. If a function, it receives the current answers as the first argument.
    defaultString/Number/Array/FunctionDefault value. If a function, it receives the current answers as the first argument.
    choicesArray/FunctionArray of choices or a function returning them. If a function, it receives the current answers. Choices can be strings or objects with name, value, and short properties.
    validateFunctionReceives user input; returns True if valid, or a String error message if invalid.
    filterFunctionReceives user input and returns a modified value to be stored in the answers hash.
    whenFunction/BooleanDetermines if the question should be asked based on current answers.
    pageSizeNumberNumber of lines to render for list-based prompts.