You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/assets/overview.md
+101-3Lines changed: 101 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ import reflex as rx
4
4
5
5
# Assets
6
6
7
-
Static files such as imagesand stylesheets can be placed in `assets/` folder of the project. These files can be referenced within your app.
7
+
Static files such as images, stylesheets, fonts, and other resources can be placed in the `assets/` folder of your Reflex project. These files are automatically made available to your application during both development and production.
8
8
9
9
```md alert
10
10
# Assets are copied during the build process.
@@ -13,9 +13,39 @@ Any files placed within the `assets/` folder at runtime will not be available to
13
13
when running in production mode. The `assets/` folder should only be used for static files.
14
14
```
15
15
16
+
## Types of Assets
17
+
18
+
Reflex supports various types of static assets:
19
+
20
+
-**Images**: PNG, JPG, GIF, SVG, WebP, etc.
21
+
-**Stylesheets**: CSS files
22
+
-**Fonts**: TTF, OTF, WOFF, WOFF2
23
+
-**Scripts**: JavaScript files
24
+
-**Data files**: JSON, CSV, etc.
25
+
-**Media**: Audio, video files
26
+
-**Documents**: PDF, etc.
27
+
28
+
## Asset Directory Structure
29
+
30
+
The `assets/` directory is located at the root of your Reflex project. You can organize your assets in subdirectories for better management:
31
+
32
+
```bash
33
+
assets/
34
+
├── images/
35
+
│ ├── logo.png
36
+
│ └── background.jpg
37
+
├── css/
38
+
│ └── custom.css
39
+
├── fonts/
40
+
│ └── custom-font.ttf
41
+
├── js/
42
+
│ └── script.js
43
+
└── favicon.ico
44
+
```
45
+
16
46
## Referencing Assets
17
47
18
-
To reference an image in the `assets/`simply pass the relative path as a prop.
48
+
To reference an asset in your Reflex app, use the path relative to the `assets/`directory, prefixed with a forward slash `/`.
19
49
20
50
For example, you can store your logo in your assets folder:
# Always prefix the asset path with a forward slash `/` to reference the asset from the root of the project, or it may not display correctly on non-root pages.
35
65
```
36
66
67
+
## Using Assets in Different Components
68
+
69
+
Assets can be used in various Reflex components:
70
+
71
+
### Images
72
+
73
+
```python
74
+
rx.image(src="/images/logo.png", width="100px")
75
+
```
76
+
77
+
### CSS Stylesheets
78
+
79
+
```python
80
+
rx.link(rel="stylesheet", href="/css/custom.css")
81
+
```
82
+
83
+
### JavaScript Files
84
+
85
+
```python
86
+
rx.script(src="/js/script.js")
87
+
```
88
+
89
+
### Background Images in Style Props
90
+
91
+
```python
92
+
rx.box(
93
+
width="100%",
94
+
height="200px",
95
+
background_image="url('/images/background.jpg')",
96
+
background_size="cover",
97
+
)
98
+
```
99
+
100
+
## Shared Assets
101
+
102
+
For library developers, Reflex provides a way to share assets from your library with applications that use your library. This is done using the `rx.asset()` function:
When `shared=True`, the asset is expected to be located next to the Python file that references it, not in the app's `assets/` directory. Reflex will automatically create a symlink to make the asset available to the app.
109
+
110
+
You can also specify a subfolder for shared assets:
@@ -6,57 +6,80 @@ from pcweb.pages.docs import api_reference
6
6
7
7
# Files
8
8
9
-
In addition to any assets you ship with your app, many web app will often need to receive or send files, whether you want to share media, allow user to import their data, or export some backend data.
9
+
In addition to static assets that ship with your app, many web applications need to handle dynamic file operations such as:
10
10
11
-
In this section, we will cover all you need to know for manipulating files in Reflex.
11
+
- Allowing users to download files from your server
12
+
- Enabling users to upload files to your server
13
+
- Processing uploaded files on the backend
14
+
- Generating files dynamically for download
15
+
16
+
This section covers everything you need to know about file handling in Reflex.
12
17
13
18
## Download
14
19
15
-
If you want to let the users of your app download files from your server to their computer, Reflex offer you two way.
20
+
Reflex offers multiple ways to let users download files from your server to their computer.
16
21
17
-
### With a regular link
22
+
### With a Regular Link
18
23
19
-
For some basic usage, simply providing the path to your resource in a`rx.link` will work, and clicking the link will download or display the resource.
24
+
For basic usage, simply providing the path to your resource in an`rx.link`component will work. Clicking the link will download or display the resource depending on the file type and browser settings.
The `data` arg accepts `str` or `bytes` data, a `data:` URI, `PIL.Image`, or any state Var. If the Var is not already a string, it will be converted to a string using `JSON.stringify`. This allows complex state structures to be offered as JSON downloads.
93
+
#### Supported Data Types
94
+
95
+
The `data` parameter of `rx.download` accepts various types:
71
96
72
-
Reference page for `rx.download`[here]({api_reference.special_events.path}#rx.download).
97
+
-`str`: Text data (will be encoded as UTF-8)
98
+
-`bytes`: Binary data
99
+
-`data:` URI: For inline data
100
+
-`PIL.Image`: Image objects from the Pillow library
101
+
- Any state `Var`: Will be converted to JSON if not already a string
102
+
103
+
This flexibility allows you to offer complex state structures as JSON downloads:
104
+
105
+
```python
106
+
@rx.event
107
+
defdownload_user_data(self):
108
+
"""Download the user's data as a JSON file."""
109
+
return rx.download(
110
+
data=self.user_data, # This could be a dict or any JSON-serializable object
111
+
filename="user_data.json"
112
+
)
113
+
```
114
+
115
+
For more details on `rx.download`, see the [reference page]({api_reference.special_events.path}#rx.download).
73
116
74
117
## Upload
75
118
76
-
Uploading files to your server let your users interact with your app in a different way than just filling forms to provide data.
119
+
File uploads are essential for many web applications. Reflex provides the `rx.upload` component to handle file uploads with features like:
77
120
78
-
The component `rx.upload` let your users upload files on the server.
121
+
- Drag-and-drop support
122
+
- Multiple file selection
123
+
- File type filtering
124
+
- Upload progress tracking
125
+
- Cancellation support
79
126
80
-
Here is a basic example of how it is used:
127
+
### Basic Upload Example
128
+
129
+
Here's a simple example of how to use the upload component:
For detailed information, see the reference page of the component [here]({library.forms.upload.path}).
205
+
### Upload Process Flow
206
+
207
+
1. User selects files via the `rx.upload` component (by clicking or drag-and-drop)
208
+
2. Selected files appear in the UI using `rx.selected_files("upload_id")`
209
+
3. User clicks an upload button that triggers an event handler with `rx.upload_files(upload_id="upload_id")`
210
+
4. The event handler receives the files as `list[rx.UploadFile]` objects
211
+
5. The handler processes the files (saving, analyzing, etc.)
212
+
6. The UI updates to reflect the uploaded files
213
+
214
+
### File Storage and Access
215
+
216
+
Reflex provides helper functions to manage uploaded files:
217
+
218
+
#### `rx.get_upload_dir()`
219
+
220
+
Returns the directory where uploaded files should be saved. By default, this is `./uploaded_files` in your project root, but it can be configured with the `REFLEX_UPLOADED_FILES_DIR` environment variable.
221
+
222
+
```python
223
+
# Save an uploaded file
224
+
outfile = rx.get_upload_dir() /file.filename
225
+
with outfile.open("wb") as f:
226
+
f.write(file_data)
227
+
```
228
+
229
+
#### `rx.get_upload_url(filename)`
230
+
231
+
Returns the URL to access an uploaded file. This URL is automatically mounted by Reflex at `/_upload/`.
When using the Reflex hosting service, the uploaded files directory is not persistent and will be cleared on every deployment. For persistent storage of uploaded files in production, it's recommended to use an external service such as AWS S3, Google Cloud Storage, or similar cloud storage solutions.
245
+
```
246
+
247
+
### Advanced Upload Features
248
+
249
+
For more advanced upload scenarios, see the [Upload component reference]({library.forms.upload.path}) which covers:
0 commit comments