number

A prompt for entering numbers. All non number input will be disabled for this prompt.

Example

demo

Classic Syntax (PyInquirer)
from InquirerPy import prompt
from InquirerPy.validator import EmptyInputValidator


def main() -> None:
    questions = [
        {
            "type": "number",
            "message": "Enter integer:",
            "min_allowed": -2,
            "max_allowed": 10,
            "validate": EmptyInputValidator(),
        },
        {
            "type": "number",
            "message": "Enter float:",
            "float_allowed": True,
            "validate": EmptyInputValidator(),
        },
    ]

    result = prompt(questions)


if __name__ == "__main__":
    main()
Alternate Syntax
from InquirerPy import inquirer
from InquirerPy.validator import EmptyInputValidator


def main() -> None:
    integer_val = inquirer.number(
        message="Enter integer:",
        min_allowed=-2,
        max_allowed=10,
        validate=EmptyInputValidator(),
    ).execute()
    float_val = inquirer.number(
        message="Enter float:",
        float_allowed=True,
        validate=EmptyInputValidator(),
    ).execute()


if __name__ == "__main__":
    main()

Keybindings

See also

keybindings

{
    "answer": [{"key": "enter"}],   # answer the prompt
    "interrupt": [{"key": "c-c"}],  # raise KeyboardInterrupt
    "skip": [{"key": "c-z"}],   # skip the prompt
}

The following dictionary contains the additional keybindings created by this prompt.

{
  "down": [
    {"key": "down"},  # decrement the number
    {"key": "c-n"},
  ],
  "up": [
    {"key": "up"},  # increment the number
    {"key": "c-p"},
  ],
  "left": [
    {"key": "left"},  # move cursor to the left
    {"key": "c-b"},
  ],
  "right": [
    {"key": "right"},   # move cursor to the right
    {"key": "c-f"},
  ],
  "focus": [
    {"key": "c-i"},   # focus the alternate input buffer when float_allowed=True
    {"key": "s-tab"},
  ],
  "negative_toggle": [{"key": "-"}], # toggle result negativity
  "dot": [{"key": "."}],  # focus the integral buffer when float_allowed=True to enter decimal points
}

When vi_mode is True, the direction navigation key will be changed.

Tip

Additionally, the input buffer can also enter normal mode by pressing esc when vi_mode is True.

{
  "down": [
    {"key": "down"},
    {"key": "j"},
  ],
  "up": [
    {"key": "up"},
    {"key": "k"},
  ],
  "left": [
    {"key": "left"},
    {"key": "h"},
  ],
  "right": [
    {"key": "right"},
    {"key": "l"},
  ],
}

Default Value

The default value of the input buffer is set to 0 to help differentiate with InputPrompt. You could disable this value and have an empty input buffer by setting the parameter default=None.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "default": None,
  }
]

result = prompt(questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.number(message="Number:", default=None).execute()

Max and Min

You can set the maximum allowed value as well as the minimum allowed value for the prompt via max_allowed and min_allowed.

Hint

When the input value goes above/below the max/min value, the input value will automatically reset to the configured max/min value.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "max_allowed": 10,
    "min_allowed": -100
  }
]

result = prompt(questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", max_allowed=10, min_allowed=-100,
).execute()

Decimal Input

Tip

Once you enable decimal input, the prompt will have a second input buffer. You can keep navigating left/right to enter the other input buffer or you can use the tab/shit-tab to focus the other buffer.

You can enable decimal input by setting the argument float_allowed to True.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "float_allowed": True,
  }
]

result = prompt(questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", float_allowed=True,
).execute()

Replace Mode

By default, all input buffer has the exact same behavior as terminal input behavior. There is an optional replace mode which you could enable for a better experience when working with decimal points input. You can enable it via parameter replace_mode=True.

Warning

Replace mode introduce some slight inconsistency with the terminal input behavior that we are used to.

Classic Syntax (PyInquirer)
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "replace_mode": True,
  }
]

result = prompt(questions)
Alternate Syntax
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", replace_mode=True,
).execute()

The following gif demonstrate the different behavior when we are trying to input number “123.102”. The first prompt is replace_mode=False and the second prompt is replace_mode=True.

demo

Reference

class InquirerPy.prompts.number.NumberPrompt(message, style=None, vi_mode=False, default=0, float_allowed=False, max_allowed=None, min_allowed=None, decimal_symbol='. ', replace_mode=False, qmark='?', amark='?', instruction='', long_instruction='', validate=None, invalid_message='Invalid input', transformer=None, filter=None, keybindings=None, wrap_lines=True, raise_keyboard_interrupt=True, mandatory=True, mandatory_message='Mandatory prompt', session_result=None)[source]

Create a input prompts that only takes number as input.

A wrapper class around Application.

Parameters
  • message (Union[str, Callable[[InquirerPySessionResult], str]]) – The question to ask the user. Refer to message documentation for more details.

  • style (Optional[InquirerPy.utils.InquirerPyStyle]) – An InquirerPyStyle instance. Refer to Style documentation for more details.

  • vi_mode (bool) – Use vim keybinding for the prompt. Refer to keybindings documentation for more details.

  • default (Union[Any, Callable[[InquirerPySessionResult], Any]]) – Set the default value of the prompt. You can enter either the floating value or integer value as the default. Refer to default documentation for more details.

  • float_allowed (bool) – Allow decimal input. This will change the prompt to have 2 input buffer, one for the whole value and one for the integral value.

  • min_allowed (Optional[Union[int, float]]) – Set the minimum value of the prompt. When the input value goes below this value, it will automatically reset to this value.

  • max_allowed (Optional[Union[int, float]]) – Set the maximum value of the prompt. When the inptu value goes above this value, it will automatically reset to this value.

  • qmark (str) – Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.

  • amark (str) – Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.

  • decimal_symbol (str) – Decimal point symbol. Custom symbol to display as the decimal point.

  • replace_mode (bool) – Start each input buffer in replace mode if default value is 0. When typing, it will replace the 0 with the new value. The replace mode will be disabled once the value is changed.

  • instruction (str) – Short instruction to display next to the question.

  • long_instruction (str) – Long instructions to display at the bottom of the prompt.

  • validate (Optional[Union[Callable[[Any], bool], Validator]]) – Add validation to user input. Refer to Validator documentation for more details.

  • invalid_message (str) – Error message to display when user input is invalid. Refer to Validator documentation for more details.

  • invalid_message – Error message to display when user input is invalid. Refer to Validator documentation for more details.

  • transformer (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the value that gets printed to the terminal. Different than filter parameter, this is only visual effect and won’t affect the actual value returned by execute(). Refer to transformer documentation for more details.

  • filter (Optional[Callable[[str], Any]]) – A function which performs additional transformation on the result. This affects the actual value returned by execute(). Refer to filter documentation for more details.

  • keybindings (Optional[Dict[str, List[Dict[str, Union[str, FilterOrBool, List[str]]]]]]) – Customise the builtin keybindings. Refer to keybindings for more details.

  • wrap_lines (bool) – Soft wrap question lines when question exceeds the terminal width.

  • raise_keyboard_interrupt (bool) – Raise the KeyboardInterrupt exception when ctrl-c is pressed. If false, the result will be None and the question is skiped.

  • mandatory (bool) – Indicate if the prompt is mandatory. If True, then the question cannot be skipped.

  • mandatory_message (str) – Error message to show when user attempts to skip mandatory prompt.

  • session_result (Optional[Dict[Union[str, int], Optional[Union[str, bool, List[Any]]]]]) – Used internally for Classic Syntax (PyInquirer).

Return type

None

Examples

>>> from InquirerPy import inquirer
>>> result = inquirer.number(message="Enter number:").execute()
>>> print(result)
0