image.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """An image component."""
  2. from __future__ import annotations
  3. from typing import Any, Optional
  4. from reflex.components.chakra import ChakraComponent, LiteralImageLoading
  5. from reflex.components.component import Component
  6. from reflex.event import EventHandler
  7. from reflex.vars import Var
  8. class Image(ChakraComponent):
  9. """Display an image."""
  10. tag = "Image"
  11. alias = "ChakraImage"
  12. # How to align the image within its bounds. It maps to css `object-position` property.
  13. align: Var[str]
  14. # Fallback Reflex component to show if image is loading or image fails.
  15. fallback: Optional[Component] = None
  16. # Fallback image src to show if image is loading or image fails.
  17. fallback_src: Var[str]
  18. # How the image to fit within its bounds. It maps to css `object-fit` property.
  19. fit: Var[str]
  20. # The native HTML height attribute to the passed to the img.
  21. html_height: Var[str]
  22. # The native HTML width attribute to the passed to the img.
  23. html_width: Var[str]
  24. # If true, opt out of the fallbackSrc logic and use as img.
  25. ignore_fallback: Var[bool]
  26. # "eager" | "lazy"
  27. loading: Var[LiteralImageLoading]
  28. # The path/url to the image or PIL image object.
  29. src: Var[Any]
  30. # The alt text of the image.
  31. alt: Var[str]
  32. # Provide multiple sources for an image, allowing the browser
  33. # to select the most appropriate source based on factors like
  34. # screen resolution and device capabilities.
  35. # Learn more _[here](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images)_
  36. src_set: Var[str]
  37. # Fired when the image fails to load.
  38. on_error: EventHandler[lambda: []]
  39. # Fired when the image is loaded.
  40. on_load: EventHandler[lambda: []]
  41. @classmethod
  42. def create(cls, *children, **props) -> Component:
  43. """Create an Image component.
  44. Args:
  45. *children: The children of the image.
  46. **props: The props of the image.
  47. Returns:
  48. The Image component.
  49. """
  50. src = props.get("src", None)
  51. if src is not None and not isinstance(src, (Var)):
  52. props["src"] = Var.create(value=src, _var_is_string=True)
  53. return super().create(*children, **props)