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
improvement: impove default DPI on matplotlib images (#7093)
Myles Scolnick0892064150ea
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsxindex 6d404d53b1..64bf1d7c2b 100644--- a/frontend/src/components/editor/Output.tsx+++ b/frontend/src/components/editor/Output.tsx@@ -8,7 +8,7 @@ import React, {useState,} from "react";import { type CellId, CellOutputId } from "@/core/cells/ids";-import type { OutputMessage } from "@/core/kernel/messages";+import type { CellOutput, OutputMessage } from "@/core/kernel/messages";import { cn } from "@/utils/cn";import { logNever } from "../../utils/assertNever";import { ErrorBoundary } from "./boundary/ErrorBoundary";@@ -44,7 +44,16 @@ import { CsvViewer } from "./file-tree/renderers";import { MarimoTracebackOutput } from "./output/MarimoTracebackOutput";import { renderMimeIcon } from "./renderMimeIcon";-type MimeBundle = Record<OutputMessage["mimetype"], { [key: string]: unknown }>;+const METADATA_KEY = "__metadata__";++type MimeBundleWithoutMetadata = Record<+ OutputMessage["mimetype"],+ { [key: string]: unknown }+>;++type MimeBundle = MimeBundleWithoutMetadata & {+ [METADATA_KEY]?: Record<string, { width?: number; height?: number }>;+};type MimeBundleOrTuple = MimeBundle | [MimeBundle, { [key: string]: unknown }];export type OnRefactorWithAI = (opts: {@@ -60,8 +69,9 @@ export const OutputRenderer: React.FC<{cellId?: CellId;onRefactorWithAI?: OnRefactorWithAI;wrapText?: boolean;+ metadata?: { width?: number; height?: number };}> = memo((props) => {- const { message, onRefactorWithAI, cellId, wrapText } = props;+ const { message, onRefactorWithAI, cellId, wrapText, metadata } = props;const { theme } = useTheme();// Memoize parsing the json data@@ -123,7 +133,15 @@ export const OutputRenderer: React.FC<{typeof data === "string",`Expected string data for mime=${mimetype}. Got ${typeof data}`,);- return <ImageOutput className={channel} src={data} alt="" />;+ return (+ <ImageOutput+ className={channel}+ src={data}+ alt=""+ width={metadata?.width}+ height={metadata?.height}+ />+ );case "image/svg+xml":invariant(typeof data === "string",@@ -230,14 +248,25 @@ const MimeBundleOutputRenderer: React.FC<{}> = memo(({ data, channel, cellId }) => {const mimebundle = Array.isArray(data) ? data[0] : data;+ // Extract metadata if present (e.g., for retina image rendering)+ const metadata = mimebundle[METADATA_KEY];++ // Filter out metadata from the mime entries and type narrow+ const mimeEntries = Objects.entries(mimebundle as Record<string, unknown>)+ .filter(([key]) => key !== METADATA_KEY)+ .map(+ ([mime, data]) =>+ [mime, data] as [OutputMessage["mimetype"], CellOutput["data"]],+ );+// If there is none, return null- const first = Objects.keys(mimebundle)[0];+ const first = mimeEntries[0]?.[0];if (!first) {return null;}// If there is only one mime type, render it directly- if (Object.keys(mimebundle).length === 1) {+ if (mimeEntries.length === 1) {return (<OutputRenderercellId={cellId}@@ -246,11 +275,11 @@ const MimeBundleOutputRenderer: React.FC<{data: mimebundle[first],mimetype: first,}}+ metadata={metadata?.[first]}/>);}- const mimeEntries = Objects.entries(mimebundle);// Sort HTML firstmimeEntries.sort(([mimeA], [_mimeB]) => {if (mimeA === "text/html") {@@ -286,6 +315,7 @@ const MimeBundleOutputRenderer: React.FC<{data: output,mimetype: mime,}}+ metadata={metadata?.[mime]}/></ErrorBoundary></TabsContent>diff --git a/frontend/test_retina.py b/frontend/test_retina.pynew file mode 100644index 0000000000..cbefaa0736--- /dev/null+++ b/frontend/test_retina.py@@ -0,0 +1,47 @@+import marimo++__generated_with = "0.17.7"+app = marimo.App(width="medium")+++def _():+ return+++def _():+ import marimo as mo+ import matplotlib.pyplot as plt+ import numpy as np+ return np, plt+++def _(np, plt):+ # Test from the original issue+ x = np.random.rand(27)+ y = 20 - np.linspace(2, 20, 27) * x++ plt.figure(figsize=(6, 4))+ plt.scatter(x, y)+ plt.title("Retina Test - Should be crisp!")+ plt.xlabel("X values")+ plt.ylabel("Y values")+ plt.gca()+ return+++def _(plt):+ # Another test with a line plot+ fig, ax = plt.subplots(figsize=(8, 6))+ ax.plot([1, 2, 3, 4], [1, 4, 2, 3], marker="o", linestyle="-", linewidth=2)+ ax.set_title("Line Plot - Retina Test")+ ax.grid(True)+ fig+ return+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_output/formatters/matplotlib_formatters.py b/marimo/_output/formatters/matplotlib_formatters.pyindex 095fb95d66..91a49bd6cf 100644--- a/marimo/_output/formatters/matplotlib_formatters.py+++ b/marimo/_output/formatters/matplotlib_formatters.py@@ -25,24 +25,15 @@ class MatplotlibFormatter(FormatterFactory):if running_in_notebook():matplotlib.use("module://marimo._output.mpl")- import base64- import io-from matplotlib.artist import Artist # type: ignorefrom matplotlib.container import BarContainer # type: ignorefrom marimo._output import formatting- from marimo._utils.data_uri import build_data_urldef mime_data_artist(artist: Artist) -> tuple[KnownMimeType, str]:- buf = io.BytesIO()- artist.figure.savefig(buf, format="png", bbox_inches="tight") # type: ignore- mimetype: KnownMimeType = "image/png"- plot_bytes = base64.b64encode(buf.getvalue())- return (- mimetype,- build_data_url(mimetype=mimetype, data=plot_bytes),- )+ from marimo._output.mpl import _render_figure_mimebundle++ return _render_figure_mimebundle(artist.figure.canvas) # type: ignore# monkey-patch a _mime_ method, instead of using a formatter, because# we want all subclasses of Artist to inherit this renderer.diff --git a/marimo/_output/mpl.py b/marimo/_output/mpl.pyindex 3d20b02b67..c9ebe0a656 100644--- a/marimo/_output/mpl.py+++ b/marimo/_output/mpl.py@@ -14,6 +14,8 @@ from __future__ import annotationsimport base64import io+import json+import structfrom typing import Optionalimport matplotlib.pyplot as plt@@ -37,17 +39,78 @@ def close_figures() -> None:plt.close("all")-def _internal_show(canvas: FigureCanvasBase) -> None:+def _extract_png_dimensions(png_bytes: bytes) -> tuple[int, int]:+ """Extract width and height from PNG binary data.++ Args:+ png_bytes: Raw PNG file data++ Returns:+ Tuple of (width, height) in pixels+ """+ # Find IHDR chunk and extract dimensions+ ihdr_index = png_bytes.index(b"IHDR")+ # Next 8 bytes after IHDR are width (4 bytes) and height (4 bytes)+ width, height = struct.unpack(+ ">II", png_bytes[ihdr_index + 4 : ihdr_index + 12]+ )+ return width, height+++def _render_figure_mimebundle(+ fig: FigureCanvasBase,+) -> tuple[KnownMimeType, str]:+ """Render a matplotlib figure as a mimebundle with retina support.++ Args:+ fig: Matplotlib figure canvas to render++ Returns:+ Tuple of (mimetype, json_data) where json_data is a mimebundle+ containing the PNG data URL and display metadata+ """buf = io.BytesIO()- buf.seek(0)- canvas.figure.savefig(buf, format="png", bbox_inches="tight") # type: ignore[attr-defined]++ # Get current DPI and double it for retina display (like Jupyter)+ original_dpi = fig.figure.dpi # type: ignore[attr-defined]+ retina_dpi = original_dpi * 2++ fig.figure.savefig(buf, format="png", bbox_inches="tight", dpi=retina_dpi) # type: ignore[attr-defined]++ png_bytes = buf.getvalue()+ plot_bytes = base64.b64encode(png_bytes)++ image_mimetype: KnownMimeType = "image/png"+ data_url = build_data_url(mimetype=image_mimetype, data=plot_bytes)++ try:+ # Extract dimensions from the PNG+ width, height = _extract_png_dimensions(png_bytes)+ mimebundle = {+ "image/png": data_url,+ "__metadata__": {+ "image/png": {+ "width": width // 2,+ "height": height // 2,+ }+ },+ }+ return (+ "application/vnd.marimo+mimebundle",+ json.dumps(mimebundle),+ )+ except (ValueError, struct.error, IndexError):+ # Fall back to plain image if dimension extraction fails+ return (image_mimetype, data_url)+++def _internal_show(canvas: FigureCanvasBase) -> None:+ mimetype, data = _render_figure_mimebundle(canvas)plt.close(canvas.figure)- mimetype: KnownMimeType = "image/png"- plot_bytes = base64.b64encode(buf.getvalue())CellOp.broadcast_console_output(channel=CellChannel.MEDIA,mimetype=mimetype,- data=build_data_url(mimetype=mimetype, data=plot_bytes),+ data=data,cell_id=None,status=None,)diff --git a/tests/_output/formatters/test_matplotlib.py b/tests/_output/formatters/test_matplotlib.pyindex b903a50150..891374b3a6 100644--- a/tests/_output/formatters/test_matplotlib.py+++ b/tests/_output/formatters/test_matplotlib.py@@ -1,5 +1,8 @@from __future__ import annotations+import base64+import json+import pytestfrom marimo._dependencies.dependencies import DependencyManager@@ -65,3 +68,178 @@ async def test_matplotlib_rc_dark(rcParams = executing_kernel.globals["rcParams"]assert rcParams["font.family"] == ["monospace"]assert rcParams["figure.facecolor"] == "black"+++def _extract_png_dimensions(data_url: str) -> tuple[int, int]:+ """Extract width and height from a PNG data URL."""+ from marimo._output.mpl import _extract_png_dimensions as extract_dims++ if data_url.startswith("data:image/png;base64,"):+ base64_data = data_url[len("data:image/png;base64,") :]+ else:+ raise ValueError("Not a PNG data URL")++ png_bytes = base64.b64decode(base64_data)+ return extract_dims(png_bytes)++[email protected](not HAS_MPL, reason="optional dependencies not installed")+async def test_matplotlib_retina_rendering(+ executing_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """Test that matplotlib figures are rendered at 2x DPI for retina displays."""+ from marimo._output.formatters.formatters import register_formatters++ register_formatters(theme="light")++ await executing_kernel.run(+ [+ exec_req.get(+ """+ import matplotlib.pyplot as plt++ # Create a simple figure+ fig, ax = plt.subplots(figsize=(4, 3))+ ax.plot([1, 2, 3], [1, 2, 3])++ # Get the formatted output+ result = fig._mime_()+ """+ )+ ]+ )++ # Get the formatted result from kernel globals+ mime_type, data = executing_kernel.globals["result"]++ # Check that it's a mimebundle+ assert mime_type == "application/vnd.marimo+mimebundle"+ assert isinstance(data, str)++ # Parse the mimebundle+ mimebundle = json.loads(data)+ assert "image/png" in mimebundle++ # Extract PNG data and check dimensions+ png_data_url = mimebundle["image/png"]+ width, height = _extract_png_dimensions(png_data_url)++ # Verify it's rendering at high DPI (should be significantly larger than+ # the base figsize in pixels). At 2x DPI, a 4x3 inch figure should be+ # at least 500x400 pixels (allowing for different base DPI values)+ # The exact value depends on matplotlib's default DPI (can be 72, 90, 100, etc.)+ assert width >= 500, f"Expected high-res width (>500px), got {width}"+ assert height >= 350, f"Expected high-res height (>350px), got {height}"++ # Verify aspect ratio is preserved (4:3 ratio)+ aspect_ratio = width / height+ expected_ratio = 4.0 / 3.0+ assert abs(aspect_ratio - expected_ratio) < 0.1, (+ f"Expected aspect ratio ~{expected_ratio}, got {aspect_ratio}"+ )++[email protected](not HAS_MPL, reason="optional dependencies not installed")+async def test_matplotlib_retina_metadata(+ executing_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """Test that matplotlib figures include proper width/height metadata."""+ from marimo._output.formatters.formatters import register_formatters++ register_formatters(theme="light")++ await executing_kernel.run(+ [+ exec_req.get(+ """+ import matplotlib.pyplot as plt++ # Create a simple figure+ fig, ax = plt.subplots(figsize=(4, 3))+ ax.plot([1, 2, 3], [1, 2, 3])+ result = fig._mime_()+ """+ )+ ]+ )++ # Get the formatted result from kernel globals+ mime_type, data = executing_kernel.globals["result"]+ assert mime_type == "application/vnd.marimo+mimebundle"+ mimebundle_data = json.loads(data)++ # Check for metadata+ assert "__metadata__" in mimebundle_data, (+ "Mimebundle should include metadata"+ )+ metadata = mimebundle_data["__metadata__"]+ assert "image/png" in metadata, (+ "Metadata should include image/png dimensions"+ )++ # Extract actual PNG dimensions+ png_data_url = mimebundle_data["image/png"]+ actual_width, actual_height = _extract_png_dimensions(png_data_url)++ # Metadata dimensions should be half of actual (for retina display)+ png_metadata = metadata["image/png"]+ assert "width" in png_metadata+ assert "height" in png_metadata++ metadata_width = png_metadata["width"]+ metadata_height = png_metadata["height"]++ # Metadata should be approximately half the actual PNG dimensions+ assert abs(metadata_width - actual_width // 2) <= 2, (+ f"Metadata width {metadata_width} should be ~half of actual {actual_width}"+ )+ assert abs(metadata_height - actual_height // 2) <= 2, (+ f"Metadata height {metadata_height} should be ~half of actual {actual_height}"+ )++[email protected](not HAS_MPL, reason="optional dependencies not installed")+async def test_matplotlib_backwards_compatibility(+ executing_kernel: Kernel, exec_req: ExecReqProvider+) -> None:+ """Test that existing matplotlib code still works with retina rendering."""+ from marimo._output.formatters.formatters import register_formatters++ register_formatters(theme="light")++ # Test various matplotlib output types+ await executing_kernel.run(+ [+ exec_req.get(+ """+ import matplotlib.pyplot as plt+ import numpy as np++ # Test 1: Simple plot+ fig1, ax1 = plt.subplots()+ ax1.plot([1, 2, 3], [1, 2, 3])+ result1 = fig1._mime_()++ # Test 2: Scatter plot+ x = np.random.rand(27)+ y = 20 - np.linspace(2, 20, 27) * x+ fig2, ax2 = plt.subplots()+ ax2.scatter(x, y)+ result2 = fig2._mime_()++ # Test 3: Bar chart+ fig3, ax3 = plt.subplots()+ bars = ax3.bar(['A', 'B', 'C'], [1, 2, 3])+ result3 = fig3._mime_()+ """+ )+ ]+ )++ # Check all outputs were generated successfully+ for result_name in ["result1", "result2", "result3"]:+ mime_type, data = executing_kernel.globals[result_name]+ # Should produce valid output (mimebundle with PNG)+ assert mime_type == "application/vnd.marimo+mimebundle"+ mimebundle = json.loads(data)+ assert "image/png" in mimebundlediff --git a/tests/_output/formatters/test_structures.py b/tests/_output/formatters/test_structures.pyindex 1c6d57a09b..8bae83cf22 100644--- a/tests/_output/formatters/test_structures.py+++ b/tests/_output/formatters/test_structures.py@@ -49,7 +49,11 @@ async def test_matplotlib_special_case(formatter = executing_kernel.globals["formatter"]lines = executing_kernel.globals["lines"]assert formatter is not None- assert formatter(lines)[0].startswith("image")+ mimetype = formatter(lines)[0]+ assert (+ mimetype.startswith("image")+ or mimetype == "application/vnd.marimo+mimebundle"+ )def test_format_structure_types() -> None: