button_documentation.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from asyncio import sleep
  2. from contextlib import asynccontextmanager
  3. from nicegui import ui
  4. from nicegui.elements.button import Button
  5. from nicegui.elements.mixins.disableable_element import DisableableElement
  6. from ..documentation_tools import text_demo
  7. def main_demo() -> None:
  8. ui.button('Click me!', on_click=lambda: ui.notify(f'You clicked me!'))
  9. def more() -> None:
  10. @text_demo('Icons', '''
  11. You can also add an icon to a button.
  12. ''')
  13. async def icons() -> None:
  14. with ui.row():
  15. ui.button('demo', icon='history')
  16. ui.button(icon='thumb_up')
  17. with ui.button():
  18. ui.label('sub-elements')
  19. ui.image('https://picsum.photos/id/377/640/360') \
  20. .classes('rounded-full w-16 h-16 ml-4')
  21. @text_demo('Await button click', '''
  22. Sometimes it is convenient to wait for a button click before continuing the execution.
  23. ''')
  24. async def await_button_click() -> None:
  25. # @ui.page('/')
  26. # async def index():
  27. b = ui.button('Step')
  28. await b.clicked()
  29. ui.label('One')
  30. await b.clicked()
  31. ui.label('Two')
  32. await b.clicked()
  33. ui.label('Three')
  34. @text_demo('Disable button with a context manager', '''
  35. This showcases a async context manager that can be used to disable a button for the duration of an async process.
  36. ''')
  37. async def disable_context_manager() -> None:
  38. @asynccontextmanager
  39. async def disable(element: DisableableElement) -> None:
  40. element.disable()
  41. try:
  42. yield
  43. finally:
  44. element.enable()
  45. async def disable_and_sleep_3(button: Button) -> None:
  46. async with disable(button):
  47. await sleep(3)
  48. b = ui.button("Disable for 3 seconds", on_click=lambda: disable_and_sleep_3(b))