Skip to main content

File Upload

Enables file upload via file explorer.

Examples

Basic Example

The following example demonstrate some of the available functionality for read_file

from abstra.forms import read_file

file_response = read_file("Upload your .xlsx file", accepted_formats=[".xlsx"])
file = file_response.file # File object

Saving file to a directory on Files storage

This example shows how to save a file to a directory on Files

import os
import shutil

from abstra.forms import read_file

file_response = read_file("Upload your file")

destination_dir = "foo/bar/"
# Creates directory if it does not exist
os.makedirs(destination_dir, exist_ok=True)

# Copies file to destination directory
shutil.copy(file_response.file.name, destination_dir + file_response.name)

Renaming in File Upload

This example demonstrates how to upload, rename, and save a user file in a persistent directory.

from pathlib import Path

from abstra.common import get_persistent_dir
from abstra.forms import read_file

f = read_file("Upload your file.json")

# Get file extension
extension = Path(f.name).suffix

# if extension is not .json, raise an error
if extension != ".json":
raise ValueError("File must be a .json file")

# if the extension is the expected, saves the file to a persistent directory
get_persistent_dir().joinpath("keys.json").write_bytes(f.file.read())

Preserving Filename in File Upload

This example demonstrates how to read a user-uploaded file and save it to a specific persistent directory, preserving its original name.

from pathlib import Path

from abstra.common import get_persistent_dir
from abstra.forms import read_file

f = read_file("Upload your file")

# Get the original filename
original_filename = Path(f.name).name

# Saves the file to a persistent directory
get_persistent_dir().joinpath(original_filename).write_bytes(f.file.read())

Parameters

NameDescriptionType
labelThe label to display to the userstr
initial_valueThe initial value to display to the user. Defaults to "".str
multipleWhether the user will be allowed to upload multiple files. Defaults to False.bool
accepted_formatsThe specific file types that the input should accept. Defaults to [], accepting all file formats.list
minThe minimal amount of files that should be submitted. Only applied if multiple is True. Defaults to None.number
maxThe maximum amount of files that should be submitted. Only applied if multiple is True. Defaults to None.number
max_file_sizeMaximum size allowed to be transfered in total in MB.float
disabledwhether the input is disabled. Defaults to False.bool
requiredWhether the input is required or not eg. "this field is required". Defaults to True.Union[bool, str]
hintA tooltip displayed to the user. Defaults to None.str
full_widthWhether the input should use full screen width. Defaults to False.bool
button_textWhat text to display on the button when the widget is not part of a Page. Defaults to 'Next'.str

Return Values

TypeDescription
Union[FileResponse, List[FileResponse]]A object containing the file uploaded by the user: FileResponse(path: Path, file: BufferedReader). If the multiple flag is set as True, it might contain a list of FileResponses.