Skip to content

Commit

Permalink
add example and temporary fix
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Jun 22, 2023
1 parent bbd32d9 commit 7eb94ee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
8 changes: 7 additions & 1 deletion solara/components/input_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def InputFile(
):
"""A file input widget the user can click on and select one or multiple files for uploading.
If lazy=True, no file content will be loaded into memory,
If lazy=True, no file content will be loaded into memory,
nor will any data be transferred by default.
A list of file objects is passed to the `on_file` callback, and data will be transferred
when needed.
Expand All @@ -45,6 +47,7 @@ class FileInfo(typing.TypedDict):
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 a `List[FileInfo]` object, which contain the file `.name`, `.length` and a `.file_obj` objects.
Expand Down Expand Up @@ -92,6 +95,9 @@ def handle_file() -> None:
if not on_file:
return
if not wired_files:
# TODO: we don't want to directly trigger this
# but we do want it on clear.
return
if multiple:
empty: List[FileInfo] = []
on_file(empty)
Expand Down
2 changes: 1 addition & 1 deletion solara/website/pages/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"name": "Input",
"icon": "mdi-chevron-left-box",
"pages": ["button", "checkbox", "input", "select", "slider", "togglebuttons", "file_browser", "file_drop"],
"pages": ["button", "checkbox", "input", "select", "slider", "togglebuttons", "file_browser", "file_drop", "file"],
},
{
"name": "Output",
Expand Down
36 changes: 36 additions & 0 deletions solara/website/pages/api/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
# InputFile
"""
import textwrap

import solara
from solara.components.file_drop import FileInfo
from solara.website.utils import apidoc


@solara.component
def Page():
content, set_content = solara.use_state(b"")
filename, set_filename = solara.use_state("")
size, set_size = solara.use_state(0)

def on_file(file: FileInfo):
set_filename(file["name"])
set_size(file["size"])
f = file["file_obj"]
set_content(f.read(100))

with solara.Div() as main:
solara.InputFile(
label="Upload file to read 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))))

return main


__doc__ += apidoc(solara.InputFile.f) # type: ignore

0 comments on commit 7eb94ee

Please sign in to comment.