dialog_documentation.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. with ui.dialog() as dialog, ui.card():
  5. ui.label('Hello world!')
  6. ui.button('Close', on_click=dialog.close)
  7. ui.button('Open a dialog', on_click=dialog.open)
  8. def more() -> None:
  9. @text_demo('Awaitable dialog', '''
  10. Dialogs can be awaited.
  11. Use the `submit` method to close the dialog and return a result.
  12. Canceling the dialog by clicking in the background or pressing the escape key yields `None`.
  13. ''')
  14. def async_dialog_demo():
  15. with ui.dialog() as dialog, ui.card():
  16. ui.label('Are you sure?')
  17. with ui.row():
  18. ui.button('Yes', on_click=lambda: dialog.submit('Yes'))
  19. ui.button('No', on_click=lambda: dialog.submit('No'))
  20. async def show():
  21. result = await dialog
  22. ui.notify(f'You chose {result}')
  23. ui.button('Await a dialog', on_click=show)
  24. @text_demo('Replacing content', '''
  25. The content of a dialog can be changed.
  26. ''')
  27. def replace_content():
  28. def replace():
  29. dialog.clear()
  30. with dialog, ui.card().classes('w-64 h-64'):
  31. ui.label('New Content')
  32. dialog.open()
  33. with ui.dialog() as dialog, ui.card():
  34. ui.label('Hello world!')
  35. ui.button('Open', on_click=dialog.open)
  36. ui.button('Replace', on_click=replace)