1
0

image_documentation.py 2.0 KB

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