1
0

button_documentation.py 2.0 KB

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