1
0

page_documentation.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.page)
  4. def main_demo() -> None:
  5. @ui.page('/other_page')
  6. def other_page():
  7. ui.label('Welcome to the other side')
  8. @ui.page('/dark_page', dark=True)
  9. def dark_page():
  10. ui.label('Welcome to the dark side')
  11. ui.link('Visit other page', other_page)
  12. ui.link('Visit dark page', dark_page)
  13. @doc.demo('Pages with Path Parameters', '''
  14. Page routes can contain parameters like [FastAPI](https://fastapi.tiangolo.com/tutorial/path-params/).
  15. If type-annotated, they are automatically converted to bool, int, float and complex values.
  16. If the page function expects a `request` argument, the request object is automatically provided.
  17. The `client` argument provides access to the websocket connection, layout, etc.
  18. ''')
  19. def page_with_path_parameters_demo():
  20. @ui.page('/repeat/{word}/{count}')
  21. def page(word: str, count: int):
  22. ui.label(word * count)
  23. ui.link('Say hi to Santa!', '/repeat/Ho! /3')
  24. @doc.demo('Wait for Client Connection', '''
  25. To wait for a client connection, you can add a `client` argument to the decorated page function
  26. and await `client.connected()`.
  27. All code below that statement is executed after the websocket connection between server and client has been established.
  28. For example, this allows you to run JavaScript commands; which is only possible with a client connection (see [#112](https://github.com/zauberzeug/nicegui/issues/112)).
  29. Also it is possible to do async stuff while the user already sees some content.
  30. ''')
  31. def wait_for_connected_demo():
  32. import asyncio
  33. @ui.page('/wait_for_connection')
  34. async def wait_for_connection():
  35. ui.label('This text is displayed immediately.')
  36. await ui.context.client.connected()
  37. await asyncio.sleep(2)
  38. ui.label('This text is displayed 2 seconds after the page has been fully loaded.')
  39. ui.link('wait for connection', wait_for_connection)
  40. @doc.demo('Multicasting', '''
  41. The content on a page is private to the client (the browser tab) and has its own local element context.
  42. If you want to send updates to _all_ clients of a specific page, you can use the `app.clients` iterator.
  43. This is useful for modifying UI elements from a background process or from other pages.
  44. ''')
  45. def multicasting():
  46. from nicegui import app
  47. @ui.page('/multicast_receiver')
  48. def page():
  49. ui.label('This page will show messages from the index page.')
  50. def send(message: str):
  51. for client in app.clients('/multicast_receiver'):
  52. with client:
  53. ui.notify(message)
  54. ui.button('Send message', on_click=lambda: send('Hi!'))
  55. ui.link('Open receiver', '/multicast_receiver', new_tab=True)
  56. @doc.demo('Modularize with APIRouter', '''
  57. You can use the NiceGUI specialization of
  58. [FastAPI's APIRouter](https://fastapi.tiangolo.com/tutorial/bigger-applications/?h=apirouter#apirouter)
  59. to modularize your code by grouping pages and other routes together.
  60. This is especially useful if you want to reuse the same prefix for multiple pages.
  61. The router and its pages can be neatly tugged away in a separate module (e.g. file) and
  62. the router is simply imported and included in the main app.
  63. See our [modularization example](https://github.com/zauberzeug/nicegui/blob/main/examples/modularization/api_router_example.py)
  64. for a multi-file app structure using an API router.
  65. ''', tab='/sub-path')
  66. def api_router_demo():
  67. # from nicegui import APIRouter, app
  68. #
  69. # router = APIRouter(prefix='/sub-path')
  70. #
  71. # @router.page('/')
  72. # def page():
  73. # ui.label('This is content on /sub-path')
  74. #
  75. # @router.page('/sub-sub-path')
  76. # def page():
  77. # ui.label('This is content on /sub-path/sub-sub-path')
  78. #
  79. # ui.link('Visit sub-path', '/sub-path')
  80. # ui.link('Visit sub-sub-path', '/sub-path/sub-sub-path')
  81. #
  82. # app.include_router(router)
  83. # END OF DEMO
  84. ui.label('Shows up on /sub-path')