Skip to main content

FileInput

File upload input widget for handling file uploads.

Examples

Basic Usage

This example runs a form with a single page containing the widget

Example Code

from abstra.forms import FileInput, run

# Create a page with the widget
example_page = [
FileInput(label="File Input", key="my-file_input"),
]

# Run the form
result = run([example_page])

# Print the result
print(result)

Using the output of a FileInput

This example runs a form with two pages. The first page requests a file and a filename from the user. The second page saves the file in the persistent directory with the given filename and sends a task with the file path.

Example Code

import os

from abstra.common import get_persistent_dir
from abstra.forms import FileInput, TextInput, run
from abstra.tasks import send_task

# Create a page to input a file and choose its filename
example_page = [
TextInput(label="Choose a filename", key="filename"),
FileInput(label="File input", key="my-file"),
]


# Save the inputted file in the persistent dir and send its path
def save_inputed_file(state):
# Get information from the previous page
inputted_file = state.get("my-file")
custom_filename = state.get("filename")

# Compute the inputted file's extension
file_name = inputted_file.name
_, file_extension = os.path.splitext(file_name)

# Save in persistent dir
pdir_path = get_persistent_dir()
file_path = f"{pdir_path}/{custom_filename}{file_extension}"
with open(file_path, "wb") as f:
file_object = inputted_file.file
f.write(file_object.read())

# Send task with the persistent file path
send_task("file", {"file_path": file_path})


# Run the form
result = run([example_page, save_inputed_file])

Parameters

NameDescriptionTypeDefault
label (required)Text label displayed above the input.strNone
keyIdentifier for the widget, defaults to label if not provided.strNone
requiredWhether a file must be uploaded before proceeding.boolTrue
hintHelp text displayed below the input.strNone
full_widthWhether the input should take up the full width of its container.boolFalse
disabledWhether the input is non-interactive.boolFalse
max_file_sizeMaximum file size in bytes.intNone
multipleWhether multiple files can be uploaded.boolFalse
minMinimum number of files required when multiple=True.intNone
maxMaximum number of files allowed when multiple=True.intNone
accepted_formatsList of accepted file extensions.List[str]None
errorsPre-defined validation error messages to display.Union[List[str], str]None

State Value

Union[FileResponse, List[FileResponse], None]: The uploaded file(s). FileResponse is a dictionary-like object: {'name': str, 'path': pathlib.Path}. If multiple is True, will be a list, otherwise a single FileResponse object.

info

This is the type of the value that this widget holds. It will show up when accessing widget's state item.

This also defines which value type can be used on initial_state