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: return data URI when matplotlib cannot render interactively (#8722)
Myles Scolnick43188cf1dad9
diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.pyindex 072bf57d93..fbaaabf7c8 100644--- a/marimo/_plugins/stateless/mpl/_mpl.py+++ b/marimo/_plugins/stateless/mpl/_mpl.py@@ -19,6 +19,7 @@ from marimo._runtime.context import (get_context,)from marimo._runtime.context.kernel_context import KernelRuntimeContext+from marimo._utils.data_uri import build_data_urlLOGGER = _loggers.marimo_logger()@@ -71,7 +72,10 @@ class NonInteractiveMplHtml(Html):super().__init__(as_html(figure).text)def _mime_(self) -> tuple[KnownMimeType, str]:- return ("image/png", png_bytes(self._figure).decode())+ data_url = build_data_url(+ mimetype="image/png", data=png_bytes(self._figure)+ )+ return ("image/png", data_url)@mddocdiff --git a/tests/_plugins/stateless/test_mpl.py b/tests/_plugins/stateless/test_mpl.pyindex 7b60561e7f..f2a71797b4 100644--- a/tests/_plugins/stateless/test_mpl.py+++ b/tests/_plugins/stateless/test_mpl.py@@ -90,6 +90,31 @@ def test_patch_javascript() -> None:assert javascript.count("// canvas_div.focus();") == 1[email protected]("matplotlib")+def test_non_interactive_mpl_mime_returns_data_uri() -> None:+ """Test that NonInteractiveMplHtml._mime_ returns a data URI."""+ import base64++ import matplotlib.pyplot as plt++ from marimo._plugins.stateless.mpl._mpl import NonInteractiveMplHtml++ fig, ax = plt.subplots()+ ax.plot([1, 2, 3], [1, 2, 3])++ html = NonInteractiveMplHtml(fig)+ mime_type, data = html._mime_()+ assert mime_type == "image/png"+ assert data.startswith("data:image/png;base64,")++ # Decode the base64 payload and verify it's a valid PNG.+ b64_payload = data.split(",", 1)[1]+ raw_png = base64.b64decode(b64_payload)+ assert raw_png.startswith(b"\x89PNG")++ plt.close(fig)++@pytest.mark.requires("matplotlib")def test_mpl_interactive_fallback_when_virtual_files_not_supported() -> None:"""Test that mpl.interactive falls back to PNG when virtual_files_supported=False."""