dialog_documentation.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.dialog)
  4. def main_demo() -> None:
  5. with ui.dialog() as dialog, ui.card():
  6. ui.label('Hello world!')
  7. ui.button('Close', on_click=dialog.close)
  8. ui.button('Open a dialog', on_click=dialog.open)
  9. @doc.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. @doc.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)
  37. doc.reference(ui.dialog)