-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
**/.DS_Store | ||
**/*.pyc | ||
assets/external/* | ||
dist/* | ||
examples/ | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Helper functions for adding assets to the app.""" | ||
import inspect | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from reflex import constants | ||
|
||
|
||
def asset(relative_filename: str, subfolder: Optional[str] = None) -> str: | ||
"""Add an asset to the app. | ||
Place the file next to your including python file. | ||
Copies the file to the app's external assets directory. | ||
Example: | ||
```python | ||
rx.script(src=rx._x.asset("my_custom_javascript.js")) | ||
rx.image(src=rx._x.asset("test_image.png","subfolder")) | ||
``` | ||
Args: | ||
relative_filename: The relative filename of the asset. | ||
subfolder: The directory to place the asset in. | ||
Raises: | ||
FileNotFoundError: If the file does not exist. | ||
Returns: | ||
The relative URL to the copied asset. | ||
""" | ||
# Determine the file by which the asset is exposed. | ||
calling_file = inspect.stack()[1].filename | ||
module = inspect.getmodule(inspect.stack()[1][0]) | ||
assert module is not None | ||
caller_module_path = module.__name__.replace(".", "/") | ||
|
||
subfolder = f"{caller_module_path}/{subfolder}" if subfolder else caller_module_path | ||
|
||
src_file = Path(calling_file).parent / relative_filename | ||
|
||
assets = constants.Dirs.APP_ASSETS | ||
external = constants.Dirs.EXTERNAL_APP_ASSETS | ||
|
||
if not src_file.exists(): | ||
raise FileNotFoundError(f"File not found: {src_file}") | ||
|
||
# Create the asset folder in the currently compiling app. | ||
asset_folder = Path.cwd() / assets / external / subfolder | ||
asset_folder.mkdir(parents=True, exist_ok=True) | ||
|
||
dst_file = asset_folder / relative_filename | ||
|
||
if not dst_file.exists(): | ||
dst_file.symlink_to(src_file) | ||
|
||
asset_url = f"/{external}/{subfolder}/{relative_filename}" | ||
return asset_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const test = "inside custom_script.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import shutil | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import reflex as rx | ||
|
||
|
||
def test_asset(): | ||
# Test the asset function. | ||
|
||
# The asset function copies a file to the app's external assets directory. | ||
asset = rx._x.asset("custom_script.js", "subfolder") | ||
assert asset == "/external/test_assets/subfolder/custom_script.js" | ||
result_file = Path( | ||
Path.cwd(), "assets/external/test_assets/subfolder/custom_script.js" | ||
) | ||
assert result_file.exists() | ||
|
||
# Running a second time should not raise an error. | ||
asset = rx._x.asset("custom_script.js", "subfolder") | ||
|
||
# Test the asset function without a subfolder. | ||
asset = rx._x.asset("custom_script.js") | ||
assert asset == "/external/test_assets/custom_script.js" | ||
result_file = Path(Path.cwd(), "assets/external/test_assets/custom_script.js") | ||
assert result_file.exists() | ||
|
||
# clean up | ||
shutil.rmtree(Path.cwd() / "assets/external") | ||
|
||
with pytest.raises(FileNotFoundError): | ||
asset = rx._x.asset("non_existent_file.js") | ||
|
||
# Nothing is done to assets when file does not exist. | ||
assert not Path(Path.cwd() / "assets/external").exists() |