Bläddra i källkod

rename rx.route decorator (#1442)

Thomas Brandého 1 år sedan
förälder
incheckning
3faad315ca
4 ändrade filer med 82 tillägg och 23 borttagningar
  1. 1 0
      reflex/__init__.py
  2. 4 2
      reflex/app.py
  3. 66 0
      reflex/page.py
  4. 11 21
      reflex/route.py

+ 1 - 0
reflex/__init__.py

@@ -36,6 +36,7 @@ from .event import window_alert as window_alert
 from .middleware import Middleware as Middleware
 from .model import Model as Model
 from .model import session as session
+from .page import page as page
 from .route import route as route
 from .state import ComputedVar as var
 from .state import State as State

+ 4 - 2
reflex/app.py

@@ -35,8 +35,10 @@ from reflex.config import get_config
 from reflex.event import Event, EventHandler, EventSpec
 from reflex.middleware import HydrateMiddleware, Middleware
 from reflex.model import Model
+from reflex.page import (
+    DECORATED_PAGES,
+)
 from reflex.route import (
-    DECORATED_ROUTES,
     catchall_in_route,
     catchall_prefix,
     get_route_args,
@@ -468,7 +470,7 @@ class App(Base):
         task = progress.add_task("Compiling: ", total=len(self.pages))
 
         # TODO: include all work done in progress indicator, not just self.pages
-        for render, kwargs in DECORATED_ROUTES:
+        for render, kwargs in DECORATED_PAGES:
             self.add_page(render, **kwargs)
 
         # Get the env mode.

+ 66 - 0
reflex/page.py

@@ -0,0 +1,66 @@
+"""The page decorator and associated variables and functions."""
+
+from __future__ import annotations
+
+from typing import List, Optional, Union
+
+from reflex.components.component import Component
+from reflex.event import EventHandler
+
+DECORATED_PAGES = []
+
+
+def page(
+    route: Optional[str] = None,
+    title: Optional[str] = None,
+    image: Optional[str] = None,
+    description: Optional[str] = None,
+    meta: Optional[str] = None,
+    script_tags: Optional[List[Component]] = None,
+    on_load: Optional[Union[EventHandler, List[EventHandler]]] = None,
+):
+    """Decorate a function as a page.
+
+    rx.App() will automatically call add_page() for any method decorated with page
+    when App.compile is called.
+
+    All defaults are None because they will use the one from add_page().
+
+    Note: the decorated functions still need to be imported.
+
+    Args:
+        route: The route to reach the page.
+        title: The title of the page.
+        image: The favicon of the page.
+        description: The description of the page.
+        meta: Additionnal meta to add to the page.
+        on_load: The event handler(s) called when the page load.
+        script_tags: scripts to attach to the page
+
+    Returns:
+        The decorated function.
+    """
+    ...
+
+    def decorator(render_fn):
+        kwargs = {}
+        if route:
+            kwargs["route"] = route
+        if title:
+            kwargs["title"] = title
+        if image:
+            kwargs["image"] = image
+        if description:
+            kwargs["description"] = description
+        if meta:
+            kwargs["meta"] = meta
+        if script_tags:
+            kwargs["script_tags"] = script_tags
+        if on_load:
+            kwargs["on_load"] = on_load
+
+        DECORATED_PAGES.append((render_fn, kwargs))
+
+        return render_fn
+
+    return decorator

+ 11 - 21
reflex/route.py

@@ -7,8 +7,8 @@ from typing import Dict, List, Optional, Union
 
 from reflex import constants
 from reflex.event import EventHandler
-
-DECORATED_ROUTES = []
+from reflex.page import page
+from reflex.utils.console import deprecate
 
 
 def route(
@@ -37,25 +37,15 @@ def route(
     Returns:
         The decorated function.
     """
-
-    def decorator(render_fn):
-        kwargs = {}
-        if route:
-            kwargs["route"] = route
-        if title:
-            kwargs["title"] = title
-        if image:
-            kwargs["image"] = image
-        if description:
-            kwargs["description"] = description
-        if on_load:
-            kwargs["on_load"] = on_load
-
-        DECORATED_ROUTES.append((render_fn, kwargs))
-
-        return render_fn
-
-    return decorator
+    deprecate("@rx.route is deprecated and is being replaced by @rx.page instead")
+
+    return page(
+        route=route,
+        title=title,
+        image=image,
+        description=description,
+        on_load=on_load,
+    )
 
 
 def verify_route_validity(route: str) -> None: