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: don't patch plotly show when running as a script (#4493)

Akshay Agrawale3cbe33a0ded
diff --git a/marimo/_output/formatters/plotly_formatters.py b/marimo/_output/formatters/plotly_formatters.py
index 8b4d0b0fd8..bb29e5d2bc 100644
--- a/marimo/_output/formatters/plotly_formatters.py
+++ b/marimo/_output/formatters/plotly_formatters.py
@@ -11,6 +11,7 @@ from marimo._messaging.ops import CellOp
from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._output.hypertext import Html
from marimo._plugins.core.web_component import build_stateless_plugin
+from marimo._runtime.context.utils import running_in_notebook
class PlotlyFormatter(FormatterFactory):
@@ -24,35 +25,39 @@ class PlotlyFormatter(FormatterFactory):
from marimo._output import formatting
- @formatting.formatter(plotly.graph_objects.Figure)
- @formatting.formatter(plotly.graph_objects.FigureWidget)
- def _show_plotly_figure(
- fig: plotly.graph_objects.Figure,
- ) -> tuple[KnownMimeType, str]:
- dragmode = getattr(fig.layout, "dragmode", None)
- if dragmode is None:
- # Users are accustomed to default zoom.
- fig.update_layout(dragmode="zoom")
- json_str: str = pio.to_json(fig)
- plugin = PlotlyFormatter.render_plotly_dict(json.loads(json_str))
- return ("text/html", plugin.text)
+ if running_in_notebook():
- # Patch Figure.show to add to console output instead of opening a
- # browser.
- def patched_show(
- self: plotly.graph_objects.Figure, *args: Any, **kwargs: Any
- ) -> None:
- del args, kwargs
- mimetype, data = _show_plotly_figure(self)
- CellOp.broadcast_console_output(
- channel=CellChannel.MEDIA,
- mimetype=mimetype,
- data=data,
- cell_id=None,
- status=None,
- )
+ @formatting.formatter(plotly.graph_objects.Figure)
+ @formatting.formatter(plotly.graph_objects.FigureWidget)
+ def _show_plotly_figure(
+ fig: plotly.graph_objects.Figure,
+ ) -> tuple[KnownMimeType, str]:
+ dragmode = getattr(fig.layout, "dragmode", None)
+ if dragmode is None:
+ # Users are accustomed to default zoom.
+ fig.update_layout(dragmode="zoom")
+ json_str: str = pio.to_json(fig)
+ plugin = PlotlyFormatter.render_plotly_dict(
+ json.loads(json_str)
+ )
+ return ("text/html", plugin.text)
+
+ # Patch Figure.show to add to console output instead of opening a
+ # browser.
+ def patched_show(
+ self: plotly.graph_objects.Figure, *args: Any, **kwargs: Any
+ ) -> None:
+ del args, kwargs
+ mimetype, data = _show_plotly_figure(self)
+ CellOp.broadcast_console_output(
+ channel=CellChannel.MEDIA,
+ mimetype=mimetype,
+ data=data,
+ cell_id=None,
+ status=None,
+ )
- plotly.graph_objects.Figure.show = patched_show
+ plotly.graph_objects.Figure.show = patched_show
@staticmethod
def render_plotly_dict(json: dict[Any, Any]) -> Html:
diff --git a/marimo/_smoke_tests/plotly/fig_show_run_as_script.py b/marimo/_smoke_tests/plotly/fig_show_run_as_script.py
new file mode 100644
index 0000000000..cca6654a6c
--- /dev/null
+++ b/marimo/_smoke_tests/plotly/fig_show_run_as_script.py
@@ -0,0 +1,16 @@
+import marimo
+
+__generated_with = "0.12.8"
+app = marimo.App(width="full")
+
+
+def _():
+ import plotly.express as px
+ fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
+ fig.show()
+ return fig, px
+
+
+if __name__ == "__main__":
+ app.run()