FilepathPrompt - kazhala/InquirerPy GitHub Wiki

This page is deprecated, documentation moved to: https://inquirerpy.readthedocs.io/

An input prompt with pre-built filepath completion.

class FilePathPrompt(InputPrompt):
    def __init__(
        self,
        message: Union[str, Callable[[SessionResult], str]],
        style: InquirerPyStyle = None,
        vi_mode: bool = False,
        default: Union[str, Callable[[SessionResult], str]] = "",
        qmark: str = "?",
        multicolumn_complete: bool = False,
        validate: Union[Callable[[str], bool], Validator] = None,
        invalid_message: str = "Invalid input",
        only_directories: bool = False,
        only_files: bool = False,
        transformer: Callable[[str], Any] = None,
        filter: Callable[[str], Any] = None,
        **kwargs,
    ) -> None:

Example

demo

Classic Syntax (PyInquirer)
from pathlib import Path
from InquirerPy import prompt
from InquirerPy.validator import PathValidator

questions = [
    {
        "type": "filepath",
        "message": "Enter file to upload:",
        "name": "location",
        "default": str(Path.cwd()),
        "validate": PathValidator(is_file=True, message="Input is not a file"),
        "only_files": True,
    },
    {
        "type": "filepath",
        "message": "Enter path to download:",
        "validate": PathValidator(is_dir=True, message="Input is not a directory"),
        "name": "destination",
        "only_directories": True,
    },
]

result = prompt(questions)
Alternate Syntax
from pathlib import Path
from InquirerPy import inquirer
from InquirerPy.validator import PathValidator

src_path = inquirer.filepath(
    message="Enter file to upload:",
    default=str(Path.cwd()),
    validate=PathValidator(is_file=True, message="Input is not a file"),
    only_files=True,
).execute()
dest_path = inquirer.filepath(
    message="Enter path to download:",
    validate=PathValidator(is_dir=True, message="Input is not a directory"),
    only_directories=True,
).execute()

Parameters

message: Union[Callable[[SessionResult], str], str]

REQUIRED

The question message to display/ask the user.

When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer), put a dummy parameter (_) in your function.

from InquirerPy import inquirer

def get_message(_) -> str:
    message = "Name:"
    # logic ...
    return message

result = inquirer.filepath(message=get_message)

style: InquirerPy

An InquirerPyStyle instance. Use get_style to retrieve an instance, reference Style documentation for more information.

If you are suing classic syntax (i.e. style), there's no need to provide this value since prompt already sets the style, unless you would like to apply different style for different question.

vi_mode: bool

Enable vim keybindings for the filepath prompt. Its exactly the same as if you enable vi_mode in bash/readline. Checkout Keybindings documentation for more information.

If you are suing classic syntax (i.e. prompt), there's no need to provide this value since prompt already sets sets this value, unless you would like to apply vi_mode to specific questions.

default: Union[Callable[[SessionResult], str], str]

Set the default value of the filepath prompt.

When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer), put a dummy parameter (_) in your function.

qmark: str

The question mark symbol to display in front of the question.

multicolumn_complete: bool

Change the auto-completion UI to a multi column display, checkout the gif demo in InputPrompt page.

validate: Union[Callable[[str], bool], Validator]

Provide the validator for this question. Checkout Validator documentation for full details.

invalid_message: str

Configure the error message to display to the user when input does not meet compliance.

If the validate parameter is a Validator instance, then skip this parameter.

only_files: bool

Only populate files in the auto-completion.

only_directories: bool

Only populate directories in the auto-completion.

transformer: Callable[[str], Any]

A callable to transform the result. This is visual effect only, meaning it doesn't affect the returned result, it only changes the result displayed in the prompt.

Note: filter function won't affect the answer passed into transformer, transformer actually run before the filter function.

result = inquirer.filepath(
    message="File to upload:", default="sample.txt", only_files=True
)  # UI -> ? Name: sample.txt
result = inquirer.filepath(
    message="File to upload:",
    default="sample.txt",
    only_files=True
    transformer=lambda result: result.replace(".txt", ""),
)  # UI -> ? Name: sample

filter: Callable[[str], Any]

A callable to filter the result. Different than the transformer, this affects the actual returned result but doesn't affect the visible prompt content.

result = inquirer.filepath(
    message="File to upload:", default="sample.txt", only_files=True
)  # result = "sample.txt"
result = inquirer.filepath(
    message="File to upload:",
    default="sample.txt",
    only_files=True
    filter=lambda result: result.replace(".txt", ""),
)  # result = "sample.txt"
⚠️ **GitHub.com Fallback** ⚠️