Skip to content

Commit 18e65c9

Browse files
committed
fix stateful_components_template
1 parent 1e6a839 commit 18e65c9

File tree

3 files changed

+8
-103
lines changed

3 files changed

+8
-103
lines changed

reflex/compiler/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def get_shared_components_recursive(component: BaseComponent):
456456
if rendered_components:
457457
_apply_common_imports(all_imports)
458458

459-
return templates.STATEFUL_COMPONENTS.render(
459+
return templates.stateful_components_template(
460460
imports=utils.compile_imports(all_imports),
461461
memoized_code="\n".join(rendered_components),
462462
)

reflex/compiler/templates.py

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import json
6-
from collections.abc import Callable, Iterable, Mapping
6+
from collections.abc import Iterable, Mapping
77
from typing import TYPE_CHECKING, Any
88

99
from reflex import constants
@@ -49,75 +49,6 @@ def _sort_hooks(hooks: dict[str, VarData | None]):
4949
return sorted_hooks
5050

5151

52-
class ReflexTemplateRenderer:
53-
"""Template renderer using f-string formatting."""
54-
55-
def __init__(self) -> None:
56-
"""Initialize template renderer with helper functions."""
57-
self.filters = {
58-
"json_dumps": json_dumps,
59-
"react_setter": lambda state: f"set{state.capitalize()}",
60-
"var_name": format_state_name,
61-
}
62-
63-
self.const = {
64-
"socket": constants.CompileVars.SOCKET,
65-
"result": constants.CompileVars.RESULT,
66-
"router": constants.CompileVars.ROUTER,
67-
"event_endpoint": constants.Endpoint.EVENT.name,
68-
"events": constants.CompileVars.EVENTS,
69-
"state": constants.CompileVars.STATE,
70-
"final": constants.CompileVars.FINAL,
71-
"processing": constants.CompileVars.PROCESSING,
72-
"initial_result": {
73-
constants.CompileVars.STATE: None,
74-
constants.CompileVars.EVENTS: [],
75-
constants.CompileVars.FINAL: True,
76-
constants.CompileVars.PROCESSING: False,
77-
},
78-
"color_mode": constants.ColorMode.NAME,
79-
"resolved_color_mode": constants.ColorMode.RESOLVED_NAME,
80-
"toggle_color_mode": constants.ColorMode.TOGGLE,
81-
"set_color_mode": constants.ColorMode.SET,
82-
"use_color_mode": constants.ColorMode.USE,
83-
"hydrate": constants.CompileVars.HYDRATE,
84-
"on_load_internal": constants.CompileVars.ON_LOAD_INTERNAL,
85-
"update_vars_internal": constants.CompileVars.UPDATE_VARS_INTERNAL,
86-
"frontend_exception_state": constants.CompileVars.FRONTEND_EXCEPTION_STATE_FULL,
87-
"hook_position": constants.Hooks.HookPosition,
88-
}
89-
90-
91-
class Template:
92-
"""Template class for f-string based rendering."""
93-
94-
def __init__(self, template_func: Callable[..., str]):
95-
"""Initialize with a template function.
96-
97-
Args:
98-
template_func: Function that takes kwargs and returns rendered string.
99-
"""
100-
self.template_func = template_func
101-
102-
def render(self, **kwargs) -> str:
103-
"""Render the template with provided context.
104-
105-
Args:
106-
**kwargs: Template context variables.
107-
108-
Returns:
109-
Rendered template string.
110-
"""
111-
renderer = ReflexTemplateRenderer()
112-
# Merge renderer utilities into context
113-
context = {
114-
"const": renderer.const,
115-
**renderer.filters,
116-
**kwargs,
117-
}
118-
return self.template_func(**context)
119-
120-
12152
class _RenderUtils:
12253
"""Utility functions for rendering components.
12354
@@ -704,16 +635,18 @@ def stateful_component_template(
704635
"""
705636

706637

707-
def _stateful_components_template(**kwargs) -> str:
638+
def stateful_components_template(imports: list[_ImportDict], memoized_code: str) -> str:
708639
"""Template for stateful components.
709640
710641
Args:
711-
**kwargs: Template context variables including code.
642+
imports: List of import statements.
643+
memoized_code: Memoized code for stateful components.
712644
713645
Returns:
714646
Rendered stateful components code as string.
715647
"""
716-
return kwargs.get("code", "")
648+
imports_str = "\n".join([_RenderUtils.get_import(imp) for imp in imports])
649+
return f"{imports_str}\n{memoized_code}"
717650

718651

719652
def custom_component_template(
@@ -799,31 +732,3 @@ def _render_hooks(hooks: dict, memo: list | None = None) -> str:
799732
hooks_code += f" {hook}\n"
800733

801734
return hooks_code
802-
803-
804-
# Template instances
805-
class TemplateFunction:
806-
"""Wrapper for template functions to match Jinja Template interface."""
807-
808-
def __init__(self, func: Callable[..., str]):
809-
"""Initialize with template function.
810-
811-
Args:
812-
func: Template function to wrap.
813-
"""
814-
self.func = func
815-
816-
def render(self, **kwargs) -> str:
817-
"""Render template with kwargs.
818-
819-
Args:
820-
**kwargs: Template context variables.
821-
822-
Returns:
823-
Rendered template as string.
824-
"""
825-
return self.func(**kwargs)
826-
827-
828-
# Code to render StatefulComponent to an external file to be shared
829-
STATEFUL_COMPONENTS = TemplateFunction(_stateful_components_template)

reflex/components/dynamic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def make_component(component: Component) -> str:
111111
else:
112112
imports[lib] = names
113113

114-
module_code_lines = templates.STATEFUL_COMPONENTS.render(
114+
module_code_lines = templates.stateful_components_template(
115115
imports=utils.compile_imports(imports),
116116
memoized_code="\n".join(rendered_components),
117117
).splitlines()[1:]

0 commit comments

Comments
 (0)