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: allow resetting plotly zoom/pan externally (#1277)

Myles Scolnick2e5d24983847
diff --git a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
index 34089fa83d..0130fbfca9 100644
--- a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
+++ b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
@@ -12,8 +12,9 @@ import { lazy, memo, useEffect, useMemo, useState } from "react";
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 { isEqual, set } from "lodash-es";
import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
+import { usePrevious } from "@uidotdev/usehooks";
interface Data {
figure: Figure;
@@ -126,17 +127,29 @@ export const PlotlyComponent = memo(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [handleReset, useDeepCompareMemoize(config)]);
+ const prevFigure = usePrevious(figure) ?? figure;
+
useEffect(() => {
- // Update layout when figure.layout changes
- // Omit keys that we don't want to override
- const layout = Objects.omit(figure.layout, [
+ const omitKeys = new Set<keyof Plotly.Layout>([
"autosize",
"dragmode",
"xaxis",
"yaxis",
]);
+
+ // If the key was updated externally (e.g. can be specifically passed in the config)
+ // then we need to update the layout
+ for (const key of omitKeys) {
+ if (!isEqual(figure.layout[key], prevFigure.layout[key])) {
+ omitKeys.delete(key);
+ }
+ }
+
+ // Update layout when figure.layout changes
+ // Omit keys that we don't want to override
+ const layout = Objects.omit(figure.layout, omitKeys);
setLayout((prev) => ({ ...prev, ...layout }));
- }, [figure.layout]);
+ }, [figure.layout, prevFigure.layout]);
return (
<LazyPlot
@@ -173,7 +186,6 @@ export const PlotlyComponent = memo(
? figure.layout.selections
: [];
if (selections.length === 0) {
- console.log("Clearing selections");
setValue((prev) => ({
...prev,
selections: selections,
diff --git a/frontend/src/utils/objects.ts b/frontend/src/utils/objects.ts
index 1d95a4a81c..dae9cba177 100644
--- a/frontend/src/utils/objects.ts
+++ b/frontend/src/utils/objects.ts
@@ -85,7 +85,10 @@ export const Objects = {
return result;
},
- omit<V extends object, K extends keyof V>(obj: V, keys: K[]): Partial<V> {
+ omit<V extends object, K extends keyof V>(
+ obj: V,
+ keys: K[] | Set<K>,
+ ): Partial<V> {
const set = new Set<K>(keys);
return Objects.filter(obj, (_, key) => !set.has(key));
},
diff --git a/marimo/_smoke_tests/bugs/1273.py b/marimo/_smoke_tests/bugs/1273.py
new file mode 100644
index 0000000000..22946df4ba
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1273.py
@@ -0,0 +1,41 @@
+import marimo
+
+__generated_with = "0.4.7"
+app = marimo.App(width="full")
+
+
+def __():
+ import marimo as mo
+ import plotly.express as px
+ return mo, px
+
+
+def __(mo):
+ s = mo.ui.range_slider(start=-5, stop=5, show_value=True, label='x range')
+ x, y = list(range(10)), [i * i for i in range(-5, 5)]
+ return s, x, y
+
+
+def __(mo, px, s, x, y):
+ # takes affect when using the slider
+ mo.vstack([
+ s,
+ px.scatter(x=x, y=y, range_x=s.value, title=f'range_x: {s.value}')
+ ])
+ return
+
+
+def __(mo, px, s, x, y):
+ # takes affect when using the slider
+ # also is zoom/range is persisted across app view, but reset when the slider changes the range
+ plot = mo.ui.plotly(px.scatter(x=x, y=y, range_x=s.value, title=f'range_x: {s.value}'))
+ plot
+ return plot,
+
+
+if __name__ == "__main__":
+ app.run()