button_documentation.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.button)
  4. def main_demo() -> None:
  5. ui.button('Click me!', on_click=lambda: ui.notify('You clicked me!'))
  6. @doc.demo('Icons', '''
  7. You can also add an icon to a button.
  8. ''')
  9. 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. @doc.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. @doc.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. def disable_context_manager() -> None:
  34. from contextlib import contextmanager
  35. import httpx
  36. @contextmanager
  37. def disable(button: ui.button):
  38. button.disable()
  39. try:
  40. yield
  41. finally:
  42. button.enable()
  43. async def get_slow_response(button: ui.button) -> None:
  44. with disable(button):
  45. async with httpx.AsyncClient() as client:
  46. response = await client.get('https://httpbin.org/delay/1', timeout=5)
  47. ui.notify(f'Response code: {response.status_code}')
  48. ui.button('Get slow response', on_click=lambda e: get_slow_response(e.sender))
  49. doc.reference(ui.button)