button_documentation.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. from typing import AsyncContextManager, Awaitable
  38. @asynccontextmanager
  39. async def disable(element: DisableableElement) -> AsyncContextManager[None]:
  40. element.disable()
  41. try:
  42. yield
  43. finally:
  44. element.enable()
  45. async def disable_and_sleep_3(button: ui.button) -> Awaitable[None]:
  46. async with disable(button):
  47. await sleep(3)
  48. ui.button("Disable for 3 seconds", on_click=lambda e: disable_and_sleep_3(e.sender))