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
Forward config parameter from plotly `fig.show()` (#5880)
Trevor Manz7d08f4c644a8
diff --git a/marimo/_output/formatters/plotly_formatters.py b/marimo/_output/formatters/plotly_formatters.pyindex 8b01b2d9da..f505dd5306 100644--- a/marimo/_output/formatters/plotly_formatters.py+++ b/marimo/_output/formatters/plotly_formatters.py@@ -27,10 +27,9 @@ class PlotlyFormatter(FormatterFactory):if running_in_notebook():- @formatting.formatter(plotly.graph_objects.Figure)- @formatting.formatter(plotly.graph_objects.FigureWidget)def _show_plotly_figure(fig: plotly.graph_objects.Figure,+ config: dict[str, Any] | None = None,) -> tuple[KnownMimeType, str]:dragmode = getattr(fig.layout, "dragmode", None)if dragmode is None:@@ -38,17 +37,27 @@ class PlotlyFormatter(FormatterFactory):fig.update_layout(dragmode="zoom")json_str: str = pio.to_json(fig)plugin = PlotlyFormatter.render_plotly_dict(- json.loads(json_str)+ json.loads(json_str), config=config)return ("text/html", plugin.text)+ @formatting.formatter(plotly.graph_objects.Figure)+ @formatting.formatter(plotly.graph_objects.FigureWidget)+ def _plotly_formatter(+ fig: plotly.graph_objects.Figure,+ ) -> tuple[KnownMimeType, str]:+ return _show_plotly_figure(fig)+# 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+ self: plotly.graph_objects.Figure,+ *args: Any, # noqa: ARG001+ **kwargs: Any,) -> None:- del args, kwargs- mimetype, data = _show_plotly_figure(self)+ # Extract config if provided+ config = kwargs.get("config")+ mimetype, data = _show_plotly_figure(self, config=config)CellOp.broadcast_console_output(channel=CellChannel.MEDIA,mimetype=mimetype,@@ -60,7 +69,9 @@ class PlotlyFormatter(FormatterFactory):plotly.graph_objects.Figure.show = patched_show@staticmethod- def render_plotly_dict(json: dict[Any, Any]) -> Html:+ def render_plotly_dict(+ json: dict[Any, Any], config: dict[str, Any] | None = None+ ) -> Html:import plotly.io as pio # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501resolved_config: dict[str, Any] = {}@@ -78,6 +89,10 @@ class PlotlyFormatter(FormatterFactory):except (AttributeError, KeyError):pass+ # Merge with any config passed via show()+ if config is not None:+ resolved_config = {**resolved_config, **config}+return Html(build_stateless_plugin(component_name="marimo-plotly",diff --git a/pyproject.toml b/pyproject.tomlindex 77ac13e11b..3838aa0e96 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -242,6 +242,7 @@ extra-dependencies = ["pyarrow>=19.0.1","pyarrow_hotfix","altair>=5.4.0",+ "plotly>=5.14.0","polars>=1.32.0","sqlglot>=23.4","sqlalchemy>=2.0.40",diff --git a/tests/_output/formatters/test_plotly_formatters.py b/tests/_output/formatters/test_plotly_formatters.pynew file mode 100644index 0000000000..be48358e24--- /dev/null+++ b/tests/_output/formatters/test_plotly_formatters.py@@ -0,0 +1,33 @@+from __future__ import annotations++import json++import pytest++from marimo._dependencies.dependencies import DependencyManager+from marimo._output.formatters.formatters import register_formatters+from marimo._output.formatters.plotly_formatters import PlotlyFormatter++HAS_DEPS = DependencyManager.plotly.has()++[email protected](not HAS_DEPS, reason="plotly not installed")+def test_plotly_config_forwarding():+ """Test that config parameter is properly forwarded"""+ register_formatters()++ import plotly.graph_objects as go+ import plotly.io as pio++ fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[4, 5, 6])])++ json_str = pio.to_json(fig)+ json_dict = json.loads(json_str)++ config = {"displayModeBar": False, "responsive": True}+ result = PlotlyFormatter.render_plotly_dict(json_dict, config=config)++ assert "marimo-plotly" in result.text+ assert "data-config=" in result.text+ assert "displayModeBar" in result.text+ assert "responsive" in result.text