route.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """The route decorator and associated variables."""
  2. from typing import Optional
  3. from pynecone.event import EventHandler
  4. DECORATED_ROUTES = []
  5. def route(
  6. route: Optional[str] = None,
  7. title: Optional[str] = None,
  8. image: Optional[str] = None,
  9. description: Optional[str] = None,
  10. on_load: Optional[EventHandler] = None,
  11. ):
  12. """Decorate a function as a page.
  13. pc.App() will automatically call add_page() for any method decorated with route
  14. when App.compile is called.
  15. All defaults are None because they will use the one from add_page().
  16. Note: the decorated functions still need to be imported.
  17. Args:
  18. route: The route to reach the page.
  19. title: The title of the page.
  20. image: The favicon of the page.
  21. description: The description of the page
  22. on_load: The event handler called when the page load.
  23. Returns:
  24. The decorated function.
  25. """
  26. def decorator(render_fn):
  27. kwargs = {}
  28. if route:
  29. kwargs["route"] = route
  30. if title:
  31. kwargs["title"] = title
  32. if image:
  33. kwargs["image"] = image
  34. if description:
  35. kwargs["description"] = description
  36. if on_load:
  37. kwargs["on_load"] = on_load
  38. DECORATED_ROUTES.append((render_fn, kwargs))
  39. return render_fn
  40. return decorator