interactive_image_documentation.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class InteractiveImageDocumentation(UiElementDocumentation, element=ui.interactive_image):
  4. def main_demo(self) -> None:
  5. from nicegui.events import MouseEventArguments
  6. def mouse_handler(e: MouseEventArguments):
  7. color = 'SkyBlue' if e.type == 'mousedown' else 'SteelBlue'
  8. ii.content += f'<circle cx="{e.image_x}" cy="{e.image_y}" r="15" fill="none" stroke="{color}" stroke-width="4" />'
  9. ui.notify(f'{e.type} at ({e.image_x:.1f}, {e.image_y:.1f})')
  10. src = 'https://picsum.photos/id/565/640/360'
  11. ii = ui.interactive_image(src, on_mouse=mouse_handler, events=['mousedown', 'mouseup'], cross=True)
  12. def more(self) -> None:
  13. @self.demo('Nesting elements', '''
  14. You can nest elements inside an interactive image.
  15. Use Tailwind classes like "absolute top-0 left-0" to position the label absolutely with respect to the image.
  16. Of course this can be done with plain CSS as well.
  17. ''')
  18. def nesting_elements():
  19. with ui.interactive_image('https://picsum.photos/id/147/640/360'):
  20. ui.button(on_click=lambda: ui.notify('thumbs up'), icon='thumb_up') \
  21. .props('flat fab color=white') \
  22. .classes('absolute bottom-0 left-0 m-2')
  23. @self.demo('Force reload', '''
  24. You can force an image to reload by calling the `force_reload` method.
  25. It will append a timestamp to the image URL, which will make the browser reload the image.
  26. ''')
  27. def force_reload():
  28. img = ui.interactive_image('https://picsum.photos/640/360').classes('w-64')
  29. ui.button('Force reload', on_click=img.force_reload)