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: mpl.interactive becomes blank with large plots (#8285)
Myles Scolnicka028fdd33544
diff --git a/marimo/_server/api/endpoints/mpl.py b/marimo/_server/api/endpoints/mpl.pyindex 449a254351..f6002491f1 100644--- a/marimo/_server/api/endpoints/mpl.py+++ b/marimo/_server/api/endpoints/mpl.py@@ -26,6 +26,12 @@ router = APIRouter()figure_endpoints: dict[int, str] = {}+# Maximum size for WebSocket messages received from the matplotlib server.+# Matplotlib's WebAgg backend sends rendered figure PNGs over WebSocket,+# which can exceed the websockets library's default 1 MB limit at higher+# DPI settings or larger figure sizes.+WS_MAX_SIZE = 20 * 1024 * 1024 # 20 MB+def mpl_fallback_handler(path_prefix: str = "",@@ -265,7 +271,9 @@ async def mpl_websocket(websocket: WebSocket) -> None:passtry:- async with websockets.connect(target_ws_url) as mpl_ws:+ async with websockets.connect(+ target_ws_url, max_size=WS_MAX_SIZE+ ) as mpl_ws:async def forward_to_mpl() -> None:try:diff --git a/marimo/_smoke_tests/issues/8184_mpl_interactive_large_plot.py b/marimo/_smoke_tests/issues/8184_mpl_interactive_large_plot.pynew file mode 100644index 0000000000..378f606a5a--- /dev/null+++ b/marimo/_smoke_tests/issues/8184_mpl_interactive_large_plot.py@@ -0,0 +1,44 @@+import marimo++__generated_with = "0.19.11"+app = marimo.App()+++def _():+ import marimo as mo+ import matplotlib.pyplot as plt+ import numpy as np++ return mo, np, plt+++def _(mo, np, plt):+ # n=200 should work+ plt.figure()+ plt.imshow(np.random.random([200, 200]))+ mo.mpl.interactive(plt.gca())+ return+++def _(mo, np, plt):+ # n=300 was blank before fix (GH-8184)+ plt.figure()+ plt.imshow(np.random.random([300, 300]))+ mo.mpl.interactive(plt.gca())+ return+++def _(mo, np, plt):+ # n=500 stress test+ plt.figure()+ plt.imshow(np.random.random([500, 500]))+ mo.mpl.interactive(plt.gca())+ return+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_smoke_tests/plotly/_plotly_resampler.py b/marimo/_smoke_tests/plotly/_plotly_resampler.pyindex 04af3bf032..dbac0a8a9f 100644--- a/marimo/_smoke_tests/plotly/_plotly_resampler.py+++ b/marimo/_smoke_tests/plotly/_plotly_resampler.py@@ -1,17 +1,20 @@# /// script# requires-python = ">=3.12"# dependencies = [-# "ipywidgets==8.1.5",-# "numpy==2.2.3",-# "pandas==2.2.3",-# "plotly==5.24.1",-# "plotly-resampler==0.10.0",+# "anywidget==0.9.21",+# "ipywidgets==8.1.8",+# "marimo",+# "numpy==2.4.2",+# "pandas==3.0.0",+# "plotly==6.5.2",+# "plotly-resampler==0.11.0",+# "pytz==2025.2",# ]# ///import marimo-__generated_with = "0.15.5"+__generated_with = "0.19.11"app = marimo.App(width="medium")@@ -21,13 +24,14 @@ def _():import pandas as pdimport plotly.express as pximport ipywidgets+ import anywidgetfrom plotly_resampler import register_plotly_resampler# Register the resampler (using widget mode for interactive performance)register_plotly_resampler(mode="widget")# Generate 1 million data points- n_points = 1_000_000+ n_points = 10_000_000np.random.seed(42) # for reproducibility# Create a DataFrame with x, y, and color columnsdiff --git a/marimo/_smoke_tests/plotly/_plotly_resampler_0_11.py b/marimo/_smoke_tests/plotly/_plotly_resampler_0_11.pydeleted file mode 100644index b43ae3ac02..0000000000--- a/marimo/_smoke_tests/plotly/_plotly_resampler_0_11.py+++ /dev/null@@ -1,55 +0,0 @@-# /// script-# requires-python = ">=3.12"-# dependencies = [-# "anywidget==0.9.15",-# "ipywidgets==8.1.5",-# "marimo",-# "numpy==2.2.3",-# "pandas==2.2.3",-# "plotly==6.0.0",-# "plotly-resampler==0.11.0rc0",-# ]-# ///--import marimo--__generated_with = "0.15.5"-app = marimo.App(width="medium")---def _():- import numpy as np- import pandas as pd- import plotly.express as px- import anywidget- from plotly_resampler import register_plotly_resampler-- # Register the resampler (using widget mode for interactive performance)- register_plotly_resampler(mode="widget")-- # Generate 1 million data points- n_points = 1_000_000- np.random.seed(42) # for reproducibility-- # Create a DataFrame with x, y, and color columns- df = pd.DataFrame(- {- "x": np.linspace(0, 100, n_points),- "y": np.sin(np.linspace(0, 100, n_points))- + np.random.normal(0, 0.5, n_points),- "color": np.random.choice(["A", "B", "C"], n_points),- }- )-- # Create a scatter plot with Plotly Express- fig = px.scatter(- df, x="x", y="y", color="color", title="Scatter Plot with 1M Datapoints"- )-- fig- return---if __name__ == "__main__":- app.run()diff --git a/tests/_server/api/endpoints/test_mpl_endpoints.py b/tests/_server/api/endpoints/test_mpl_endpoints.pyindex b7bd3b455d..bfb7074ae7 100644--- a/tests/_server/api/endpoints/test_mpl_endpoints.py+++ b/tests/_server/api/endpoints/test_mpl_endpoints.py@@ -4,7 +4,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKINGfrom unittest.mock import AsyncMock, MagicMock, patch-from marimo._server.api.endpoints.mpl import figure_endpoints+from marimo._server.api.endpoints.mpl import WS_MAX_SIZE, figure_endpointsif TYPE_CHECKING:from starlette.testclient import TestClient@@ -178,3 +178,6 @@ class TestMatplotlibProxyEndpoints:mock_connect.assert_called()call_url = mock_connect.call_args[0][0]assert "localhost:8888" in call_url+ # Verify max_size is set to handle large matplotlib PNGs+ call_kwargs = mock_connect.call_args[1]+ assert call_kwargs.get("max_size") == WS_MAX_SIZE