Skip to content

Commit 2aa49a9

Browse files
authored
do not force import event and page (#5559)
* do not force import event and page * post merge
1 parent 3d2e060 commit 2aa49a9

File tree

4 files changed

+118
-29
lines changed

4 files changed

+118
-29
lines changed

pyi_hashes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"reflex/__init__.pyi": "8f9482b205f5a33a59f748006ded637b",
2+
"reflex/__init__.pyi": "e9f08e115b1e1dd89beabacade84a589",
33
"reflex/components/__init__.pyi": "ac05995852baa81062ba3d18fbc489fb",
44
"reflex/components/base/__init__.pyi": "16e47bf19e0d62835a605baa3d039c5a",
55
"reflex/components/base/app_wrap.pyi": "ae600e2cc9d70f2ce613bdd6c1da3b16",

reflex/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,11 @@
8484

8585
from __future__ import annotations
8686

87-
from types import ModuleType
88-
from typing import Any
89-
9087
from reflex.utils import (
9188
compat, # for side-effects
9289
lazy_loader,
9390
)
9491

95-
from .event import event as event
96-
97-
# import this here explicitly to avoid returning the page module since page attr has the
98-
# same name as page module(page.py)
99-
from .page import page as page
100-
10192
# Remove the `compat` name from the namespace, it was imported for side-effects only.
10293
del compat
10394

@@ -301,6 +292,7 @@
301292
"constants": ["Env"],
302293
"constants.colors": ["Color"],
303294
"event": [
295+
"event",
304296
"EventChain",
305297
"EventHandler",
306298
"call_script",
@@ -331,6 +323,7 @@
331323
],
332324
"middleware": ["middleware", "Middleware"],
333325
"model": ["asession", "session", "Model"],
326+
"page": ["page"],
334327
"state": [
335328
"var",
336329
"ComponentState",

reflex/event.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dataclasses
44
import inspect
5+
import sys
56
import types
67
import urllib.parse
78
from base64 import b64encode
@@ -403,7 +404,7 @@ def __init__(self, fn: Callable[..., EventSpec] | None = None, **kwargs):
403404
404405
Args:
405406
fn: The function to decorate.
406-
**kwargs: The kwargs to pass to pydantic initializer
407+
**kwargs: The kwargs to pass to the EventSpec constructor.
407408
"""
408409
if fn is not None:
409410
default_event_spec = fn()
@@ -1243,19 +1244,6 @@ def download(
12431244
)
12441245

12451246

1246-
# This function seems unused. Check if we still need it. If not, remove in 0.7.0
1247-
def _callback_arg_spec(eval_result: Any):
1248-
"""ArgSpec for call_script callback function.
1249-
1250-
Args:
1251-
eval_result: The result of the javascript execution.
1252-
1253-
Returns:
1254-
Args for the callback function
1255-
"""
1256-
return [eval_result]
1257-
1258-
12591247
def call_script(
12601248
javascript_code: str | Var[str],
12611249
callback: "EventType[Any] | None" = None,
@@ -2197,7 +2185,9 @@ def __call__(self, *args: Var) -> Any:
21972185
class EventNamespace:
21982186
"""A namespace for event related classes."""
21992187

2188+
# Core Event Classes
22002189
Event = Event
2190+
EventActionsMixin = EventActionsMixin
22012191
EventHandler = EventHandler
22022192
EventSpec = EventSpec
22032193
CallableEventSpec = CallableEventSpec
@@ -2206,8 +2196,43 @@ class EventNamespace:
22062196
LiteralEventVar = LiteralEventVar
22072197
EventChainVar = EventChainVar
22082198
LiteralEventChainVar = LiteralEventChainVar
2209-
EventType = EventType
22102199
EventCallback = EventCallback
2200+
LambdaEventCallback = LambdaEventCallback
2201+
2202+
# Javascript Event Classes
2203+
JavascriptHTMLInputElement = JavascriptHTMLInputElement
2204+
JavascriptInputEvent = JavascriptInputEvent
2205+
JavascriptKeyboardEvent = JavascriptKeyboardEvent
2206+
JavascriptMouseEvent = JavascriptMouseEvent
2207+
JavascriptPointerEvent = JavascriptPointerEvent
2208+
2209+
# Type Info Classes
2210+
KeyInputInfo = KeyInputInfo
2211+
MouseEventInfo = MouseEventInfo
2212+
PointerEventInfo = PointerEventInfo
2213+
IdentityEventReturn = IdentityEventReturn
2214+
2215+
# File Upload
2216+
FileUpload = FileUpload
2217+
2218+
# Type Aliases
2219+
EventType = EventType
2220+
LAMBDA_OR_STATE = LAMBDA_OR_STATE
2221+
BASIC_EVENT_TYPES = BASIC_EVENT_TYPES
2222+
IndividualEventType = IndividualEventType
2223+
2224+
# Constants
2225+
BACKGROUND_TASK_MARKER = BACKGROUND_TASK_MARKER
2226+
_EVENT_FIELDS = _EVENT_FIELDS
2227+
upload_files = upload_files
2228+
stop_propagation = stop_propagation
2229+
prevent_default = prevent_default
2230+
2231+
# Private/Internal Functions
2232+
_values_returned_from_event = staticmethod(_values_returned_from_event)
2233+
_check_event_args_subclass_of_callback = staticmethod(
2234+
_check_event_args_subclass_of_callback
2235+
)
22112236

22122237
@overload
22132238
def __new__(
@@ -2359,10 +2384,20 @@ def wrapper(
23592384
check_fn_match_arg_spec = staticmethod(check_fn_match_arg_spec)
23602385
resolve_annotation = staticmethod(resolve_annotation)
23612386
parse_args_spec = staticmethod(parse_args_spec)
2387+
unwrap_var_annotation = staticmethod(unwrap_var_annotation)
2388+
get_fn_signature = staticmethod(get_fn_signature)
2389+
2390+
# Event Spec Functions
23622391
passthrough_event_spec = staticmethod(passthrough_event_spec)
23632392
input_event = staticmethod(input_event)
2393+
int_input_event = staticmethod(int_input_event)
2394+
float_input_event = staticmethod(float_input_event)
2395+
checked_input_event = staticmethod(checked_input_event)
23642396
key_event = staticmethod(key_event)
2397+
pointer_event_spec = staticmethod(pointer_event_spec)
23652398
no_args_event_spec = staticmethod(no_args_event_spec)
2399+
2400+
# Server Side Events
23662401
server_side = staticmethod(server_side)
23672402
redirect = staticmethod(redirect)
23682403
console_log = staticmethod(console_log)
@@ -2383,6 +2418,9 @@ def wrapper(
23832418
call_script = staticmethod(call_script)
23842419
call_function = staticmethod(call_function)
23852420
run_script = staticmethod(run_script)
2421+
__file__ = __file__
23862422

23872423

23882424
event = EventNamespace
2425+
event.event = event # pyright: ignore[reportAttributeAccessIssue]
2426+
sys.modules[__name__] = event # pyright: ignore[reportArgumentType]

reflex/page.py

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
from __future__ import annotations
44

5+
import sys
56
from collections import defaultdict
6-
from collections.abc import Callable
7-
from typing import Any
7+
from typing import TYPE_CHECKING
88

9-
from reflex.config import get_config
10-
from reflex.event import EventType
9+
if TYPE_CHECKING:
10+
from collections.abc import Callable
11+
from typing import Any
12+
13+
from reflex.event import EventType
1114

1215
DECORATED_PAGES: dict[str, list] = defaultdict(list)
1316

@@ -42,6 +45,7 @@ def page(
4245
Returns:
4346
The decorated function.
4447
"""
48+
from reflex.config import get_config
4549

4650
def decorator(render_fn: Callable):
4751
kwargs = {}
@@ -65,3 +69,57 @@ def decorator(render_fn: Callable):
6569
return render_fn
6670

6771
return decorator
72+
73+
74+
class PageNamespace:
75+
"""A namespace for page names."""
76+
77+
DECORATED_PAGES = DECORATED_PAGES
78+
79+
def __new__(
80+
cls,
81+
route: str | None = None,
82+
title: str | None = None,
83+
image: str | None = None,
84+
description: str | None = None,
85+
meta: list[Any] | None = None,
86+
script_tags: list[Any] | None = None,
87+
on_load: EventType[()] | None = None,
88+
):
89+
"""Decorate a function as a page.
90+
91+
rx.App() will automatically call add_page() for any method decorated with page
92+
when App.compile is called.
93+
94+
All defaults are None because they will use the one from add_page().
95+
96+
Note: the decorated functions still need to be imported.
97+
98+
Args:
99+
route: The route to reach the page.
100+
title: The title of the page.
101+
image: The favicon of the page.
102+
description: The description of the page.
103+
meta: Additional meta to add to the page.
104+
on_load: The event handler(s) called when the page load.
105+
script_tags: scripts to attach to the page
106+
107+
Returns:
108+
The decorated function.
109+
"""
110+
return page(
111+
route=route,
112+
title=title,
113+
image=image,
114+
description=description,
115+
meta=meta,
116+
script_tags=script_tags,
117+
on_load=on_load,
118+
)
119+
120+
page = staticmethod(page)
121+
__file__ = __file__
122+
123+
124+
page_namespace = PageNamespace
125+
sys.modules[__name__] = page_namespace # pyright: ignore[reportArgumentType]

0 commit comments

Comments
 (0)