Skip to content

Commit 1dfe73c

Browse files
Add rx.asset documentation and explain difference between assets and upload directory
Co-Authored-By: Alek Petuskey <[email protected]>
1 parent c2bbe47 commit 1dfe73c

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

docs/assets/overview.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ when running in production mode. The `assets/` folder should only be used for st
1515

1616
## Referencing Assets
1717

18-
To reference an image in the `assets/` simply pass the relative path as a prop.
18+
There are two ways to reference assets in your Reflex app:
19+
20+
### 1. Direct Path Reference
21+
22+
To reference an image in the `assets/` folder, simply pass the relative path as a prop.
1923

2024
For example, you can store your logo in your assets folder:
2125

@@ -34,6 +38,53 @@ rx.image(src="/Reflex.svg", width="5em")
3438
# 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.
3539
```
3640

41+
### 2. Using rx.asset Function
42+
43+
The `rx.asset` function provides a more flexible way to reference assets in your app. It supports both local assets (in the app's `assets/` directory) and shared assets (placed next to your Python files).
44+
45+
#### Local Assets
46+
47+
Local assets are stored in the app's `assets/` directory and are referenced using `rx.asset`:
48+
49+
```python demo
50+
rx.image(src=rx.asset("Reflex.svg"), width="5em")
51+
```
52+
53+
#### Shared Assets
54+
55+
Shared assets are placed next to your Python file and are linked to the app's external assets directory. This is useful for creating reusable components with their own assets:
56+
57+
```python box
58+
# my_component.py
59+
import reflex as rx
60+
61+
# my_script.js is located next to this Python file
62+
def my_component():
63+
return rx.box(
64+
rx.script(src=rx.asset("my_script.js", shared=True)),
65+
"Component with custom script"
66+
)
67+
```
68+
69+
You can also specify a subfolder for shared assets:
70+
71+
```python box
72+
# my_component.py
73+
import reflex as rx
74+
75+
# image.png is located in a subfolder next to this Python file
76+
def my_component_with_image():
77+
return rx.image(
78+
src=rx.asset("image.png", shared=True, subfolder="images")
79+
)
80+
```
81+
82+
```md alert
83+
# Shared assets are linked to your app via symlinks.
84+
85+
When using `shared=True`, the asset is symlinked from its original location to your app's external assets directory. This allows you to keep assets alongside their related code.
86+
```
87+
3788
## Favicon
3889

3990
The favicon is the small icon that appears in the browser tab.

docs/assets/upload_and_download_files.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ In addition to any assets you ship with your app, many web app will often need t
1010

1111
In this section, we will cover all you need to know for manipulating files in Reflex.
1212

13+
## Assets vs Upload Directory
14+
15+
Before diving into file uploads and downloads, it's important to understand the difference between assets and the upload directory in Reflex:
16+
17+
| Feature | Assets | Upload Directory |
18+
| --- | --- | --- |
19+
| Purpose | Static files included with your app (images, stylesheets, scripts) | Dynamic files uploaded by users during runtime |
20+
| Location | `assets/` folder or next to Python files (shared assets) | `uploaded_files/` directory (configurable) |
21+
| Access Method | `rx.asset()` or direct path reference | `rx.get_upload_url()` |
22+
| When to Use | For files that are part of your application's codebase | For files that users upload through your application |
23+
| Availability | Available at compile time | Available at runtime |
24+
25+
```md alert
26+
# Assets are primarily intended for frontend use
27+
28+
Assets in Reflex are primarily intended for frontend use and are not expected to be read from the backend. When assets are needed in both frontend and backend, they are currently copied to the backend (though this behavior may change in future versions).
29+
```
30+
31+
For more information about assets, see the [Assets Overview](/docs/assets/overview/).
32+
1333
## Download
1434

1535
If you want to let the users of your app download files from your server to their computer, Reflex offer you two way.

0 commit comments

Comments
 (0)