Skip to content

Commit

Permalink
separate DECORATED_PAGES per reflex app
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Apr 17, 2024
1 parent 9b7e5f4 commit 7388487
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def _apply_decorated_pages(self):
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):
Expand Down
11 changes: 7 additions & 4 deletions reflex/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -55,7 +58,7 @@ def decorator(render_fn):
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

Expand All @@ -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"],
)
8 changes: 5 additions & 3 deletions reflex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,15 @@ def _initialize_app(self):
# 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):
Expand Down Expand Up @@ -409,7 +411,7 @@ def stop(self) -> None:

# 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()`.
Expand Down

0 comments on commit 7388487

Please sign in to comment.