File Upload
info
This widget is deprecated. Use the File Upload widget instead.
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
Name | Description | Type |
---|---|---|
label | The label to display to the user | str |
initial_value | The initial value to display to the user. Defaults to "". | str |
multiple | Whether the user will be allowed to upload multiple files. Defaults to False. | bool |
accepted_formats | The specific file types that the input should accept. Defaults to [], accepting all file formats. | list |
min | The minimal amount of files that should be submitted. Only applied if multiple is True. Defaults to None. | number |
max | The maximum amount of files that should be submitted. Only applied if multiple is True. Defaults to None. | number |
max_file_size | Maximum size allowed to be transfered in total in MB. | float |
disabled | whether the input is disabled. Defaults to False. | bool |
required | Whether the input is required or not eg. "this field is required". Defaults to True. | Union[bool, str] |
hint | A tooltip displayed to the user. Defaults to None. | str |
full_width | Whether the input should use full screen width. Defaults to False. | bool |
button_text | What text to display on the button when the widget is not part of a Page. Defaults to 'Next'. | str |
Return Values
Type | Description |
---|---|
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. |