Skip to content

Commit fabfb88

Browse files
committed
Make pyscript utils more extensible
1 parent 5171a1a commit fabfb88

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/reactpy/executors/pyscript/utils.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
import subprocess
99
import textwrap
10+
from collections.abc import Callable
1011
from glob import glob
1112
from logging import getLogger
1213
from pathlib import Path
@@ -17,7 +18,7 @@
1718
import reactpy
1819
from reactpy.config import REACTPY_DEBUG, REACTPY_PATH_PREFIX, REACTPY_WEB_MODULES_DIR
1920
from reactpy.types import VdomDict
20-
from reactpy.utils import reactpy_to_string
21+
from reactpy.utils import GITHUB_ACTIONS, reactpy_to_string
2122

2223
if TYPE_CHECKING:
2324
from collections.abc import Sequence
@@ -47,15 +48,18 @@ def minify_python(source: str) -> str:
4748
)
4849

4950

50-
def pyscript_executor_html(file_paths: Sequence[str], uuid: str, root: str) -> str:
51+
def pyscript_executor_html(
52+
file_paths: Sequence[str], uuid: str, root: str, cache_handler: Callable
53+
) -> str:
5154
"""Inserts the user's code into the PyScript template using pattern matching."""
5255
# Create a valid PyScript executor by replacing the template values
5356
executor = PYSCRIPT_COMPONENT_TEMPLATE.replace("UUID", uuid)
5457
executor = executor.replace("return root()", f"return {root}()")
5558

5659
# Fetch the user's PyScript code
60+
cache_handler = cache_handler or fetch_cached_python_file
5761
all_file_contents: list[str] = []
58-
all_file_contents.extend(cached_file_read(file_path) for file_path in file_paths)
62+
all_file_contents.extend(cache_handler(file_path) for file_path in file_paths)
5963

6064
# Prepare the PyScript code block
6165
user_code = "\n".join(all_file_contents) # Combine all user code
@@ -110,12 +114,14 @@ def extend_pyscript_config(
110114
extra_py: Sequence[str],
111115
extra_js: dict[str, str] | str,
112116
config: dict[str, Any] | str,
117+
modules: dict[str, str] | str | None = None,
113118
) -> str:
114119
# Extends ReactPy's default PyScript config with user provided values.
115120
pyscript_config: dict[str, Any] = {
116121
"packages": [reactpy_version_string(), "jsonpointer==3.*", "ssl"],
117122
"js_modules": {
118-
"main": {
123+
"main": modules
124+
or {
119125
f"{REACTPY_PATH_PREFIX.current}static/morphdom/morphdom-esm.js": "morphdom"
120126
}
121127
},
@@ -141,12 +147,9 @@ def extend_pyscript_config(
141147

142148

143149
def reactpy_version_string() -> str: # nocov
144-
from reactpy.testing.common import GITHUB_ACTIONS
145-
146-
local_version = reactpy.__version__
147-
148150
# Get a list of all versions via `pip index versions`
149151
result = get_reactpy_versions()
152+
local_version = reactpy.__version__
150153

151154
# Check if the command failed
152155
if not result:
@@ -231,6 +234,6 @@ def get_reactpy_versions() -> dict[Any, Any]:
231234

232235

233236
@functools.cache
234-
def cached_file_read(file_path: str, minifiy: bool = True) -> str:
237+
def fetch_cached_python_file(file_path: str, minifiy: bool = True) -> str:
235238
content = Path(file_path).read_text(encoding="utf-8").strip()
236239
return minify_python(content) if minifiy else content

tests/test_core/test_hooks.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ async def effect():
634634
await asyncio.wait_for(effect_was_cancelled.wait(), 1)
635635

636636

637-
638637
async def test_error_in_effect_is_gracefully_handled():
639638
@reactpy.component
640639
def ComponentWithEffect():
@@ -1420,4 +1419,3 @@ async def effect():
14201419

14211420
# Verify the previous effect was cancelled
14221421
await asyncio.wait_for(effect_was_cancelled.wait(), 1)
1423-

0 commit comments

Comments
 (0)