Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate DECORATED_PAGES per reflex app #3102

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading