From 0a1ff389203a8eec9ef90e77703ca5b51414c290 Mon Sep 17 00:00:00 2001 From: benedikt-bartscher <31854409+benedikt-bartscher@users.noreply.github.com> Date: Mon, 22 Apr 2024 20:32:31 +0200 Subject: [PATCH] separate DECORATED_PAGES per reflex app (#3102) --- reflex/app.py | 2 +- reflex/page.py | 11 +++++++---- reflex/testing.py | 8 +++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/reflex/app.py b/reflex/app.py index 4af183367..4b5da0b7d 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -726,7 +726,7 @@ class App(Base): This can move back into `compile_` when py39 support is dropped. """ # Add the @rx.page decorated pages to collect on_load events. - for render, kwargs in DECORATED_PAGES: + for render, kwargs in DECORATED_PAGES[get_config().app_name]: self.add_page(render, **kwargs) def compile_(self, export: bool = False): diff --git a/reflex/page.py b/reflex/page.py index 4ad3bb503..1e5e8603d 100644 --- a/reflex/page.py +++ b/reflex/page.py @@ -2,9 +2,12 @@ from __future__ import annotations -from typing import Any +from collections import defaultdict +from typing import Any, Dict, List -DECORATED_PAGES = [] +from reflex.config import get_config + +DECORATED_PAGES: Dict[str, List] = defaultdict(list) def page( @@ -55,7 +58,7 @@ def page( if on_load: kwargs["on_load"] = on_load - DECORATED_PAGES.append((render_fn, kwargs)) + DECORATED_PAGES[get_config().app_name].append((render_fn, kwargs)) return render_fn @@ -69,6 +72,6 @@ def get_decorated_pages() -> list[dict]: The decorated pages. """ return sorted( - [page_data for render_fn, page_data in DECORATED_PAGES], + [page_data for _, page_data in DECORATED_PAGES[get_config().app_name]], key=lambda x: x["route"], ) diff --git a/reflex/testing.py b/reflex/testing.py index b07071509..944c8e5d8 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -240,13 +240,15 @@ class AppHarness: # ensure config and app are reloaded when testing different app reflex.config.get_config(reload=True) # Save decorated pages before importing the test app module - before_decorated_pages = reflex.app.DECORATED_PAGES.copy() + before_decorated_pages = reflex.app.DECORATED_PAGES[self.app_name].copy() # Ensure the AppHarness test does not skip State assignment due to running via pytest os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None) self.app_module = reflex.utils.prerequisites.get_compiled_app(reload=True) # Save the pages that were added during testing self._decorated_pages = [ - p for p in reflex.app.DECORATED_PAGES if p not in before_decorated_pages + p + for p in reflex.app.DECORATED_PAGES[self.app_name] + if p not in before_decorated_pages ] self.app_instance = self.app_module.app if isinstance(self.app_instance._state_manager, StateManagerRedis): @@ -411,7 +413,7 @@ class AppHarness: # Cleanup decorated pages added during testing for page in self._decorated_pages: - reflex.app.DECORATED_PAGES.remove(page) + reflex.app.DECORATED_PAGES[self.app_name].remove(page) def __exit__(self, *excinfo) -> None: """Contextmanager protocol for `stop()`.