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 with large plot sizes (#767)
Myles Scolnick4d7a4c3f4b37
diff --git a/marimo/_output/builder.py b/marimo/_output/builder.pyindex 4b1f98c43d..f765cdce38 100644--- a/marimo/_output/builder.py+++ b/marimo/_output/builder.py@@ -88,6 +88,7 @@ class _HTMLBuilder:@staticmethoddef iframe(src: Optional[str] = None,+ srcdoc: Optional[str] = None,width: Optional[str] = None,height: Optional[str] = None,style: Optional[str] = None,@@ -96,6 +97,8 @@ class _HTMLBuilder:params: List[Tuple[str, Union[str, None]]] = []if src:params.append(("src", src))+ if srcdoc:+ params.append(("srcdoc", srcdoc))if width:params.append(("width", width))if height:diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.pyindex 79c1ad5b54..817a8808dc 100644--- a/marimo/_plugins/stateless/mpl/_mpl.py+++ b/marimo/_plugins/stateless/mpl/_mpl.py@@ -8,6 +8,7 @@ Adapted from https://matplotlib.org/stable/gallery/user_interfaces/embedding_webfrom __future__ import annotationsimport asyncio+import htmlimport ioimport mimetypesimport signal@@ -55,6 +56,18 @@ class FigureManagers:figure_managers = FigureManagers()+def _template(+ host: str,+ port: int,+ fig_id: str,+) -> str:+ return html_content % {+ "ws_uri": f"ws://{host}:{port}/ws?figure={fig_id}",+ "fig_id": fig_id,+ "base_url": f"http://{host}:{port}",+ }++def create_application(host: str,port: int,@@ -67,13 +80,7 @@ def create_application(async def main_page(request: Request) -> HTMLResponse:figure_id = request.query_params.get("figure")assert figure_id is not None- ws_uri = f"ws://{host}:{port}/ws?figure={figure_id}"-- content = html_content % {- "ws_uri": ws_uri,- "fig_id": figure_id,- "custom_css": css_content,- }+ content = _template(host, port, figure_id)return HTMLResponse(content=content)async def mpl_js(request: Request) -> Response:@@ -83,6 +90,13 @@ def create_application(media_type="application/javascript",)+ async def mpl_custom_css(request: Request) -> Response:+ del request+ return Response(+ content=css_content,+ media_type="text/css",+ )+async def download(request: Request) -> Response:figure_id = request.query_params.get("figure")assert figure_id is not None@@ -145,6 +159,7 @@ def create_application(routes=[Route("/", main_page, methods=["GET"]),Route("/mpl/mpl.js", mpl_js, methods=["GET"]),+ Route("/mpl/custom.css", mpl_custom_css, methods=["GET"]),Route("/download.{fmt}", download, methods=["GET"]),WebSocketRoute("/ws", websocket_endpoint),Mount(@@ -265,9 +280,12 @@ def interactive(figure: "Figure | Axes") -> Html: # type: ignore[name-defined]ctx.cell_lifecycle_registry.add(CleanupHandle())ctx.stream.cell_id = ctx.kernel.execution_context.cell_id+ content = _template(host, port, str(figure_manager.num))+return Html(h.iframe(- src=f"http://{host}:{port}?figure={figure_manager.num}",+ # srcdoc allows us to use __resizeIframe+ srcdoc=html.escape(content),width="100%",height="550px",onload="__resizeIframe(this)",@@ -278,15 +296,14 @@ def interactive(figure: "Figure | Axes") -> Html: # type: ignore[name-defined]html_content = """<!DOCTYPE html><html lang="en">+ <base href="%(base_url)s" /><head><link rel="stylesheet" href="mpl/_static/css/page.css" type="text/css" /><link rel="stylesheet" href="mpl/_static/css/boilerplate.css" type="text/css" /><link rel="stylesheet" href="mpl/_static/css/fbm.css" type="text/css" /><link rel="stylesheet" href="mpl/_static/css/mpl.css" type="text/css" />+ <link rel="stylesheet" href="mpl/custom.css" type="text/css" /><script src="mpl/mpl.js"></script>- <style>- %(custom_css)s- </style><script>function ondownload(figure, format) {