image_documentation.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class ImageDocumentation(UiElementDocumentation, element=ui.image):
  4. def main_demo(self) -> None:
  5. ui.image('https://picsum.photos/id/377/640/360')
  6. def more(self) -> None:
  7. ui.add_body_html(
  8. '<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>')
  9. @self.demo('Local files', '''
  10. You can use local images as well by passing a path to the image file.
  11. ''')
  12. def local():
  13. ui.image('website/static/logo.png').classes('w-16')
  14. @self.demo('Base64 string', '''
  15. You can also use a Base64 string as image source.
  16. ''')
  17. def base64():
  18. base64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
  19. ui.image(base64).classes('w-2 h-2 m-auto')
  20. @self.demo('PIL image', '''
  21. You can also use a PIL image as image source.
  22. ''')
  23. def pil():
  24. import numpy as np
  25. from PIL import Image
  26. image = Image.fromarray(np.random.randint(0, 255, (100, 100), dtype=np.uint8))
  27. ui.image(image).classes('w-32')
  28. @self.demo('Lottie files', '''
  29. You can also use [Lottie files](https://lottiefiles.com/) with animations.
  30. ''')
  31. def lottie():
  32. # ui.add_body_html('<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>')
  33. src = 'https://assets1.lottiefiles.com/datafiles/HN7OcWNnoqje6iXIiZdWzKxvLIbfeCGTmvXmEm1h/data.json'
  34. ui.html(f'<lottie-player src="{src}" loop autoplay />').classes('w-full')
  35. @self.demo('Image link', '''
  36. Images can link to another page by wrapping them in a [ui.link](https://nicegui.io/documentation/link).
  37. ''')
  38. def link():
  39. with ui.link(target='https://github.com/zauberzeug/nicegui'):
  40. ui.image('https://picsum.photos/id/41/640/360').classes('w-64')
  41. @self.demo('Force reload', '''
  42. You can force an image to reload by calling the `force_reload` method.
  43. It will append a timestamp to the image URL, which will make the browser reload the image.
  44. ''')
  45. def force_reload():
  46. img = ui.image('https://picsum.photos/640/360').classes('w-64')
  47. ui.button('Force reload', on_click=img.force_reload)