77import shutil
88import subprocess
99import textwrap
10+ from collections .abc import Callable
1011from glob import glob
1112from logging import getLogger
1213from pathlib import Path
1718import reactpy
1819from reactpy .config import REACTPY_DEBUG , REACTPY_PATH_PREFIX , REACTPY_WEB_MODULES_DIR
1920from reactpy .types import VdomDict
20- from reactpy .utils import reactpy_to_string
21+ from reactpy .utils import GITHUB_ACTIONS , reactpy_to_string
2122
2223if 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
143149def 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
0 commit comments