pages_routing.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import uuid
  2. from nicegui import app, ui
  3. from ..tools import element_demo, load_demo, subheading, text_demo
  4. CONSTANT_UUID = str(uuid.uuid4())
  5. def content() -> None:
  6. load_demo(ui.page)
  7. @text_demo('Auto-index page', '''
  8. Pages created with the `@ui.page` decorator are "private".
  9. Their content is re-created for each client.
  10. Thus, in the demo to the right, the displayed ID on the private page changes when the browser reloads the page.
  11. UI elements that are not wrapped in a decorated page function are placed on an automatically generated index page at route "/".
  12. This auto-index page is created once on startup and *shared* across all clients that might connect.
  13. Thus, each connected client will see the *same* elements.
  14. In the demo to the right, the displayed ID on the auto-index page remains constant when the browser reloads the page.
  15. ''')
  16. def auto_index_page():
  17. from uuid import uuid4
  18. @ui.page('/private_page')
  19. async def private_page():
  20. ui.label(f'private page with ID {uuid4()}')
  21. # ui.label(f'shared auto-index page with ID {uuid4()}')
  22. # ui.link('private page', private_page)
  23. # END OF DEMO
  24. ui.label(f'shared auto-index page with ID {CONSTANT_UUID}')
  25. ui.link('private page', private_page)
  26. @text_demo('Page Layout', '''
  27. With `ui.header`, `ui.footer`, `ui.left_drawer` and `ui.right_drawer` you can add additional layout elements to a page.
  28. The `fixed` argument controls whether the element should scroll or stay fixed on the screen.
  29. The `top_corner` and `bottom_corner` arguments indicate whether a drawer should expand to the top or bottom of the page.
  30. See <https://quasar.dev/layout/header-and-footer> and <https://quasar.dev/layout/drawer> for more information about possible props.
  31. With `ui.page_sticky` you can place an element "sticky" on the screen.
  32. See <https://quasar.dev/layout/page-sticky> for more information.
  33. ''')
  34. def page_layout_demo():
  35. @ui.page('/page_layout')
  36. def page_layout():
  37. ui.label('CONTENT')
  38. [ui.label(f'Line {i}') for i in range(100)]
  39. with ui.header(elevated=True).style('background-color: #3874c8').classes('items-center justify-between'):
  40. ui.label('HEADER')
  41. ui.button(on_click=lambda: right_drawer.toggle(), icon='menu').props('flat color=white')
  42. with ui.left_drawer(top_corner=True, bottom_corner=True).style('background-color: #d7e3f4'):
  43. ui.label('LEFT DRAWER')
  44. with ui.right_drawer(fixed=False).style('background-color: #ebf1fa').props('bordered') as right_drawer:
  45. ui.label('RIGHT DRAWER')
  46. with ui.footer().style('background-color: #3874c8'):
  47. ui.label('FOOTER')
  48. ui.link('show page with fancy layout', page_layout)
  49. @text_demo('Parameter injection', '''
  50. Thanks to FastAPI, a page function accepts optional parameters to provide
  51. [path parameters](https://fastapi.tiangolo.com/tutorial/path-params/),
  52. [query parameters](https://fastapi.tiangolo.com/tutorial/query-params/) or the whole incoming
  53. [request](https://fastapi.tiangolo.com/advanced/using-request-directly/) for accessing
  54. the body payload, headers, cookies and more.
  55. ''')
  56. def parameter_demo():
  57. @ui.page('/icon/{icon}')
  58. def icons(icon: str, amount: int = 1):
  59. ui.label(icon).classes('text-h3')
  60. with ui.row():
  61. [ui.icon(icon).classes('text-h3') for _ in range(amount)]
  62. ui.link('Star', '/icon/star?amount=5')
  63. ui.link('Home', '/icon/home')
  64. ui.link('Water', '/icon/water_drop?amount=3')
  65. load_demo(ui.open)
  66. load_demo(ui.download)
  67. subheading('Static files')
  68. @element_demo(app.add_static_files)
  69. def add_static_files_demo():
  70. from nicegui import app
  71. app.add_static_files('/examples', 'examples')
  72. ui.label('Some NiceGUI Examples').classes('text-h5')
  73. ui.link('AI interface', '/examples/ai_interface/main.py')
  74. ui.link('Custom FastAPI app', '/examples/fastapi/main.py')
  75. ui.link('Authentication', '/examples/authentication/main.py')
  76. subheading('Media files')
  77. @element_demo(app.add_media_files)
  78. def add_media_files_demo():
  79. from pathlib import Path
  80. import requests
  81. from nicegui import app
  82. media = Path('media')
  83. # media.mkdir(exist_ok=True)
  84. # r = requests.get('https://cdn.coverr.co/videos/coverr-cloudy-sky-2765/1080p.mp4')
  85. # (media / 'clouds.mp4').write_bytes(r.content)
  86. # app.add_media_files('/my_videos', media)
  87. # ui.video('/my_videos/clouds.mp4')
  88. # END OF DEMO
  89. ui.video('https://cdn.coverr.co/videos/coverr-cloudy-sky-2765/1080p.mp4')
  90. @text_demo('API Responses', '''
  91. NiceGUI is based on [FastAPI](https://fastapi.tiangolo.com/).
  92. This means you can use all of FastAPI's features.
  93. For example, you can implement a RESTful API in addition to your graphical user interface.
  94. You simply import the `app` object from `nicegui`.
  95. Or you can run NiceGUI on top of your own FastAPI app by using `ui.run_with(app)` instead of starting a server automatically with `ui.run()`.
  96. You can also return any other FastAPI response object inside a page function.
  97. For example, you can return a `RedirectResponse` to redirect the user to another page if certain conditions are met.
  98. This is used in our [authentication demo](https://github.com/zauberzeug/nicegui/tree/main/examples/authentication/main.py).
  99. ''')
  100. def fastapi_demo():
  101. import random
  102. from nicegui import app
  103. @app.get('/random/{max}')
  104. def generate_random_number(max: int):
  105. return {'min': 0, 'max': max, 'value': random.randint(0, max)}
  106. max = ui.number('max', value=100)
  107. ui.button('generate random number', on_click=lambda: ui.open(f'/random/{max.value:.0f}'))