2727 from reactpy_django .types import AsyncFormEvent , SyncFormEvent , ViewToComponentConstructor , ViewToIframeConstructor
2828
2929
30- DjangoJS = web .export (
31- web .module_from_file ("reactpy-django" , file = Path (__file__ ).parent / "static" / "reactpy_django" / "client.js" ),
32- ("OnlyOnceJS" ),
33- )
34-
35-
3630def view_to_component (
3731 view : Callable | View | str ,
3832 transforms : Sequence [Callable [[VdomDict ], Any ]] = (),
@@ -92,7 +86,9 @@ def constructor(
9286 return constructor
9387
9488
95- def django_css (static_path : str , key : Key | None = None ) -> ComponentType :
89+ def django_css (
90+ static_path : str , only_once : bool = False , auto_remove : bool = False , key : Key | None = None
91+ ) -> ComponentType :
9692 """Fetches a CSS static file for use within ReactPy. This allows for deferred CSS loading.
9793
9894 Args:
@@ -102,11 +98,11 @@ def django_css(static_path: str, key: Key | None = None) -> ComponentType:
10298 immediate siblings
10399 """
104100
105- return _django_css (static_path = static_path , key = key )
101+ return _django_css (static_path = static_path , only_once = only_once , auto_remove = auto_remove , key = key )
106102
107103
108104def django_js (
109- static_path : str , only_once : bool = False , only_once_auto_remove : bool = False , key : Key | None = None
105+ static_path : str , only_once : bool = False , auto_remove : bool = False , key : Key | None = None
110106) -> ComponentType :
111107 """Fetches a JS static file for use within ReactPy. This allows for deferred JS loading.
112108
@@ -117,9 +113,7 @@ def django_js(
117113 immediate siblings
118114 """
119115
120- return _django_js (
121- static_path = static_path , only_once = only_once , only_once_auto_remove = only_once_auto_remove , key = key
122- )
116+ return _django_js (static_path = static_path , only_once = only_once , auto_remove = auto_remove , key = key )
123117
124118
125119def django_form (
@@ -285,13 +279,30 @@ def _view_to_iframe(
285279
286280
287281@component
288- def _django_css (static_path : str ):
282+ def _django_css (static_path : str , only_once : bool , auto_remove : bool ):
283+ if only_once :
284+ return LoadOnlyOnce ({
285+ "path" : static (static_path ),
286+ "nodeName" : "link" ,
287+ "autoRemove" : auto_remove ,
288+ })
289+
289290 return html .style (cached_static_file (static_path ))
290291
291292
292293@component
293- def _django_js (static_path : str , only_once : bool , only_once_auto_remove : bool ):
294+ def _django_js (static_path : str , only_once : bool , auto_remove : bool ):
294295 if only_once :
295- return DjangoJS ({"jsPath" : static (static_path ), "autoRemove" : only_once_auto_remove })
296+ return LoadOnlyOnce ({
297+ "path" : static (static_path ),
298+ "nodeName" : "script" ,
299+ "autoRemove" : auto_remove ,
300+ })
296301
297302 return html .script (cached_static_file (static_path ))
303+
304+
305+ LoadOnlyOnce = web .export (
306+ web .module_from_file ("reactpy-django" , file = Path (__file__ ).parent / "static" / "reactpy_django" / "client.js" ),
307+ ("LoadOnlyOnce" ),
308+ )
0 commit comments