|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
6 | | -from collections.abc import Callable, Iterable, Mapping |
| 6 | +from collections.abc import Iterable, Mapping |
7 | 7 | from typing import TYPE_CHECKING, Any |
8 | 8 |
|
9 | 9 | from reflex import constants |
@@ -49,75 +49,6 @@ def _sort_hooks(hooks: dict[str, VarData | None]): |
49 | 49 | return sorted_hooks |
50 | 50 |
|
51 | 51 |
|
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 | | - |
121 | 52 | class _RenderUtils: |
122 | 53 | """Utility functions for rendering components. |
123 | 54 |
|
@@ -704,16 +635,18 @@ def stateful_component_template( |
704 | 635 | """ |
705 | 636 |
|
706 | 637 |
|
707 | | -def _stateful_components_template(**kwargs) -> str: |
| 638 | +def stateful_components_template(imports: list[_ImportDict], memoized_code: str) -> str: |
708 | 639 | """Template for stateful components. |
709 | 640 |
|
710 | 641 | Args: |
711 | | - **kwargs: Template context variables including code. |
| 642 | + imports: List of import statements. |
| 643 | + memoized_code: Memoized code for stateful components. |
712 | 644 |
|
713 | 645 | Returns: |
714 | 646 | Rendered stateful components code as string. |
715 | 647 | """ |
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}" |
717 | 650 |
|
718 | 651 |
|
719 | 652 | def custom_component_template( |
@@ -799,31 +732,3 @@ def _render_hooks(hooks: dict, memo: list | None = None) -> str: |
799 | 732 | hooks_code += f" {hook}\n" |
800 | 733 |
|
801 | 734 | 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) |
0 commit comments