Skip to content

Commit c8a1740

Browse files
committed
Use the ReactPy core's PyScript hooks (new PR, coming soon in v2.0.0b12), and mark Django template tag output as HTML-safe
1 parent 54e44f4 commit c8a1740

File tree

10 files changed

+117
-335
lines changed

10 files changed

+117
-335
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ artifacts = ["/src/reactpy_django/static/"]
6767

6868
[tool.hatch.metadata]
6969
license-files = { paths = ["LICENSE.md"] }
70+
allow-direct-references = true
7071

7172
[tool.hatch.envs.default]
7273
installer = "uv"

src/reactpy_django/pyscript/__init__.py

Whitespace-only changes.

src/reactpy_django/pyscript/component_template.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/reactpy_django/pyscript/layout_handler.py

Lines changed: 0 additions & 127 deletions
This file was deleted.

src/reactpy_django/pyscript/utils.py

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<div id="pyscript-{{pyscript_uuid}}" class="pyscript" data-uuid="{{pyscript_uuid}}">{{pyscript_initial_html}}</div>
2-
<py-script async>{{pyscript_executor}}</py-script>
2+
<script type="py">{{pyscript_executor}}</script>

src/reactpy_django/templates/reactpy/pyscript_setup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
<link rel="stylesheet" href="{% static 'reactpy_django/pyscript-hide-debug.css' %}" />
66
{% endif %}
77
<script type="module" async crossorigin="anonymous" src="{% static 'reactpy_django/pyscript/core.js' %}"></script>
8-
<py-script async config='{{pyscript_config}}'>{{pyscript_layout_handler}}</py-script>
8+
<script type="py" config='{{pyscript_config}}'>{{pyscript_layout_handler}}</script>

src/reactpy_django/templatetags/reactpy.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
from uuid import uuid4
66

77
from django import template
8+
from django.templatetags.static import static
89
from django.urls import NoReverseMatch, reverse
10+
from django.utils.safestring import mark_safe
11+
from reactpy.executors.pyscript.utils import PYSCRIPT_LAYOUT_HANDLER, extend_pyscript_config, pyscript_executor_html
912

1013
from reactpy_django import config as reactpy_config
1114
from reactpy_django.exceptions import (
@@ -15,8 +18,8 @@
1518
InvalidHostError,
1619
OfflineComponentMissingError,
1720
)
18-
from reactpy_django.pyscript.utils import PYSCRIPT_LAYOUT_HANDLER, extend_pyscript_config, render_pyscript_template
1921
from reactpy_django.utils import (
22+
fetch_cached_python_file,
2023
prerender_component,
2124
reactpy_to_string,
2225
save_component_params,
@@ -178,11 +181,10 @@ def component(
178181
"reactpy_reconnect_max_interval": reactpy_config.REACTPY_RECONNECT_MAX_INTERVAL,
179182
"reactpy_reconnect_backoff_multiplier": reactpy_config.REACTPY_RECONNECT_BACKOFF_MULTIPLIER,
180183
"reactpy_reconnect_max_retries": reactpy_config.REACTPY_RECONNECT_MAX_RETRIES,
181-
"reactpy_prerender_html": _prerender_html,
182-
"reactpy_offline_html": _offline_html,
184+
"reactpy_prerender_html": mark_safe(_prerender_html),
185+
"reactpy_offline_html": mark_safe(_offline_html),
183186
}
184187

185-
186188
@register.inclusion_tag("reactpy/pyscript_component.html", takes_context=True)
187189
def pyscript_component(
188190
context: template.RequestContext,
@@ -208,12 +210,12 @@ def pyscript_component(
208210
uuid = uuid4().hex
209211
request: HttpRequest | None = context.get("request")
210212
initial = reactpy_to_string(initial, request=request, uuid=uuid)
211-
executor = render_pyscript_template(file_paths, uuid, root)
213+
executor = pyscript_executor_html(file_paths, uuid, root, fetch_cached_python_file)
212214

213215
return {
214-
"pyscript_executor": executor,
216+
"pyscript_executor": mark_safe(executor),
215217
"pyscript_uuid": uuid,
216-
"pyscript_initial_html": initial,
218+
"pyscript_initial_html": mark_safe(initial),
217219
}
218220

219221

@@ -239,8 +241,12 @@ def pyscript_setup(
239241
from reactpy_django.config import DJANGO_DEBUG
240242

241243
return {
242-
"pyscript_config": extend_pyscript_config(extra_py, extra_js, config),
243-
"pyscript_layout_handler": PYSCRIPT_LAYOUT_HANDLER,
244+
"pyscript_config": mark_safe(
245+
extend_pyscript_config(
246+
extra_py, extra_js, config, {static("reactpy_django/morphdom/morphdom-esm.js"): "morphdom"}
247+
)
248+
),
249+
"pyscript_layout_handler": mark_safe(PYSCRIPT_LAYOUT_HANDLER),
244250
"django_debug": DJANGO_DEBUG,
245251
}
246252

src/reactpy_django/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from fnmatch import fnmatch
1414
from functools import wraps
1515
from importlib import import_module
16+
from pathlib import Path
1617
from typing import TYPE_CHECKING, Any, Callable
1718
from uuid import UUID, uuid4
1819

@@ -531,3 +532,22 @@ def del_html_head_body_transform(vdom: VdomDict) -> VdomDict:
531532
if vdom["tagName"] in {"html", "body", "head"}:
532533
return {"tagName": "", "children": vdom.setdefault("children", [])}
533534
return vdom
535+
536+
537+
def fetch_cached_python_file(file_path: str, minify: bool = True) -> str:
538+
from reactpy.executors.pyscript.utils import minify_python
539+
540+
from reactpy_django.config import REACTPY_CACHE
541+
542+
# Try to get user code from cache
543+
cache_key = create_cache_key("pyscript", file_path)
544+
last_modified_time = os.stat(file_path).st_mtime
545+
file_contents: str = caches[REACTPY_CACHE].get(cache_key, version=int(last_modified_time))
546+
if file_contents:
547+
return file_contents
548+
549+
file_contents = Path(file_path).read_text(encoding="utf-8").strip()
550+
if minify:
551+
file_contents = minify_python(file_contents)
552+
caches[REACTPY_CACHE].set(cache_key, file_contents, version=int(last_modified_time))
553+
return file_contents

0 commit comments

Comments
 (0)