button_documentation.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class ButtonDocumentation(UiElementDocumentation, element=ui.button):
  4. def main_demo(self) -> None:
  5. ui.button('Click me!', on_click=lambda: ui.notify('You clicked me!'))
  6. def more(self) -> None:
  7. @self.demo('Icons', '''
  8. You can also add an icon to a button.
  9. ''')
  10. 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. @self.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. @self.demo('Disable button with a context manager', '''
  32. This showcases a context manager that can be used to disable a button for the duration of an async process.
  33. ''')
  34. def disable_context_manager() -> None:
  35. from contextlib import contextmanager
  36. import httpx
  37. @contextmanager
  38. def disable(button: ui.button):
  39. button.disable()
  40. try:
  41. yield
  42. finally:
  43. button.enable()
  44. async def get_slow_response(button: ui.button) -> None:
  45. with disable(button):
  46. async with httpx.AsyncClient() as client:
  47. response = await client.get('https://httpbin.org/delay/1', 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))