base.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Welcome to Reflex! This file outlines the steps to create a basic app."""
  2. from typing import Callable
  3. import reflex as rx
  4. from .pages import dashboard_page, home_page, settings_page
  5. from .sidebar import sidebar
  6. from .state import State
  7. from .styles import *
  8. meta = [
  9. {
  10. "name": "viewport",
  11. "content": "width=device-width, shrink-to-fit=no, initial-scale=1",
  12. },
  13. ]
  14. def template(main_content: Callable[[], rx.Component]) -> rx.Component:
  15. """The template for each page of the app.
  16. Args:
  17. main_content (Callable[[], rx.Component]): The main content of the page.
  18. Returns:
  19. rx.Component: The template for each page of the app.
  20. """
  21. menu_button = rx.box(
  22. rx.menu(
  23. rx.menu_button(
  24. rx.icon(
  25. tag="hamburger",
  26. size="4em",
  27. color=text_color,
  28. ),
  29. ),
  30. rx.menu_list(
  31. rx.menu_item(rx.link("Home", href="/", width="100%")),
  32. rx.menu_divider(),
  33. rx.menu_item(
  34. rx.link("About", href="https://github.com/reflex-dev", width="100%")
  35. ),
  36. rx.menu_item(
  37. rx.link("Contact", href="mailto:founders@=reflex.dev", width="100%")
  38. ),
  39. ),
  40. ),
  41. position="fixed",
  42. right="1.5em",
  43. top="1.5em",
  44. z_index="500",
  45. )
  46. return rx.hstack(
  47. sidebar(),
  48. main_content(),
  49. rx.spacer(),
  50. menu_button,
  51. align_items="flex-start",
  52. transition="left 0.5s, width 0.5s",
  53. position="relative",
  54. left=rx.cond(State.sidebar_displayed, "0px", f"-{sidebar_width}"),
  55. )
  56. @rx.page("/", meta=meta)
  57. @template
  58. def home() -> rx.Component:
  59. """Home page.
  60. Returns:
  61. rx.Component: The home page.
  62. """
  63. return home_page()
  64. @rx.page("/settings", meta=meta)
  65. @template
  66. def settings() -> rx.Component:
  67. """Settings page.
  68. Returns:
  69. rx.Component: The settings page.
  70. """
  71. return settings_page()
  72. @rx.page("/dashboard", meta=meta)
  73. @template
  74. def dashboard() -> rx.Component:
  75. """Dashboard page.
  76. Returns:
  77. rx.Component: The dashboard page.
  78. """
  79. return dashboard_page()
  80. # Add state and page to the app.
  81. app = rx.App(style=base_style)
  82. app.compile()