image_documentation.py 2.2 KB

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