Skip to content

Commit 47ddc8d

Browse files
tgberkeleyTom Gotsman
andauthored
update upload docs and links to audio, video and image docs (#1159)
* update upload docs and links to audio, video and image docs * change gif to webm --------- Co-authored-by: Tom Gotsman <tomgotsman@Toms-MacBook-Pro.local>
1 parent 3822e24 commit 47ddc8d

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

assets/upload_single_video.webm

160 KB
Binary file not shown.

docs/library/forms/upload.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ an event handler.
3131
To upload the file(s), you need to bind an event handler and pass the special
3232
`rx.upload_files(upload_id=id)` event arg to it.
3333

34-
A full example is shown below.
34+
35+
### Multiple File Upload
36+
37+
Below is an example of how to allow multiple file uploads (in this case images).
3538

3639
```python demo box
3740
rx.image(src="/upload.gif")
@@ -92,6 +95,87 @@ def index():
9295
)
9396
```
9497

98+
### Uploading a Single File (Video)
99+
100+
Below is an example of how to allow only a single file upload and render (in this case a video).
101+
102+
```python demo box
103+
rx.el.video(src="/upload_single_video.webm", auto_play=True, controls=True, loop=True)
104+
```
105+
106+
```python
107+
class State(rx.State):
108+
"""The app state."""
109+
110+
# The video to show.
111+
video: str
112+
113+
@rx.event
114+
async def handle_upload(
115+
self, files: list[rx.UploadFile]
116+
):
117+
"""Handle the upload of file(s).
118+
119+
Args:
120+
files: The uploaded files.
121+
"""
122+
current_file = files[0]
123+
upload_data = await current_file.read()
124+
outfile = rx.get_upload_dir() / current_file.filename
125+
126+
# Save the file.
127+
with outfile.open("wb") as file_object:
128+
file_object.write(upload_data)
129+
130+
# Update the video var.
131+
self.video = current_file.filename
132+
133+
134+
color = "rgb(107,99,246)"
135+
136+
137+
def index():
138+
"""The main view."""
139+
return rx.vstack(
140+
rx.upload(
141+
rx.vstack(
142+
rx.button(
143+
"Select File",
144+
color=color,
145+
bg="white",
146+
border=f"1px solid \{color}",
147+
),
148+
rx.text(
149+
"Drag and drop files here or click to select files"
150+
),
151+
),
152+
id="upload1",
153+
max_files=1,
154+
border=f"1px dotted \{color}",
155+
padding="5em",
156+
),
157+
rx.text(rx.selected_files("upload1")),
158+
rx.button(
159+
"Upload",
160+
on_click=State.handle_upload(
161+
rx.upload_files(upload_id="upload1")
162+
),
163+
),
164+
rx.button(
165+
"Clear",
166+
on_click=rx.clear_selected_files("upload1"),
167+
),
168+
rx.cond(
169+
State.video,
170+
rx.video(url=rx.get_upload_url(State.video)),
171+
),
172+
padding="5em",
173+
)
174+
```
175+
176+
177+
### Customizing the Upload
178+
95179
In the example below, the upload component accepts a maximum number of 5 files of specific types.
96180
It also disables the use of the space or enter key in uploading files.
97181

@@ -166,6 +250,7 @@ def index():
166250
)
167251
```
168252

253+
169254
## Handling the Upload
170255

171256
Your event handler should be an async function that accepts a single argument,

docs/library/media/audio.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ components:
77

88
```python exec
99
import reflex as rx
10+
from pcweb.pages.docs import library
1011
```
1112

1213
The audio component can display an audio given an src path as an argument. This could either be a local path from the assets folder or an external link.
@@ -20,3 +21,8 @@ rx.audio(
2021
```
2122

2223
If we had a local file in the `assets` folder named `test.mp3` we could set `url="/test.mp3"` to view the audio file.
24+
25+
```md alert info
26+
# How to let your user upload an audio file
27+
To let a user upload an audio file to your app check out the [upload docs]({library.forms.upload.path}).
28+
```

docs/library/media/image.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ components:
55

66
```python exec
77
import reflex as rx
8+
from pcweb.pages.docs import library
89
```
910

1011
# Image
@@ -54,3 +55,8 @@ def image_pil_example():
5455
# rx.image only accepts URLs and Pillow Images
5556
A cv2 image must be covnerted to a PIL image to be passed directly to `rx.image` as a State variable, or saved to the `assets` folder and then passed to the `rx.image` component.
5657
```
58+
59+
```md alert info
60+
# How to let your user upload an image
61+
To let a user upload an image to your app check out the [upload docs]({library.forms.upload.path}).
62+
```

docs/library/media/video.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ components:
77

88
```python exec
99
import reflex as rx
10+
from pcweb.pages.docs import library
1011
```
1112

1213
The video component can display a video given an src path as an argument. This could either be a local path from the assets folder or an external link.
@@ -20,3 +21,9 @@ rx.video(
2021
```
2122

2223
If we had a local file in the `assets` folder named `test.mp4` we could set `url="/test.mp4"` to view the video.
24+
25+
26+
```md alert info
27+
# How to let your user upload a video
28+
To let a user upload a video to your app check out the [upload docs]({library.forms.upload.path}).
29+
```

0 commit comments

Comments
 (0)