Skip to content

Commit d944643

Browse files
committed
move asgi_component_html function
1 parent 36c6642 commit d944643

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

src/reactpy/asgi/executors/standalone.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
from reactpy import html
1616
from reactpy.asgi.middleware import ReactPyMiddleware
1717
from reactpy.asgi.types import AsgiApp, AsgiHttpApp, AsgiLifespanApp, AsgiWebsocketApp
18-
from reactpy.asgi.utils import import_dotted_path, vdom_head_to_html
18+
from reactpy.asgi.utils import (
19+
asgi_component_html,
20+
import_dotted_path,
21+
vdom_head_to_html,
22+
)
1923
from reactpy.types import ReactPyConfig, RootComponentConstructor, VdomDict
20-
from reactpy.utils import asgi_component_html
2124

2225
_logger = getLogger(__name__)
2326

src/reactpy/asgi/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
from typing import Any
77

88
from reactpy._option import Option
9+
from reactpy.config import (
10+
REACTPY_PATH_PREFIX,
11+
REACTPY_RECONNECT_BACKOFF_MULTIPLIER,
12+
REACTPY_RECONNECT_INTERVAL,
13+
REACTPY_RECONNECT_MAX_INTERVAL,
14+
REACTPY_RECONNECT_MAX_RETRIES,
15+
)
916
from reactpy.types import ReactPyConfig, VdomDict
1017
from reactpy.utils import vdom_to_html
1118

@@ -73,3 +80,21 @@ def process_settings(settings: ReactPyConfig) -> None:
7380
config_object.set_current(settings[setting]) # type: ignore
7481
else:
7582
raise ValueError(f'Unknown ReactPy setting "{setting}".')
83+
84+
85+
def asgi_component_html(element_id: str, class_: str, component_path: str) -> str:
86+
return (
87+
f'<div id="{element_id}" class="{class_}"></div>'
88+
'<script type="module" crossorigin="anonymous">'
89+
f'import {{ mountReactPy }} from "{REACTPY_PATH_PREFIX.current}static/index.js";'
90+
"mountReactPy({"
91+
f' mountElement: document.getElementById("{element_id}"),'
92+
f' pathPrefix: "{REACTPY_PATH_PREFIX.current}",'
93+
f' componentPath: "{component_path}",'
94+
f" reconnectInterval: {REACTPY_RECONNECT_INTERVAL.current},"
95+
f" reconnectMaxInterval: {REACTPY_RECONNECT_MAX_INTERVAL.current},"
96+
f" reconnectMaxRetries: {REACTPY_RECONNECT_MAX_RETRIES.current},"
97+
f" reconnectBackoffMultiplier: {REACTPY_RECONNECT_BACKOFF_MULTIPLIER.current},"
98+
"});"
99+
"</script>"
100+
)

src/reactpy/templatetags/jinja.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
from jinja2_simple_tags import StandaloneTag
55

6-
from reactpy.pyscript.utils import (
7-
pyscript_component_html,
8-
pyscript_setup_html,
9-
)
10-
from reactpy.utils import asgi_component_html
6+
from reactpy.asgi.utils import asgi_component_html
7+
from reactpy.pyscript.utils import pyscript_component_html, pyscript_setup_html
118

129

1310
class Jinja(StandaloneTag): # type: ignore

src/reactpy/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from lxml import etree
99
from lxml.html import fromstring, tostring
1010

11-
from reactpy import config
1211
from reactpy.core.vdom import vdom as make_vdom
1312
from reactpy.types import ComponentType, VdomDict
1413

@@ -314,21 +313,3 @@ def _vdom_attr_to_html_str(key: str, value: Any) -> tuple[str, str]:
314313

315314
# Pattern for delimitting camelCase names (e.g. camelCase to camel-case)
316315
_CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
317-
318-
319-
def asgi_component_html(element_id: str, class_: str, component_path: str) -> str:
320-
return (
321-
f'<div id="{element_id}" class="{class_}"></div>'
322-
'<script type="module" crossorigin="anonymous">'
323-
f'import {{ mountReactPy }} from "{config.REACTPY_PATH_PREFIX.current}static/index.js";'
324-
"mountReactPy({"
325-
f' mountElement: document.getElementById("{element_id}"),'
326-
f' pathPrefix: "{config.REACTPY_PATH_PREFIX.current}",'
327-
f' componentPath: "{component_path}",'
328-
f" reconnectInterval: {config.REACTPY_RECONNECT_INTERVAL.current},"
329-
f" reconnectMaxInterval: {config.REACTPY_RECONNECT_MAX_INTERVAL.current},"
330-
f" reconnectMaxRetries: {config.REACTPY_RECONNECT_MAX_RETRIES.current},"
331-
f" reconnectBackoffMultiplier: {config.REACTPY_RECONNECT_BACKOFF_MULTIPLIER.current},"
332-
"});"
333-
"</script>"
334-
)

0 commit comments

Comments
 (0)