Skip to content

Commit 8b1ad1d

Browse files
Add rx.asset documentation and explain difference between assets vs upload directory (#1313)
* Add rx.asset documentation and explain difference between assets and upload directory Co-Authored-By: Alek Petuskey <[email protected]> * Remove 'simply' from documentation as requested in PR review Co-Authored-By: Alek Petuskey <[email protected]> * Replace markdown table with Reflex table component Co-Authored-By: Alek Petuskey <[email protected]> * Update table to use rx.code with violet styling for quoted items Co-Authored-By: Alek Petuskey <[email protected]> * Fix spacing property in HStack component to use valid value Co-Authored-By: Alek Petuskey <[email protected]> * Change table code block to use python eval instead of demo exec Co-Authored-By: Alek Petuskey <[email protected]> * Change table code block to use python box instead of eval Co-Authored-By: Alek Petuskey <[email protected]> * Change table code block to use python demo instead of box Co-Authored-By: Alek Petuskey <[email protected]> * Restructure table code to use direct expressions with python demo exec Co-Authored-By: Alek Petuskey <[email protected]> * Simplify table code and use python eval to hide code display Co-Authored-By: Alek Petuskey <[email protected]> * Address PR comments: update rx.asset documentation Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]> Co-authored-by: Alek Petuskey <[email protected]>
1 parent 8736cb4 commit 8b1ad1d

File tree

3 files changed

+121
-9
lines changed

3 files changed

+121
-9
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, 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 in the same directory as 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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import reflex as rx
33
from pcweb.pages.docs import library
44
from pcweb.pages.docs import api_reference
5+
from pcweb.styles.styles import get_code_style
6+
from pcweb.styles.colors import c_color
57
```
68

79
# Files
@@ -10,6 +12,67 @@ In addition to any assets you ship with your app, many web app will often need t
1012

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

15+
## Assets vs Upload Directory
16+
17+
Before diving into file uploads and downloads, it's important to understand the difference between assets and the upload directory in Reflex:
18+
19+
```python eval
20+
# Simple table comparing assets vs upload directory
21+
rx.table.root(
22+
rx.table.header(
23+
rx.table.row(
24+
rx.table.column_header_cell("Feature"),
25+
rx.table.column_header_cell("Assets"),
26+
rx.table.column_header_cell("Upload Directory"),
27+
),
28+
),
29+
rx.table.body(
30+
rx.table.row(
31+
rx.table.cell(rx.text("Purpose", font_weight="bold")),
32+
rx.table.cell(rx.text("Static files included with your app (images, stylesheets, scripts)")),
33+
rx.table.cell(rx.text("Dynamic files uploaded by users during runtime")),
34+
),
35+
rx.table.row(
36+
rx.table.cell(rx.text("Location", font_weight="bold")),
37+
rx.table.cell(rx.hstack(
38+
rx.code("assets/", style=get_code_style("violet")),
39+
rx.text(" folder or next to Python files (shared assets)"),
40+
spacing="2",
41+
)),
42+
rx.table.cell(rx.hstack(
43+
rx.code("uploaded_files/", style=get_code_style("violet")),
44+
rx.text(" directory (configurable)"),
45+
spacing="2",
46+
)),
47+
),
48+
rx.table.row(
49+
rx.table.cell(rx.text("Access Method", font_weight="bold")),
50+
rx.table.cell(rx.hstack(
51+
rx.code("rx.asset()", style=get_code_style("violet")),
52+
rx.text(" or direct path reference"),
53+
spacing="2",
54+
)),
55+
rx.table.cell(rx.code("rx.get_upload_url()", style=get_code_style("violet"))),
56+
),
57+
rx.table.row(
58+
rx.table.cell(rx.text("When to Use", font_weight="bold")),
59+
rx.table.cell(rx.text("For files that are part of your application's codebase")),
60+
rx.table.cell(rx.text("For files that users upload or generate through your application")),
61+
),
62+
rx.table.row(
63+
rx.table.cell(rx.text("Availability", font_weight="bold")),
64+
rx.table.cell(rx.text("Available at compile time")),
65+
rx.table.cell(rx.text("Available at runtime")),
66+
),
67+
),
68+
width="100%",
69+
)
70+
```
71+
72+
73+
74+
For more information about assets, see the [Assets Overview](/docs/assets/overview/).
75+
1376
## Download
1477

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

docs/wrapping-react/guide.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,28 +441,26 @@ def index():
441441

442442
## Assets
443443

444-
_Experimental feature added in v0.5.3_.
445-
446444
If a wrapped component depends on assets such as images, scripts, or
447-
stylesheets, these can kept adjacent to the component code and
448-
included in the final build using the `rx._x.asset` function.
445+
stylesheets, these can be kept adjacent to the component code and
446+
included in the final build using the `rx.asset` function.
449447

450-
`rx._x.asset` returns a relative path that references the asset in the compiled
448+
`rx.asset` returns a relative path that references the asset in the compiled
451449
output. The target files are copied into a subdirectory of `assets/external`
452450
based on the module where they are initially used. This allows third-party
453451
components to have external assets with the same name without conflicting
454452
with each other.
455453

456454
For example, if there is an SVG file named `wave.svg` in the same directory as
457-
this component, it can be rendered using `rx.image` and `rx._x.asset`.
455+
this component, it can be rendered using `rx.image` and `rx.asset`.
458456

459457
```python
460458
class Hello(rx.Component):
461459
@classmethod
462460
def create(cls, *children, **props) -> rx.Component:
463461
props.setdefault("align", "center")
464462
return rx.hstack(
465-
rx.image(src=rx._x.asset("wave.svg"), width="50px", height="50px"),
463+
rx.image(src=rx.asset("wave.svg", shared=True), width="50px", height="50px"),
466464
rx.heading("Hello ", *children),
467465
**props
468466
)
@@ -471,4 +469,4 @@ class Hello(rx.Component):
471469

472470
## Debugging
473471

474-
If you encounter an error while wrapping a component it is recommended to check the Console in the browser developer tools. You can access this by going to inspect element and then clicking on the Console tab on Mac. This is because the Console is where most Javascript errors are logged.
472+
If you encounter an error while wrapping a component it is recommended to check the Console in the browser developer tools. You can access this by going to inspect element and then clicking on the Console tab on Mac. This is because the Console is where most Javascript errors are logged.

0 commit comments

Comments
 (0)