button_documentation.py 1.9 KB

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