Skip to content

ImageView

Base classes

Name Children Inherits
Vertical
textual.containers
An expanding container with vertical layout and no scrollbars.

⋔ Inheritance diagram

graph TD
  866412005["image.ImageView"]
  723859837["containers.Vertical"]
  4188532303["widget.Widget"]
  3946681048["dom.DOMNode"]
  53685856["message_pump.MessagePump"]
  2751680898["builtins.object"]
  723859837 --> 866412005
  4188532303 --> 723859837
  3946681048 --> 4188532303
  53685856 --> 3946681048
  2751680898 --> 53685856

🛈 DocStrings

Bases: Vertical

An image block: the picture, and a caption with its type, size and URI.

Source code in src/acptui/ui/widgets/image.py
class ImageView(Vertical):
    """An image block: the picture, and a caption with its type, size and URI."""

    DEFAULT_CSS = """
    ImageView {
        height: auto;
        width: 100%;
        margin: 0 0 1 0;
        .image-pixels { max-width: 100%; }
        .image-caption { color: $text-muted; height: auto; }
    }
    """

    def __init__(
        self, data: str, mime_type: str, *, uri: str | None = None, mode: ImageMode | None = None
    ) -> None:
        super().__init__()
        self.data = data
        self.mime_type = mime_type
        self.uri = uri
        self.mode: ImageMode = mode or image_mode()
        self.key = image_key(data)
        self.error: str | None = None
        """Why the image is not drawn (``None`` if it is)."""

    def compose(self) -> ComposeResult:
        size = human_size(len(self.data) * 3 // 4)
        caption = f"image: {self.mime_type}, {size}"
        if self.uri:
            caption += f", {self.uri}"
        if self.mode == "off":
            self.error = "images are turned off"
        else:
            decoded = decode(self.data)
            if isinstance(decoded, str):
                self.error = decoded
            else:
                caption += f", {decoded.width}x{decoded.height} px"
                yield _image_widget(decoded, self.mode)
        if self.error:
            caption += f" ({self.error})"
        yield Static(Content(caption), classes="image-caption")

error instance-attribute

error: str | None = None

Why the image is not drawn (None if it is).

:fa-brands-github: Show source on GitHub