page_documentation.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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('Modularize with APIRouter', '''
  41. You can use the NiceGUI specialization of
  42. [FastAPI's APIRouter](https://fastapi.tiangolo.com/tutorial/bigger-applications/?h=apirouter#apirouter)
  43. to modularize your code by grouping pages and other routes together.
  44. This is especially useful if you want to reuse the same prefix for multiple pages.
  45. The router and its pages can be neatly tugged away in a separate module (e.g. file) and
  46. the router is simply imported and included in the main app.
  47. See our [modularization example](https://github.com/zauberzeug/nicegui/blob/main/examples/modularization/api_router_example.py)
  48. for a multi-file app structure using an API router.
  49. ''', tab='/sub-path')
  50. def api_router_demo():
  51. # from nicegui import APIRouter, app
  52. #
  53. # router = APIRouter(prefix='/sub-path')
  54. #
  55. # @router.page('/')
  56. # def page():
  57. # ui.label('This is content on /sub-path')
  58. #
  59. # @router.page('/sub-sub-path')
  60. # def page():
  61. # ui.label('This is content on /sub-path/sub-sub-path')
  62. #
  63. # ui.link('Visit sub-path', '/sub-path')
  64. # ui.link('Visit sub-sub-path', '/sub-path/sub-sub-path')
  65. #
  66. # app.include_router(router)
  67. # END OF DEMO
  68. ui.label('Shows up on /sub-path')