separate DECORATED_PAGES per reflex app (#3102)

This commit is contained in:
benedikt-bartscher 2024-04-22 20:32:31 +02:00 committed by GitHub
parent 041abb39c8
commit 0a1ff38920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 8 deletions

View File

@ -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):

View File

@ -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"],
)

View File

@ -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()`.