image.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """An image component."""
  2. from __future__ import annotations
  3. from typing import Any, Optional, Set
  4. from pynecone.components.component import Component
  5. from pynecone.components.libs.chakra import ChakraComponent
  6. from pynecone.components.tags import Tag
  7. from pynecone.utils import format, types
  8. from pynecone.vars import Var
  9. class Image(ChakraComponent):
  10. """Display an image."""
  11. tag = "Image"
  12. # How to align the image within its bounds. It maps to css `object-position` property.
  13. align: Var[str]
  14. # Fallback Pynecone 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[str]
  28. # The image src attribute.
  29. src: Var[Any]
  30. # The image srcset attribute.
  31. src_set: Var[str]
  32. def get_triggers(self) -> Set[str]:
  33. """Get the event triggers for the component.
  34. Returns:
  35. The event triggers.
  36. """
  37. return super().get_triggers() | {"on_error", "on_load"}
  38. def _render(self) -> Tag:
  39. # If the src is an image, convert it to a base64 string.
  40. if types.is_image(type(self.src)):
  41. self.src = Var.create(format.format_image_data(self.src)) # type: ignore
  42. # Render the table.
  43. return super()._render()