Skip to content

Commit d324b25

Browse files
authored
disable stateful app detection (#5583)
* disable stateful app detection * disable state for test tailwind * wghat
1 parent aa7c48b commit d324b25

File tree

6 files changed

+41
-92
lines changed

6 files changed

+41
-92
lines changed

reflex/app.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ class App(MiddlewareMixin, LifespanMixin):
400400
# The state class to use for the app.
401401
_state: type[BaseState] | None = None
402402

403+
# Whether to enable state for the app. If False, the app will not use state.
404+
enable_state: bool = True
405+
403406
# Class to manage many client states.
404407
_state_manager: StateManager | None = None
405408

@@ -480,7 +483,8 @@ def __post_init__(self):
480483
if issubclass(clz, AppMixin):
481484
clz._init_mixin(self)
482485

483-
self._setup_state()
486+
if self.enable_state:
487+
self._enable_state()
484488

485489
# Set up the admin dash.
486490
self._setup_admin_dash()
@@ -495,7 +499,7 @@ def _enable_state(self) -> None:
495499
"""Enable state for the app."""
496500
if not self._state:
497501
self._state = State
498-
self._setup_state()
502+
self._setup_state()
499503

500504
def _setup_state(self) -> None:
501505
"""Set up the state for the app.
@@ -847,14 +851,10 @@ def _compile_page(self, route: str, save_page: bool = True):
847851
save_page: If True, the compiled page is saved to self._pages.
848852
"""
849853
n_states_before = len(all_base_state_classes)
850-
component, enable_state = compiler.compile_unevaluated_page(
851-
route, self._unevaluated_pages[route], self._state, self.style, self.theme
854+
component = compiler.compile_unevaluated_page(
855+
route, self._unevaluated_pages[route], self.style, self.theme
852856
)
853857

854-
# Indicate that the app should use state.
855-
if enable_state:
856-
self._enable_state()
857-
858858
# Indicate that evaluating this page creates one or more state classes.
859859
if len(all_base_state_classes) > n_states_before:
860860
self._stateful_pages[route] = None
@@ -1146,7 +1146,6 @@ def get_compilation_time() -> str:
11461146
for route in stateful_pages:
11471147
console.debug(f"BE Evaluating stateful page: {route}")
11481148
self._compile_page(route, save_page=False)
1149-
self._enable_state()
11501149
self._add_optional_endpoints()
11511150
return
11521151

@@ -1345,8 +1344,6 @@ def memoized_toast_provider():
13451344
for route, component in zip(self._pages, page_components, strict=True):
13461345
ExecutorSafeFunctions.COMPONENTS[route] = component
13471346

1348-
ExecutorSafeFunctions.STATE = self._state
1349-
13501347
modify_files_tasks: list[tuple[str, str, Callable[[str], str]]] = []
13511348

13521349
with console.timing("Compile to Javascript"), executor as executor:

reflex/compiler/compiler.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,11 @@ def _compile_contexts(state: type[BaseState] | None, theme: Component | None) ->
145145
)
146146

147147

148-
def _compile_page(
149-
component: BaseComponent,
150-
state: type[BaseState] | None,
151-
) -> str:
152-
"""Compile the component given the app state.
148+
def _compile_page(component: BaseComponent) -> str:
149+
"""Compile the component.
153150
154151
Args:
155152
component: The component to compile.
156-
state: The app state.
157153
158154
Returns:
159155
The compiled component.
@@ -163,15 +159,12 @@ def _compile_page(
163159
imports = utils.compile_imports(imports)
164160

165161
# Compile the code to render the component.
166-
kwargs = {"state_name": state.get_name()} if state is not None else {}
167-
168162
return templates.PAGE.render(
169163
imports=imports,
170164
dynamic_imports=component._get_all_dynamic_imports(),
171165
custom_codes=component._get_all_custom_code(),
172166
hooks=component._get_all_hooks(),
173167
render=component.render(),
174-
**kwargs,
175168
)
176169

177170

@@ -556,15 +549,12 @@ def compile_contexts(
556549
return output_path, _compile_contexts(state, theme)
557550

558551

559-
def compile_page(
560-
path: str, component: BaseComponent, state: type[BaseState] | None
561-
) -> tuple[str, str]:
552+
def compile_page(path: str, component: BaseComponent) -> tuple[str, str]:
562553
"""Compile a single page.
563554
564555
Args:
565556
path: The path to compile the page to.
566557
component: The component to compile.
567-
state: The app state.
568558
569559
Returns:
570560
The path and code of the compiled page.
@@ -573,7 +563,7 @@ def compile_page(
573563
output_path = utils.get_page_path(path)
574564

575565
# Add the style to the component.
576-
code = _compile_page(component, state)
566+
code = _compile_page(component)
577567
return output_path, code
578568

579569

@@ -788,16 +778,14 @@ def into_component(component: Component | ComponentCallable) -> Component:
788778
def compile_unevaluated_page(
789779
route: str,
790780
page: UnevaluatedPage,
791-
state: type[BaseState] | None = None,
792781
style: ComponentStyle | None = None,
793782
theme: Component | None = None,
794-
) -> tuple[Component, bool]:
783+
) -> Component:
795784
"""Compiles an uncompiled page into a component and adds meta information.
796785
797786
Args:
798787
route: The route of the page.
799788
page: The uncompiled page object.
800-
state: The state of the app.
801789
style: The style of the page.
802790
theme: The theme of the page.
803791
@@ -813,21 +801,6 @@ def compile_unevaluated_page(
813801

814802
component._add_style_recursive(style or {}, theme)
815803

816-
enable_state = False
817-
# Ensure state is enabled if this page uses state.
818-
if state is None:
819-
if page.on_load or component._has_stateful_event_triggers():
820-
enable_state = True
821-
else:
822-
for var in component._get_vars(include_children=True):
823-
var_data = var._get_all_var_data()
824-
if not var_data:
825-
continue
826-
if not var_data.state:
827-
continue
828-
enable_state = True
829-
break
830-
831804
from reflex.utils.format import make_default_page_title
832805

833806
component = Fragment.create(component)
@@ -856,7 +829,7 @@ def compile_unevaluated_page(
856829
e.add_note(f"Happened while evaluating page {route!r}")
857830
raise
858831
else:
859-
return component, enable_state
832+
return component
860833

861834

862835
class ExecutorSafeFunctions:
@@ -886,7 +859,6 @@ class ExecutorSafeFunctions:
886859

887860
COMPONENTS: dict[str, BaseComponent] = {}
888861
UNCOMPILED_PAGES: dict[str, UnevaluatedPage] = {}
889-
STATE: type[BaseState] | None = None
890862

891863
@classmethod
892864
def compile_page(cls, route: str) -> tuple[str, str]:
@@ -898,7 +870,7 @@ def compile_page(cls, route: str) -> tuple[str, str]:
898870
Returns:
899871
The path and code of the compiled page.
900872
"""
901-
return compile_page(route, cls.COMPONENTS[route], cls.STATE)
873+
return compile_page(route, cls.COMPONENTS[route])
902874

903875
@classmethod
904876
def compile_unevaluated_page(
@@ -917,10 +889,10 @@ def compile_unevaluated_page(
917889
Returns:
918890
The route, compiled component, and compiled page.
919891
"""
920-
component, enable_state = compile_unevaluated_page(
921-
route, cls.UNCOMPILED_PAGES[route], cls.STATE, style, theme
892+
component = compile_unevaluated_page(
893+
route, cls.UNCOMPILED_PAGES[route], style, theme
922894
)
923-
return route, component, compile_page(route, component, cls.STATE)
895+
return route, component, compile_page(route, component)
924896

925897
@classmethod
926898
def compile_theme(cls, style: ComponentStyle | None) -> tuple[str, str]:

tests/benchmarks/test_compilation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def import_templates():
1212
def test_compile_page(evaluated_page: Component, benchmark: BenchmarkFixture):
1313
import_templates()
1414

15-
benchmark(lambda: _compile_page(evaluated_page, None))
15+
benchmark(lambda: _compile_page(evaluated_page))
1616

1717

1818
def test_compile_stateful(evaluated_page: Component, benchmark: BenchmarkFixture):

tests/integration/test_tailwind.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def index():
4646
assets.mkdir(exist_ok=True)
4747
stylesheet = assets / "test_styles.css"
4848
stylesheet.write_text(".external { color: rgba(0, 0, 255, 0.5) }")
49-
app = rx.App(style={"font_family": "monospace"}, stylesheets=[stylesheet.name])
49+
app = rx.App(
50+
style={"font_family": "monospace"},
51+
stylesheets=[stylesheet.name],
52+
enable_state=False,
53+
)
5054
app.add_page(index)
5155
if not tailwind_version:
5256
config = rx.config.get_config()

tests/integration/tests_playwright/test_stateless_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def StatelessApp():
1616
def index():
1717
return rx.heading("This is a stateless app")
1818

19-
app = rx.App()
19+
app = rx.App(enable_state=False)
2020
app.add_page(index)
2121

2222

0 commit comments

Comments
 (0)