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.

improvement: support plotly config and renderers (#1170)

Myles Scolnick96b32de96bf0
diff --git a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
index 9f6c1cbe26..592378ed0b 100644
--- a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
+++ b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
@@ -13,9 +13,11 @@ import useEvent from "react-use-event-hook";
import { PlotlyTemplateParser, createParser } from "./parse-from-template";
import { Objects } from "@/utils/objects";
import { set } from "lodash-es";
+import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
interface Data {
figure: Figure;
+ config: Partial<Plotly.Config>;
}
type AxisName = string;
@@ -46,6 +48,7 @@ export class PlotlyPlugin implements IPlugin<T, Data> {
.object({})
.passthrough()
.transform((spec) => spec as unknown as Figure),
+ config: z.object({}).passthrough(),
});
render(props: IPluginProps<T, Data>): JSX.Element {
@@ -81,7 +84,7 @@ function initialLayout(figure: Figure): Partial<Plotly.Layout> {
}
export const PlotlyComponent = memo(
- ({ figure, value, setValue }: PlotlyPluginProps) => {
+ ({ figure, value, setValue, config }: PlotlyPluginProps) => {
const [layout, setLayout] = useState<Partial<Plotly.Layout>>(() => {
return {
...initialLayout(figure),
@@ -98,7 +101,7 @@ export const PlotlyComponent = memo(
setNonce((prev) => prev + 1);
});
- const config = useMemo((): Partial<Plotly.Config> => {
+ const plotlyConfig = useMemo((): Partial<Plotly.Config> => {
return {
displaylogo: false,
modeBarButtonsToAdd: [
@@ -117,8 +120,11 @@ export const PlotlyComponent = memo(
click: handleReset,
},
],
+ // Prioritize user's config
+ ...config,
};
- }, [handleReset]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [handleReset, useDeepCompareMemoize(config)]);
useEffect(() => {
// Update layout when figure.layout changes
@@ -158,7 +164,7 @@ export const PlotlyComponent = memo(
setValue((prev) => ({ ...prev, ...obj }));
}
}}
- config={config}
+ config={plotlyConfig}
onSelected={useEvent((evt: Readonly<Plotly.PlotSelectionEvent>) => {
if (!evt) {
return;
diff --git a/marimo/_output/formatters/plotly_formatters.py b/marimo/_output/formatters/plotly_formatters.py
index fe012d58cc..f346b097fb 100644
--- a/marimo/_output/formatters/plotly_formatters.py
+++ b/marimo/_output/formatters/plotly_formatters.py
@@ -17,7 +17,7 @@ class PlotlyFormatter(FormatterFactory):
def register(self) -> None:
import plotly.graph_objects # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
- import plotly.io # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
+ import plotly.io as pio # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
from marimo._output import formatting
@@ -25,15 +25,24 @@ class PlotlyFormatter(FormatterFactory):
def _show_plotly_figure(
fig: plotly.graph_objects.Figure,
) -> tuple[KnownMimeType, str]:
- json_str: str = plotly.io.to_json(fig)
+ json_str: str = pio.to_json(fig)
plugin = PlotlyFormatter.render_plotly_dict(json.loads(json_str))
return ("text/html", plugin.text)
@staticmethod
def render_plotly_dict(json: dict[Any, Any]) -> Html:
+ import plotly.io as pio # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
+
+ resolved_config: dict[str, Any] = {}
+ try:
+ default_renderer: Any = pio.renderers[pio.renderers.default]
+ resolved_config = default_renderer.config or {}
+ except AttributeError:
+ pass
+
return Html(
build_stateless_plugin(
component_name="marimo-plotly",
- args={"figure": json},
+ args={"figure": json, "config": resolved_config},
)
)
diff --git a/marimo/_plugins/ui/_impl/plotly.py b/marimo/_plugins/ui/_impl/plotly.py
index e2c944437a..0ea5eeffed 100644
--- a/marimo/_plugins/ui/_impl/plotly.py
+++ b/marimo/_plugins/ui/_impl/plotly.py
@@ -10,6 +10,7 @@ from typing import (
Final,
List,
Optional,
+ cast,
)
from marimo import _loggers
@@ -64,6 +65,15 @@ class plotly(UIElement[PlotlySelection, List[Dict[str, Any]]]):
mo.hstack([plot, plot.value])
```
+ Or with custom configuration:
+
+ ```python
+ plot = mo.ui.plotly(
+ _plot,
+ config={"staticPlot": True},
+ )
+ ```
+
**Attributes.**
- `value`: a dict of the plot data
@@ -73,6 +83,14 @@ class plotly(UIElement[PlotlySelection, List[Dict[str, Any]]]):
**Initialization Args.**
- `figure`: A `plotly.graph_objects.Figure`
+ - `config`: optional configuration for the plot
+ This is a dictionary that is passed directly to the plotly.
+ See the plotly documentation for more information:
+ https://plotly.com/javascript/configuration-options/
+ This takes precedence over the default configuration of the renderer.
+ - `renderer_name`: optional renderer to use for the plot.
+ If this is not provided, the default renderer (pio.renderers.default)
+ is used.
- `label`: optional text label for the element
- `on_change`: optional callback to run when this element's value changes
"""
@@ -82,6 +100,8 @@ class plotly(UIElement[PlotlySelection, List[Dict[str, Any]]]):
def __init__(
self,
figure: go.Figure,
+ config: Optional[Dict[str, Any]] = None,
+ renderer_name: Optional[str] = None,
*,
label: str = "",
on_change: Optional[Callable[[JSONType], None]] = None,
@@ -92,12 +112,30 @@ class plotly(UIElement[PlotlySelection, List[Dict[str, Any]]]):
json_str = pio.to_json(figure)
+ resolved_config: Dict[str, Any] = {}
+ if config is not None:
+ resolved_config = config
+ else:
+ resolved_name: str = renderer_name or cast(
+ str, pio.renderers.default
+ )
+ default_renderer: Any = pio.renderers[resolved_name]
+ if default_renderer is not None:
+ try:
+ resolved_config = default_renderer.config or {}
+ except AttributeError:
+ LOGGER.warning(
+ "Could not find default renderer configuration. "
+ "Using an empty configuration."
+ )
+
super().__init__(
component_name=plotly.name,
initial_value={},
label=label,
args={
"figure": json.loads(json_str),
+ "config": resolved_config,
},
on_change=on_change,
)
diff --git a/marimo/_smoke_tests/bugs/1161.py b/marimo/_smoke_tests/bugs/1161.py
index 7dfdf271de..7ed96bad53 100644
--- a/marimo/_smoke_tests/bugs/1161.py
+++ b/marimo/_smoke_tests/bugs/1161.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import marimo
__generated_with = "0.4.0"
diff --git a/marimo/_smoke_tests/bugs/1165.py b/marimo/_smoke_tests/bugs/1165.py
index 6e915ec1fd..e4b859fd77 100644
--- a/marimo/_smoke_tests/bugs/1165.py
+++ b/marimo/_smoke_tests/bugs/1165.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import marimo
__generated_with = "0.4.0"
diff --git a/marimo/_smoke_tests/bugs/846.py b/marimo/_smoke_tests/bugs/846.py
new file mode 100644
index 0000000000..843ca34038
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/846.py
@@ -0,0 +1,106 @@
+# Copyright 2024 Marimo. All rights reserved.
+
+import marimo
+
+__generated_with = "0.4.0"
+app = marimo.App()
+
+
+def __(mo):
+ mo.md(
+ "This notebook covers a couple different cases for rendering plotly under different configs and with different renderers."
+ )
+ return
+
+
+def __():
+ import marimo as mo
+ import plotly.io as pio
+
+ pio.renderers.default = "notebook"
+ pio.renderers["notebook"].config["scrollZoom"] = True
+ pio.renderers["notebook"].config["editable"] = True
+ return mo, pio
+
+
+def __(pio):
+ pio.renderers.default
+ return
+
+
+def __(pio):
+ pio.renderers[pio.renderers.default].config
+ return
+
+
+def __(mo, pio):
+ keys = list(pio.renderers.keys())
+
+
+ def get_config(renderer):
+ try:
+ return str(renderer.config)
+ except:
+ return "none"
+
+
+ printed_configs = [
+ {"name": key, "config": get_config(pio.renderers[key])} for key in keys
+ ]
+ mo.ui.table(printed_configs)
+ return get_config, keys, printed_configs
+
+
+def __(pio):
+ pio.renderers
+ return
+
+
+def __():
+ import plotly.express as px
+
+ x_data = [1, 2, 3, 4, 5, 6]
+ y_data = [1, 2, 3, 2, 3, 4]
+ fig = px.scatter(x=x_data, y=y_data)
+ return fig, px, x_data, y_data
+
+
+def __(fig):
+ fig # Uses the default renderer "notebook"
+ return
+
+
+def __(fig, mo):
+ mo.ui.plotly(fig) # Uses the default renderer "notebook"
+ return
+
+
+def __(fig, mo):
+ mo.ui.plotly(fig, config={}) # Uses the empty config
+ return
+
+
+def __(fig, mo):
+ mo.ui.plotly(fig, config={"staticPlot": True}) # Uses the passed config
+ return
+
+
+def __(fig, mo):
+ mo.ui.plotly(fig, renderer_name="browser") # Uses a pre-defined rendererer
+ return
+
+
+if __name__ == "__main__":
+ app.run()