-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b2ea6d
commit fa6cd23
Showing
3 changed files
with
120 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 53 additions & 21 deletions
74
solara/website/pages/documentation/components/input/file_drop.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,75 @@ | ||
""" | ||
# FileDrop | ||
# FileDrop components | ||
FileDrop comes in two flavours: | ||
* `FileDrop` for a single file upload | ||
* `FileDropMultiple` which allows for multiple file upload | ||
""" | ||
import textwrap | ||
from typing import List, Union, cast | ||
from typing import List, cast | ||
|
||
import solara | ||
from solara.components.file_drop import FileInfo | ||
from solara.website.utils import apidoc | ||
|
||
|
||
@solara.component | ||
def Page(): | ||
def FileDropMultipleDemo(): | ||
content, set_content = solara.use_state(cast(List[bytes], [])) | ||
filename, set_filename = solara.use_state(cast(List[str], [])) | ||
size, set_size = solara.use_state(cast(List[int], [])) | ||
multiple_upload, set_multiple_upload = solara.use_state(False) | ||
|
||
def process_files(files: Union[FileInfo, List[FileInfo]]): | ||
if not isinstance(files, list): | ||
files = [files] | ||
def on_file(files: List[FileInfo]): | ||
set_filename([f["name"] for f in files]) | ||
set_size([f["size"] for f in files]) | ||
set_content([f["file_obj"].read(100) for f in files]) | ||
|
||
with solara.Div() as main: | ||
solara.Checkbox(label="Multiple upload", value=multiple_upload, on_value=set_multiple_upload) | ||
solara.FileDrop( | ||
label="Drag and drop files(s) here to read the first 100 bytes.", | ||
on_file=process_files, | ||
lazy=True, # We will only read the first 100 bytes | ||
multiple=multiple_upload, | ||
) | ||
if content: | ||
solara.Info(f"Number of uploaded files: {len(filename)}") | ||
for f, s, c in zip(filename, size, content): | ||
solara.Info(f"File {f} has total length: {s}\n, first 100 bytes:") | ||
solara.Preformatted("\n".join(textwrap.wrap(repr(c)))) | ||
solara.FileDropMultiple( | ||
label="Drag and drop files(s) here to read the first 100 bytes.", | ||
on_file=on_file, | ||
lazy=True, # We will only read the first 100 bytes | ||
) | ||
if content: | ||
solara.Info(f"Number of uploaded files: {len(filename)}") | ||
for f, s, c in zip(filename, size, content): | ||
solara.Info(f"File {f} has total length: {s}\n, first 100 bytes:") | ||
solara.Preformatted("\n".join(textwrap.wrap(repr(c)))) | ||
|
||
|
||
return main | ||
@solara.component | ||
def FileDropDemo(): | ||
content, set_content = solara.use_state(b"") | ||
filename, set_filename = solara.use_state("") | ||
size, set_size = solara.use_state(0) | ||
|
||
def on_file(f: FileInfo): | ||
set_filename(f["name"]) | ||
set_size(f["size"]) | ||
set_content(f["file_obj"].read(100)) | ||
|
||
solara.FileDrop( | ||
label="Drag and drop a file here to read the first 100 bytes.", | ||
on_file=on_file, | ||
lazy=True, # We will only read the first 100 bytes | ||
) | ||
if content: | ||
solara.Info(f"File {filename} has total length: {size}\n, first 100 bytes:") | ||
solara.Preformatted("\n".join(textwrap.wrap(repr(content)))) | ||
|
||
|
||
@solara.component | ||
def Page(): | ||
with solara.Row(): | ||
with solara.Card(title="FileDrop"): | ||
FileDropDemo() | ||
with solara.Card(title="FileDropMultiple"): | ||
FileDropMultipleDemo() | ||
|
||
|
||
__doc__ += "# FileDrop" | ||
__doc__ += apidoc(solara.FileDrop.f) # type: ignore | ||
__doc__ += "# FileDropMultiple" | ||
__doc__ += apidoc(solara.FileDropMultiple.f) # type: ignore |