1
0

section_pages_routing.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import uuid
  2. from nicegui import app, ui
  3. from . import (
  4. doc,
  5. download_documentation,
  6. navigate_documentation,
  7. page_documentation,
  8. page_layout_documentation,
  9. page_title_documentation,
  10. )
  11. CONSTANT_UUID = str(uuid.uuid4())
  12. doc.title('*Pages* & Routing')
  13. doc.intro(page_documentation)
  14. @doc.demo('Auto-index page', '''
  15. Pages created with the `@ui.page` decorator are "private".
  16. Their content is re-created for each client.
  17. Thus, in the demo to the right, the displayed ID on the private page changes when the browser reloads the page.
  18. UI elements that are not wrapped in a decorated page function are placed on an automatically generated index page at route "/".
  19. This auto-index page is created once on startup and *shared* across all clients that might connect.
  20. Thus, each connected client will see the *same* elements.
  21. In the demo to the right, the displayed ID on the auto-index page remains constant when the browser reloads the page.
  22. ''')
  23. def auto_index_page():
  24. from uuid import uuid4
  25. @ui.page('/private_page')
  26. async def private_page():
  27. ui.label(f'private page with ID {uuid4()}')
  28. # ui.label(f'shared auto-index page with ID {uuid4()}')
  29. # ui.link('private page', private_page)
  30. # END OF DEMO
  31. ui.label(f'shared auto-index page with ID {CONSTANT_UUID}')
  32. ui.link('private page', private_page)
  33. doc.intro(page_layout_documentation)
  34. @doc.demo('Parameter injection', '''
  35. Thanks to FastAPI, a page function accepts optional parameters to provide
  36. [path parameters](https://fastapi.tiangolo.com/tutorial/path-params/),
  37. [query parameters](https://fastapi.tiangolo.com/tutorial/query-params/) or the whole incoming
  38. [request](https://fastapi.tiangolo.com/advanced/using-request-directly/) for accessing
  39. the body payload, headers, cookies and more.
  40. ''')
  41. def parameter_demo():
  42. @ui.page('/icon/{icon}')
  43. def icons(icon: str, amount: int = 1):
  44. ui.label(icon).classes('text-h3')
  45. with ui.row():
  46. [ui.icon(icon).classes('text-h3') for _ in range(amount)]
  47. ui.link('Star', '/icon/star?amount=5')
  48. ui.link('Home', '/icon/home')
  49. ui.link('Water', '/icon/water_drop?amount=3')
  50. doc.intro(page_title_documentation)
  51. doc.intro(navigate_documentation)
  52. doc.redirects['open'] = 'navigate#ui_navigate_to_(formerly_ui_open)'
  53. doc.text('ui.open', f'''
  54. The `ui.open` function is deprecated.
  55. Use [`ui.navigate.to`]({doc.redirects["open"]}) instead.
  56. ''')
  57. doc.intro(download_documentation)
  58. @doc.demo(app.add_static_files)
  59. def add_static_files_demo():
  60. from nicegui import app
  61. app.add_static_files('/examples', 'examples')
  62. ui.label('Some NiceGUI Examples').classes('text-h5')
  63. ui.link('AI interface', '/examples/ai_interface/main.py')
  64. ui.link('Custom FastAPI app', '/examples/fastapi/main.py')
  65. ui.link('Authentication', '/examples/authentication/main.py')
  66. @doc.demo(app.add_media_files)
  67. def add_media_files_demo():
  68. from pathlib import Path
  69. import requests
  70. from nicegui import app
  71. media = Path('media')
  72. # media.mkdir(exist_ok=True)
  73. # r = requests.get('https://cdn.coverr.co/videos/coverr-cloudy-sky-2765/1080p.mp4')
  74. # (media / 'clouds.mp4').write_bytes(r.content)
  75. # app.add_media_files('/my_videos', media)
  76. # ui.video('/my_videos/clouds.mp4')
  77. # END OF DEMO
  78. ui.video('https://cdn.coverr.co/videos/coverr-cloudy-sky-2765/1080p.mp4')
  79. @doc.demo('Add HTML to the page', '''
  80. You can add HTML to the page by calling `ui.add_head_html` or `ui.add_body_html`.
  81. This is useful for adding custom CSS styles or JavaScript code.
  82. ''')
  83. def add_head_html_demo():
  84. ui.add_head_html('''
  85. <style>
  86. .my-red-label {
  87. color: Crimson;
  88. font-weight: bold;
  89. }
  90. </style>
  91. ''')
  92. ui.label('RED').classes('my-red-label')
  93. @doc.demo('API Responses', '''
  94. NiceGUI is based on [FastAPI](https://fastapi.tiangolo.com/).
  95. This means you can use all of FastAPI's features.
  96. For example, you can implement a RESTful API in addition to your graphical user interface.
  97. You simply import the `app` object from `nicegui`.
  98. 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()`.
  99. You can also return any other FastAPI response object inside a page function.
  100. For example, you can return a `RedirectResponse` to redirect the user to another page if certain conditions are met.
  101. This is used in our [authentication demo](https://github.com/zauberzeug/nicegui/tree/main/examples/authentication/main.py).
  102. ''')
  103. def fastapi_demo():
  104. import random
  105. from nicegui import app
  106. @app.get('/random/{max}')
  107. def generate_random_number(max: int):
  108. return {'min': 0, 'max': max, 'value': random.randint(0, max)}
  109. max = ui.number('max', value=100)
  110. ui.button('generate random number',
  111. on_click=lambda: ui.navigate.to(f'/random/{max.value:.0f}'))