1
0

button_documentation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. with ui.column(): # HIDE
  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. @doc.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))
  50. @doc.demo('Custom toggle button', '''
  51. As with all other elements, you can implement your own subclass with specialized logic.
  52. Like this red/green toggle button with an internal boolean state.
  53. ''')
  54. def toggle_button() -> None:
  55. class ToggleButton(ui.button):
  56. def __init__(self, *args, **kwargs) -> None:
  57. super().__init__(*args, **kwargs)
  58. self._state = False
  59. self.on('click', self.toggle)
  60. def toggle(self) -> None:
  61. """Toggle the button state."""
  62. self._state = not self._state
  63. self.update()
  64. def update(self) -> None:
  65. self.props(f'color={"green" if self._state else "red"}')
  66. super().update()
  67. ToggleButton('Toggle me')
  68. @doc.demo('Floating Action Button', '''
  69. As described in the [Quasar documentation](https://quasar.dev/vue-components/floating-action-button),
  70. a Floating Action Button (FAB) is simply a "page-sticky" with a button inside.
  71. With the "fab" prop, the button will be rounded and gets a shadow.
  72. Color can be freely chosen, but most often it is an accent color.
  73. ''')
  74. def fab() -> None:
  75. ui.colors(accent='#6AD4DD')
  76. # with ui.page_sticky(x_offset=18, y_offset=18):
  77. with ui.row().classes('w-full h-full justify-end items-end'): # HIDE
  78. ui.button(icon='home', on_click=lambda: ui.notify('home')) \
  79. .props('fab color=accent')
  80. @doc.demo('Expandable Floating Action Button', '''
  81. The [Quasar FAB (q-fab)](https://quasar.dev/vue-components/floating-action-button),
  82. is a button that reveals multiple actions when clicked.
  83. While it is not a separate element in NiceGUI, it can be easily created using the generic `ui.element`.
  84. ''')
  85. def expandable_fab() -> None:
  86. with ui.element('q-fab').props('icon=navigation color=green'):
  87. ui.element('q-fab-action').props('icon=train color=green-5') \
  88. .on('click', lambda: ui.notify('train'))
  89. ui.element('q-fab-action').props('icon=sailing color=green-5') \
  90. .on('click', lambda: ui.notify('boat'))
  91. ui.element('q-fab-action').props('icon=rocket color=green-5') \
  92. .on('click', lambda: ui.notify('rocket'))
  93. doc.reference(ui.button)