Skip to content

Commit 4066adb

Browse files
committed
import encapsulation
1 parent fc71864 commit 4066adb

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/reactpy_django/pyscript/component_template.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# pylint: disable=used-before-assignment
21
from typing import TYPE_CHECKING
32

43
if TYPE_CHECKING:

src/reactpy_django/pyscript/layout_handler.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# mypy: disable-error-code=attr-defined
22
import asyncio
3-
from typing import Callable
4-
5-
import js
6-
from jsonpointer import set_pointer
7-
from pyodide.ffi.wrappers import add_event_listener
8-
from reactpy.core.layout import Layout
9-
from reactpy.types import ComponentType
103

114

125
class ReactPyLayoutHandler:
@@ -23,13 +16,17 @@ def __init__(self, uuid):
2316
@staticmethod
2417
def apply_update(update, root_model):
2518
"""Apply an update ReactPy's internal DOM model."""
19+
from jsonpointer import set_pointer
20+
2621
if update["path"]:
2722
set_pointer(root_model, update["path"], update["model"])
2823
else:
2924
root_model.update(update["model"])
3025

3126
def render(self, layout, model):
3227
"""Submit ReactPy's internal DOM model into the HTML DOM."""
28+
import js
29+
3330
container = js.document.getElementById(f"pyscript-{self.uuid}")
3431

3532
# FIXME: The current implementation completely recreates the DOM on every render.
@@ -39,6 +36,8 @@ def render(self, layout, model):
3936

4037
def build_element_tree(self, layout, parent, model):
4138
"""Recursively build an element tree, starting from the root component."""
39+
import js
40+
4241
if isinstance(model, str):
4342
parent.appendChild(js.document.createTextNode(model))
4443
elif isinstance(model, dict):
@@ -74,6 +73,8 @@ def build_element_tree(self, layout, parent, model):
7473
def create_event_handler(layout, element, event_name, event_handler_model):
7574
"""Create an event handler for an element. This function is used as an
7675
adapter between ReactPy and browser events."""
76+
from pyodide.ffi.wrappers import add_event_listener
77+
7778
target = event_handler_model["target"]
7879

7980
def event_handler(*args):
@@ -89,6 +90,8 @@ def delete_old_workspaces():
8990
"""To prevent memory leaks, we must delete all user generated Python code
9091
whe it is no longer on the page. To do this, we compare what UUIDs exist on
9192
the DOM, versus what UUIDs exist within the PyScript global interpreter."""
93+
import js
94+
9295
dom_workspaces = js.document.querySelectorAll(".pyscript")
9396
dom_uuids = {element.dataset.uuid for element in dom_workspaces}
9497
python_uuids = {
@@ -115,8 +118,10 @@ def delete_old_workspaces():
115118
f"Warning: Could not auto delete PyScript workspace {workspace_name}"
116119
)
117120

118-
async def run(self, workspace_function: Callable[[], ComponentType]):
121+
async def run(self, workspace_function):
119122
"""Run the layout handler. This function is main executor for all user generated code."""
123+
from reactpy.core.layout import Layout
124+
120125
self.delete_old_workspaces()
121126
root_model: dict = {}
122127

tests/test_app/pyscript/components/multifile_parent.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# pylint: disable=used-before-assignment
21
from typing import TYPE_CHECKING
32

43
from reactpy import component, html

0 commit comments

Comments
 (0)