File tree Expand file tree Collapse file tree 9 files changed +52
-28
lines changed Expand file tree Collapse file tree 9 files changed +52
-28
lines changed Original file line number Diff line number Diff line change @@ -322,33 +322,23 @@ def _python_to_pyscript(
322
322
config : str | dict = "" ,
323
323
root : str = "root" ,
324
324
):
325
+ rendered , set_rendered = hooks .use_state (False )
325
326
uuid = uuid4 ().hex .replace ("-" , "" )
326
327
initial = vdom_or_component_to_string (initial , uuid = uuid )
327
328
executor = render_pyscript_template (file_path , uuid , root )
328
329
new_config = extend_pyscript_config (config , extra_packages )
329
330
331
+ if not rendered :
332
+ # FIXME: This is needed to properly kill off any previous PyScript instances
333
+ # such as when a component is re-rendered due to WebSocket disconnection.
334
+ # There may be a better way to do this, but it's not clear at the moment
335
+ set_rendered (True )
336
+ return None
337
+
330
338
return html .div (
331
- html .link (
332
- {"rel" : "stylesheet" , "href" : static ("reactpy_django/pyscript/core.css" )}
333
- ),
334
- html .script (
335
- {
336
- "type" : "module" ,
337
- "src" : static (
338
- "reactpy_django/pyscript/core.js" ,
339
- ),
340
- }
341
- ),
342
339
html .div ((extra_props or {}) | {"id" : f"pyscript-{ uuid } " }, initial ),
343
340
PYSCRIPT_TAG (
344
- {
345
- "async" : "" ,
346
- "config" : orjson .dumps (new_config ).decode (),
347
- "id" : f"script-{ uuid } " ,
348
- },
341
+ {"async" : "" , "config" : orjson .dumps (new_config ).decode ()},
349
342
executor ,
350
343
),
351
- html .script (
352
- f"if (document.querySelector('#pyscript-{ uuid } ') && document.querySelector('#pyscript-{ uuid } ').childElementCount != 0 && document.querySelector('#script-{ uuid } ')) document.querySelector('#script-{ uuid } ').remove();"
353
- ),
354
344
)
Original file line number Diff line number Diff line change @@ -89,4 +89,6 @@ async def run(self):
89
89
self .render (layout , root_model )
90
90
91
91
92
- asyncio .create_task (LayoutManagerUUID ().run ())
92
+ # PyScript allows top-level await, which allows us to not throw errors on components
93
+ # that terminate early (such as hook-less components)
94
+ await LayoutManagerUUID ().run () # noqa: F704
Original file line number Diff line number Diff line change 1
- {% load static %}
2
- < link rel ="stylesheet " href ="{% static 'reactpy_django/pyscript/core.css' %} " />
3
- < script type ="module " src ="{% static 'reactpy_django/pyscript/core.js' %} "> </ script >
4
1
{% if reactpy_class %}< div id ="pyscript-{{reactpy_uuid}} " class ="{{reactpy_class}} "> {{reactpy_initial_html}}</ div >
5
2
{% endif %}
6
3
{% if not reactpy_class %}< div id ="pyscript-{{reactpy_uuid}} "> {{reactpy_initial_html}}</ div > {% endif %}
Original file line number Diff line number Diff line change
1
+ {% load static %}
2
+ < link rel ="stylesheet " href ="{% static 'reactpy_django/pyscript/core.css' %} " />
3
+ < link rel ="stylesheet " href ="{% static 'reactpy_django/pyscript-custom.css' %} " />
4
+ < script type ="module " async src ="{% static 'reactpy_django/pyscript/core.js' %} "> </ script >
Original file line number Diff line number Diff line change @@ -228,3 +228,8 @@ def pyscript_component(
228
228
"reactpy_initial_html" : initial ,
229
229
"reactpy_config" : orjson .dumps (new_config ).decode (),
230
230
}
231
+
232
+
233
+ @register .inclusion_tag ("reactpy/pyscript_static_files.html" )
234
+ def pyscript_static_files ():
235
+ return {}
Original file line number Diff line number Diff line change 55
55
+ rf"({ _OFFLINE_KWARG_PATTERN } |{ _GENERIC_KWARG_PATTERN } )*?"
56
56
+ r"\s*%}"
57
57
)
58
- PYSCRIPT_TEMPLATE = ( Path ( __file__ ). parent / "pyscript_template.py" ). read_text (
59
- encoding = "utf-8 "
60
- )
58
+ PYSCRIPT_COMPONENT_TEMPLATE = (
59
+ Path ( __file__ ). parent / "pyscript" / "component_template.py "
60
+ ). read_text ( encoding = "utf-8" )
61
61
PYSCRIPT_TAG = make_vdom_constructor ("py-script" )
62
62
PYSCRIPT_DEFAULT_CONFIG = {
63
63
"packages" : [
@@ -458,7 +458,7 @@ def vdom_or_component_to_string(
458
458
def render_pyscript_template (file_path : str , uuid : str , root : str ):
459
459
"""Inserts the user's code into our PyScript template using pattern matching."""
460
460
# Create a valid PyScript executor by replacing the template values
461
- executor = PYSCRIPT_TEMPLATE .replace ("UUID" , uuid )
461
+ executor = PYSCRIPT_COMPONENT_TEMPLATE .replace ("UUID" , uuid )
462
462
executor = executor .replace ("return root()" , f"return { root } ()" )
463
463
464
464
# Insert the user code into the template
Original file line number Diff line number Diff line change 1
- from reactpy import component , html
1
+ from reactpy import component , html , use_state
2
2
from reactpy_django .components import python_to_pyscript
3
3
4
4
@@ -8,3 +8,26 @@ def embed():
8
8
{"className" : "embeddable" },
9
9
python_to_pyscript ("./test_app/pyscript/components/child_embed.py" ),
10
10
)
11
+
12
+
13
+ @component
14
+ def toggeable_embed ():
15
+ state , set_state = use_state (False )
16
+
17
+ if not state :
18
+ return html .div (
19
+ {"className" : "embeddable" },
20
+ html .button (
21
+ {"onClick" : lambda x : set_state (not state )},
22
+ "Click to show/hide" ,
23
+ ),
24
+ )
25
+
26
+ return html .div (
27
+ {"className" : "embeddable" },
28
+ html .button (
29
+ {"onClick" : lambda x : set_state (not state )},
30
+ "Click to show/hide" ,
31
+ ),
32
+ python_to_pyscript ("./test_app/pyscript/components/child_embed.py" ),
33
+ )
Original file line number Diff line number Diff line change 8
8
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
9
9
< link rel ="shortcut icon " type ="image/png " href ="{% static 'favicon.ico' %} " />
10
10
< title > ReactPy</ title >
11
+ {% pyscript_static_files %}
11
12
</ head >
12
13
13
14
< body >
@@ -19,6 +20,8 @@ <h1>ReactPy PyScript Test Page</h1>
19
20
< hr >
20
21
{% component "test_app.pyscript.components.child.embed" %}
21
22
< hr >
23
+ {% component "test_app.pyscript.components.child.toggeable_embed" %}
24
+ < hr >
22
25
</ body >
23
26
24
27
</ html >
You can’t perform that action at this time.
0 commit comments