page_documentation.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. @ui.page('/other_page')
  5. def other_page():
  6. ui.label('Welcome to the other side')
  7. ui.link('Back to main page', '/documentation#page')
  8. @ui.page('/dark_page', dark=True)
  9. def dark_page():
  10. ui.label('Welcome to the dark side')
  11. ui.link('Back to main page', '/documentation#page')
  12. ui.link('Visit other page', other_page)
  13. ui.link('Visit dark page', dark_page)
  14. def more() -> None:
  15. @text_demo('Pages with Path Parameters', '''
  16. Page routes can contain parameters like [FastAPI](https://fastapi.tiangolo.com/tutorial/path-params/>).
  17. If type-annotated, they are automatically converted to bool, int, float and complex values.
  18. If the page function expects a `request` argument, the request object is automatically provided.
  19. The `client` argument provides access to the websocket connection, layout, etc.
  20. ''')
  21. def page_with_path_parameters_demo():
  22. @ui.page('/repeat/{word}/{count}')
  23. def page(word: str, count: int):
  24. ui.label(word * count)
  25. ui.link('Say hi to Santa!', 'repeat/Ho! /3')
  26. @text_demo('Wait for Client Connection', '''
  27. To wait for a client connection, you can add a `client` argument to the decorated page function
  28. and await `client.connected()`.
  29. All code below that statement is executed after the websocket connection between server and client has been established.
  30. 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)).
  31. Also it is possible to do async stuff while the user already sees some content.
  32. ''')
  33. def wait_for_connected_demo():
  34. import asyncio
  35. from nicegui import Client
  36. @ui.page('/wait_for_connection')
  37. async def wait_for_connection(client: Client):
  38. ui.label('This text is displayed immediately.')
  39. await client.connected()
  40. await asyncio.sleep(2)
  41. ui.label('This text is displayed 2 seconds after the page has been fully loaded.')
  42. ui.label(f'The IP address {client.ip} was obtained from the websocket.')
  43. ui.link('wait for connection', wait_for_connection)