page.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """The page decorator and associated variables and functions."""
  2. from __future__ import annotations
  3. from reflex.components.component import Component
  4. from reflex.event import EventHandler
  5. DECORATED_PAGES = []
  6. def page(
  7. route: str | None = None,
  8. title: str | None = None,
  9. image: str | None = None,
  10. description: str | None = None,
  11. meta: str | None = None,
  12. script_tags: list[Component] | None = None,
  13. on_load: EventHandler | list[EventHandler] | None = None,
  14. ):
  15. """Decorate a function as a page.
  16. rx.App() will automatically call add_page() for any method decorated with page
  17. when App.compile is called.
  18. All defaults are None because they will use the one from add_page().
  19. Note: the decorated functions still need to be imported.
  20. Args:
  21. route: The route to reach the page.
  22. title: The title of the page.
  23. image: The favicon of the page.
  24. description: The description of the page.
  25. meta: Additionnal meta to add to the page.
  26. on_load: The event handler(s) called when the page load.
  27. script_tags: scripts to attach to the page
  28. Returns:
  29. The decorated function.
  30. """
  31. def decorator(render_fn):
  32. kwargs = {}
  33. if route:
  34. kwargs["route"] = route
  35. if title:
  36. kwargs["title"] = title
  37. if image:
  38. kwargs["image"] = image
  39. if description:
  40. kwargs["description"] = description
  41. if meta:
  42. kwargs["meta"] = meta
  43. if script_tags:
  44. kwargs["script_tags"] = script_tags
  45. if on_load:
  46. kwargs["on_load"] = on_load
  47. DECORATED_PAGES.append((render_fn, kwargs))
  48. return render_fn
  49. return decorator