Skip to content

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.

fix: mpl.interactive should not hang when running as a script (#1870)

Akshay Agrawalf92506efdeeb
diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.py
index b4bb0b2676..af07ff07cb 100644
--- a/marimo/_plugins/stateless/mpl/_mpl.py
+++ b/marimo/_plugins/stateless/mpl/_mpl.py
@@ -20,14 +20,15 @@ from typing import TYPE_CHECKING, Any, Optional, Tuple
from starlette.websockets import WebSocketState
from marimo._output.builder import h
+from marimo._output.formatting import as_html
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._runtime.cell_lifecycle_item import CellLifecycleItem
from marimo._runtime.context import (
- ContextNotInitializedError,
RuntimeContext,
get_context,
)
+from marimo._runtime.context.kernel_context import KernelRuntimeContext
from marimo._server.utils import find_free_port
from marimo._utils.signals import get_signals
@@ -259,12 +260,9 @@ def interactive(figure: "Figure | Axes") -> Html: # type: ignore[name-defined]
if isinstance(figure, Axes):
figure = figure.get_figure()
- try:
- ctx = get_context()
- except ContextNotInitializedError as err:
- raise RuntimeError(
- "marimo.mpl.interactive can't be used when running as a script."
- ) from err
+ ctx = get_context()
+ if not isinstance(ctx, KernelRuntimeContext):
+ return as_html(figure)
# Figure Manager, Any type because matplotlib doesn't have typings
figure_manager = new_figure_manager_given_figure(id(figure), figure)