FileDrop components
FileDrop comes in two flavours:
FileDropfor a single file uploadFileDropMultiplewhich allows for multiple file upload
FileDrop
Region a user can drop a file into for file uploading.
@solara.component
def FileDrop(
label="Drop file here",
on_total_progress: Optional[Callable[[float], None]] = None,
on_file: Optional[Callable[[FileInfo], None]] = None,
lazy: bool = True,
):
...
If lazy=True, no file content will be loaded into memory,
nor will any data be transferred by default.
If lazy=False, file content will be loaded into memory and passed to the on_file callback via the FileInfo.data attribute.
A file object is of the following argument type:
class FileInfo(typing.TypedDict):
name: str # file name
size: int # file size in bytes
file_obj: typing.BinaryIO
data: Optional[bytes]: bytes # only present if lazy=False
Arguments
on_total_progress: Will be called with the progress in % of the file upload.on_file: Will be called with aFileInfoobject, which contains the file.name,.lengthand a.file_objobject.lazy: Whether to load the file contents into memory or not. IfFalse, the file contents will be loaded into memory via the.dataattribute of file object(s).
Load into Pandas
To load the data into a Pandas DF, set lazy=False and use file['file_obj'] (be careful of memory)
You can run this directly in your Jupyter notebook
import io
import pandas as pd
import solara
@solara.component
def Page():
def load_file_df(file):
df = pd.read_csv(file["file_obj"])
print("Loaded dataframe:")
print(df)
solara.FileDrop(label="Drop file to see dataframe!", on_file=load_file_df)
FileDropMultiple
Region a user can drop multiple files into for file uploading.
@solara.component
def FileDropMultiple(
label="Drop files here",
on_total_progress: Optional[Callable[[float], None]] = None,
on_file: Optional[Callable[[List[FileInfo]], None]] = None,
lazy: bool = True,
):
...
Almost identical to FileDrop except that multiple files can be dropped and on_file is called
with a list of FileInfo objects.
Arguments
on_total_progress: Will be called with the progress in % of the file(s) upload.on_file: Will be called with aList[FileInfo]. EachFileInfocontains the file.name,.length,.file_objobject, and.dataattributes.lazy: Whether to load the file contents into memory or not.
Example
FileDrop
Drag and drop a file here to read the first 100 bytes.
FileDropMultiple
Drag and drop files(s) here to read the first 100 bytes.