pages_routing.py 6.6 KB

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