dialog_documentation.py 1.6 KB

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