A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: render IPython.display.Image with raw bytes (e.g. GIFs) (#8282)
Akshay Agrawalcd15db1dfcc2
diff --git a/marimo/_output/formatters/ipython_formatters.py b/marimo/_output/formatters/ipython_formatters.pyindex ff4c980247..8e98884d27 100644--- a/marimo/_output/formatters/ipython_formatters.py+++ b/marimo/_output/formatters/ipython_formatters.py@@ -3,11 +3,12 @@ from __future__ import annotationsimport functoolsfrom dataclasses import dataclass-from typing import Any, Callable, Optional+from typing import Any, Callable, Optional, castfrom marimo._messaging.mimetypes import KnownMimeTypefrom marimo._output.builder import hfrom marimo._output.formatters.formatter_factory import FormatterFactory+from marimo._plugins.core.media import io_to_data_urlclass IPythonFormatter(FormatterFactory):@@ -147,6 +148,22 @@ class IPythonFormatter(FormatterFactory):return ("text/html", data)+ @formatting.formatter(+ IPython.display.Image # type:ignore+ )+ def _format_image(+ img: IPython.display.Image, # type:ignore+ ) -> tuple[KnownMimeType, str]:+ if img.data is not None:+ mime_type = img._MIMETYPES.get(+ img.format, f"image/{img.format}"+ )+ data_url = io_to_data_url(img.data, mime_type)+ return (cast(KnownMimeType, mime_type), data_url or "")+ elif img.url is not None:+ return ("text/html", h.img(src=img.url, alt=""))+ return ("text/plain", repr(img))+return unpatchdiff --git a/marimo/_output/formatters/repr_formatters.py b/marimo/_output/formatters/repr_formatters.pyindex 69ac6589e1..9848010225 100644--- a/marimo/_output/formatters/repr_formatters.py+++ b/marimo/_output/formatters/repr_formatters.py@@ -82,6 +82,18 @@ def maybe_get_repr_formatter(data_url = io_to_data_url(data, mime_key)if data_url:contents[mime_key] = data_url+ elif (+ mime_key.startswith(MEDIA_MIME_PREFIXES)+ and isinstance(data, str)+ and not data.startswith(+ ("data:", "http:", "https:", "<", "{")+ )+ ):+ # Base64-encoded string (e.g. from+ # IPython._repr_mimebundle_)+ contents[mime_key] = (+ f"data:{mime_key};base64,{data}"+ )# Remove text/plain from the mimebundle if it's present# since there are other representations availablediff --git a/tests/_output/formatters/test_ipython_formatters.py b/tests/_output/formatters/test_ipython_formatters.pyindex b42eb773b1..6a8db509b1 100644--- a/tests/_output/formatters/test_ipython_formatters.py+++ b/tests/_output/formatters/test_ipython_formatters.py@@ -60,6 +60,63 @@ def test_repr_mimebundle():assert bundle._repr_mimebundle_(include=["text/html"]) == data[email protected](not HAS_DEPS, reason="IPython not installed")+def test_format_image_gif_bytes():+ from IPython.display import Image++ IPythonFormatter().register()++ # Minimal valid GIF+ gif_bytes = (+ b"GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00"+ b"!\xf9\x04\x00\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00"+ b"\x01\x00\x00\x02\x02D\x01\x00;"+ )+ img = Image(data=gif_bytes, format="gif")+ formatter = get_formatter(img)+ assert formatter is not None+ mime_type, data = formatter(img)+ assert mime_type == "image/gif"+ assert data.startswith("data:image/gif;base64,")++[email protected](not HAS_DEPS, reason="IPython not installed")+def test_format_image_png_bytes():+ from IPython.display import Image++ IPythonFormatter().register()++ # Minimal valid PNG (1x1 white pixel)+ import base64++ png_b64 = (+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4"+ "2mP8/58BAwAI/AL+hc2rNAAAAABJRU5ErkJggg=="+ )+ png_bytes = base64.b64decode(png_b64)+ img = Image(data=png_bytes, format="png")+ formatter = get_formatter(img)+ assert formatter is not None+ mime_type, data = formatter(img)+ assert mime_type == "image/png"+ assert data.startswith("data:image/png;base64,")++[email protected](not HAS_DEPS, reason="IPython not installed")+def test_format_image_url():+ from IPython.display import Image++ IPythonFormatter().register()++ img = Image(url="https://example.com/img.gif")+ formatter = get_formatter(img)+ assert formatter is not None+ mime_type, data = formatter(img)+ assert mime_type == "text/html"+ assert "https://example.com/img.gif" in data+ assert "<img" in data++@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")@patch("marimo._runtime.output._output.append")def test_display_html(mock_append: MagicMock):