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.

Replace react-plotly.js with vendored Plot component (#8238)

Florian Verdonck19ace695a048
diff --git a/frontend/package.json b/frontend/package.json
index 61a7341128..37bdea4e18 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -157,7 +157,6 @@
"react-grid-layout": "^1.5.3",
"react-hook-form": "7.54.2",
"react-markdown": "^9.1.0",
- "react-plotly.js": "^2.6.0",
"react-resizable-panels": "2.1.9",
"react-use-event-hook": "^0.9.6",
"react-vega": "^8.0.0",
@@ -227,9 +226,9 @@
"@types/katex": "^0.16.8",
"@types/lodash-es": "^4.17.12",
"@types/node": "^24.10.9",
+ "@types/plotly.js": "^2.33.4",
"@types/react": "^19.2.10",
"@types/react-dom": "^19.2.3",
- "@types/react-plotly.js": "^2.6.4",
"@types/timestring": "^6.0.5",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
diff --git a/frontend/src/plugins/impl/plotly/Plot.tsx b/frontend/src/plugins/impl/plotly/Plot.tsx
new file mode 100644
index 0000000000..a65e534828
--- /dev/null
+++ b/frontend/src/plugins/impl/plotly/Plot.tsx
@@ -0,0 +1,171 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type * as PlotlyTypes from "plotly.js";
+// Import the pre-built dist bundle, not the source entry point.
+// The source entry point requires Node.js polyfills (e.g. `buffer/`)
+// that are unavailable in the browser/bundler environment.
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-expect-error — no type declarations for dist path, we use PlotlyTypes above
+import Plotly from "plotly.js/dist/plotly";
+import { useEffect, useRef } from "react";
+
+// Plotly attaches `on` and `removeListener` to the DOM element at runtime.
+// The @types/plotly.js PlotlyHTMLElement type includes `on` and `removeAllListeners`
+// but not the per-handler `removeListener`. We extend the type to include it.
+interface PlotlyElement extends PlotlyTypes.PlotlyHTMLElement {
+ removeListener(event: string, handler: (...args: never[]) => void): void;
+}
+
+export interface Figure {
+ data: PlotlyTypes.Data[];
+ layout: Partial<PlotlyTypes.Layout>;
+ frames: PlotlyTypes.Frame[] | null;
+}
+
+export interface PlotProps {
+ data: PlotlyTypes.Data[];
+ layout: Partial<PlotlyTypes.Layout>;
+ frames?: PlotlyTypes.Frame[];
+ config?: Partial<PlotlyTypes.Config>;
+ className?: string;
+ style?: React.CSSProperties;
+ useResizeHandler?: boolean;
+ divId?: string;
+ onRelayout?: (event: PlotlyTypes.PlotRelayoutEvent) => void;
+ onRelayouting?: (event: PlotlyTypes.PlotRelayoutEvent) => void;
+ onSelected?: (event: PlotlyTypes.PlotSelectionEvent) => void;
+ onDeselect?: () => void;
+ onSunburstClick?: (event: PlotlyTypes.PlotMouseEvent) => void;
+ onTreemapClick?: (event: PlotlyTypes.PlotMouseEvent) => void;
+ onError?: (err: Error) => void;
+}
+
+// Plotly event name convention:
+// - events are attached as `'plotly_' + name.toLowerCase()`
+// - react props are `'on' + name`
+const EVENT_NAMES = [
+ "Relayout",
+ "Relayouting",
+ "Selected",
+ "Deselect",
+ "SunburstClick",
+ "TreemapClick",
+] as const;
+
+type EventName = (typeof EVENT_NAMES)[number];
+
+function propName(event: EventName): keyof PlotProps {
+ return `on${event}` as keyof PlotProps;
+}
+
+function plotlyEventName(event: EventName): string {
+ return `plotly_${event.toLowerCase()}`;
+}
+
+export const Plot = (props: PlotProps) => {
+ const containerRef = useRef<HTMLDivElement>(null);
+
+ const {
+ data,
+ layout,
+ config,
+ frames,
+ className,
+ style,
+ useResizeHandler,
+ divId,
+ onError,
+ } = props;
+
+ // Render / update the plot
+ useEffect(() => {
+ const el = containerRef.current;
+ if (!el) {
+ return;
+ }
+
+ Plotly.react(el, data, layout, config)
+ .then(() => {
+ if (frames && frames.length > 0) {
+ return Plotly.addFrames(el as unknown as PlotlyTypes.Root, frames);
+ }
+ })
+ .catch((error: Error) => {
+ onError?.(error);
+ });
+ }, [data, layout, config, frames, onError]);
+
+ // Sync event handlers
+ useEffect(
+ () => {
+ const el = containerRef.current;
+ if (!el) {
+ return;
+ }
+
+ const plotlyEl = el as unknown as PlotlyElement;
+
+ // eslint-disable-next-line @typescript-eslint/ban-types -- Plotly's event API uses generic function references
+ const attached: {
+ plotlyName: string;
+ handler: (...args: never[]) => void;
+ }[] = [];
+
+ for (const name of EVENT_NAMES) {
+ const handler = props[propName(name)];
+ if (typeof handler === "function") {
+ const plotlyName = plotlyEventName(name);
+ plotlyEl.on(plotlyName as never, handler as never);
+ attached.push({
+ plotlyName,
+ handler: handler as (...args: never[]) => void,
+ });
+ }
+ }
+
+ return () => {
+ for (const { plotlyName, handler } of attached) {
+ plotlyEl.removeListener(plotlyName, handler as never);
+ }
+ };
+ // Re-sync whenever any event handler prop changes
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ },
+ EVENT_NAMES.map((name) => props[propName(name)]),
+ );
+
+ // Window resize handler
+ useEffect(() => {
+ if (!useResizeHandler) {
+ return;
+ }
+
+ const el = containerRef.current;
+ if (!el) {
+ return;
+ }
+
+ const handler = () => {
+ Plotly.Plots.resize(el as unknown as PlotlyTypes.Root);
+ };
+
+ window.addEventListener("resize", handler);
+ return () => {
+ window.removeEventListener("resize", handler);
+ };
+ }, [useResizeHandler]);
+
+ // Cleanup on unmount
+ useEffect(() => {
+ const el = containerRef.current;
+ return () => {
+ if (el) {
+ Plotly.purge(el as unknown as PlotlyTypes.Root);
+ }
+ };
+ }, []);
+
+ return (
+ <div id={divId} className={className} style={style} ref={containerRef} />
+ );
+};
diff --git a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
index 2efe311e65..d275e79012 100644
--- a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
+++ b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
@@ -1,9 +1,10 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import type { Figure, PlotParams } from "react-plotly.js";
+import type * as Plotly from "plotly.js";
import { z } from "zod";
import type { IPlugin, IPluginProps, Setter } from "@/plugins/types";
import { Logger } from "@/utils/Logger";
+import type { Figure } from "./Plot";
import "./plotly.css";
import "./mapbox.css";
@@ -70,13 +71,8 @@ interface PlotlyPluginProps extends Data {
host: HTMLElement;
}
-// For whatever reason, the version of vite-rolldown that we are one is not exporting this default export correctly.
-export const LazyPlot = lazy(() =>
- import("react-plotly.js").then((module) => {
- return module.default as unknown as {
- default: React.ComponentType<PlotParams>;
- };
- }),
+const LazyPlot = lazy(() =>
+ import("./Plot").then((mod) => ({ default: mod.Plot })),
);
const SUNBURST_DATA_KEYS: (keyof Plotly.SunburstPlotDatum)[] = [
@@ -176,7 +172,6 @@ export const PlotlyComponent = memo(
};
});
})}
- // @ts-expect-error We patched this prop here so it doesn't exist in the types
onTreemapClick={useEvent((evt: Readonly<Plotly.PlotMouseEvent>) => {
if (!evt) {
return;
@@ -215,7 +210,7 @@ export const PlotlyComponent = memo(
className="w-full"
useResizeHandler={true}
frames={figure.frames ?? undefined}
- onError={useEvent((err) => {
+ onError={useEvent((err: Error) => {
Logger.error("PlotlyPlugin: ", err);
})}
/>
diff --git a/frontend/src/plugins/impl/plotly/__tests__/parse-from-template.test.ts b/frontend/src/plugins/impl/plotly/__tests__/parse-from-template.test.ts
index cea9bb7ea1..f2537e9e41 100644
--- a/frontend/src/plugins/impl/plotly/__tests__/parse-from-template.test.ts
+++ b/frontend/src/plugins/impl/plotly/__tests__/parse-from-template.test.ts
@@ -1,4 +1,5 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import type * as Plotly from "plotly.js";
import { describe, expect, it } from "vitest";
import { createParser } from "../parse-from-template";
diff --git a/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts b/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
index cdd143979f..135e3d37cf 100644
--- a/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
+++ b/frontend/src/plugins/impl/plotly/__tests__/usePlotlyLayout.test.ts
@@ -1,7 +1,8 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import type { Figure } from "react-plotly.js";
+import type * as Plotly from "plotly.js";
import { describe, expect, it } from "vitest";
+import type { Figure } from "../Plot";
import {
computeLayoutOnFigureChange,
computeLayoutUpdate,
diff --git a/frontend/src/plugins/impl/plotly/parse-from-template.ts b/frontend/src/plugins/impl/plotly/parse-from-template.ts
index 723991c208..9f434ab2bd 100644
--- a/frontend/src/plugins/impl/plotly/parse-from-template.ts
+++ b/frontend/src/plugins/impl/plotly/parse-from-template.ts
@@ -1,6 +1,7 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { get } from "lodash-es";
+import type * as Plotly from "plotly.js";
export interface PlotlyTemplateParser {
/**
diff --git a/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts b/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
index c7a9e32a69..d151bb6a44 100644
--- a/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
+++ b/frontend/src/plugins/impl/plotly/usePlotlyLayout.ts
@@ -2,9 +2,10 @@
import { usePrevious } from "@uidotdev/usehooks";
import { isEqual, pick } from "lodash-es";
+import type * as Plotly from "plotly.js";
import { useEffect, useState } from "react";
-import type { Figure } from "react-plotly.js";
import { Objects } from "@/utils/objects";
+import type { Figure } from "./Plot";
/**
* Keys that are preserved across figure updates when set by user interaction.
diff --git a/package.json b/package.json
index bc402081f1..27225262ac 100644
--- a/package.json
+++ b/package.json
@@ -22,7 +22,6 @@
"packageManager": "[email protected]",
"pnpm": {
"patchedDependencies": {
- "react-plotly.js": "patches/react-plotly.js.patch",
},
"overrides": {
diff --git a/patches/react-plotly.js.patch b/patches/react-plotly.js.patch
deleted file mode 100644
index 96b632c4c8..0000000000
--- a/patches/react-plotly.js.patch
+++ /dev/null
@@ -1,15 +0,0 @@
-diff --git a/factory.js b/factory.js
-index 40f3edccbebc45b491dd3488ed619ec9208be8d9..f88f451588d656805d1ae76ce4184a486a179eb1 100644
---- a/factory.js
-+++ b/factory.js
-@@ -40,8 +40,8 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
- // The naming convention is:
- // - events are attached as `'plotly_' + eventName.toLowerCase()`
- // - react props are `'on' + eventName`
--var eventNames = ['AfterExport', 'AfterPlot', 'Animated', 'AnimatingFrame', 'AnimationInterrupted', 'AutoSize', 'BeforeExport', 'BeforeHover', 'ButtonClicked', 'Click', 'ClickAnnotation', 'Deselect', 'DoubleClick', 'Framework', 'Hover', 'LegendClick', 'LegendDoubleClick', 'Relayout', 'Relayouting', 'Restyle', 'Redraw', 'Selected', 'Selecting', 'SliderChange', 'SliderEnd', 'SliderStart', 'SunburstClick', 'Transitioning', 'TransitionInterrupted', 'Unhover', 'WebGlContextLost'];
--var updateEvents = ['plotly_restyle', 'plotly_redraw', 'plotly_relayout', 'plotly_relayouting', 'plotly_doubleclick', 'plotly_animated', 'plotly_sunburstclick']; // Check if a window is available since SSR (server-side rendering)
-+var eventNames = ['AfterExport', 'AfterPlot', 'Animated', 'AnimatingFrame', 'AnimationInterrupted', 'AutoSize', 'BeforeExport', 'BeforeHover', 'ButtonClicked', 'Click', 'ClickAnnotation', 'Deselect', 'DoubleClick', 'Framework', 'Hover', 'LegendClick', 'LegendDoubleClick', 'Relayout', 'Relayouting', 'Restyle', 'Redraw', 'Selected', 'Selecting', 'SliderChange', 'SliderEnd', 'SliderStart', 'SunburstClick', 'TreemapClick', 'Transitioning', 'TransitionInterrupted', 'Unhover', 'WebGlContextLost'];
-+var updateEvents = ['plotly_restyle', 'plotly_redraw', 'plotly_relayout', 'plotly_relayouting', 'plotly_doubleclick', 'plotly_animated', 'plotly_sunburstclick', 'plotly_treemapclick']; // Check if a window is available since SSR (server-side rendering)
- // breaks unnecessarily if you try to use it server-side.
-
- var isBrowser = typeof window !== 'undefined';
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bc05b06965..950fb5db44 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -14,9 +14,6 @@ patchedDependencies:
hash: 5fdb1de4a857e2fda0956b7e05a1b32817de8e7008009e71c0120ef02b886219
path: patches/[email protected]
- react-plotly.js:
- hash: e65888c226f3ef8f42923cbb3b026d2f4811da3e96fa0d7dee3cb62df02c6189
- path: patches/react-plotly.js.patch
importers:
@@ -448,9 +445,6 @@ importers:
react-markdown:
specifier: ^9.1.0
version: 9.1.0(@types/[email protected])([email protected])
- react-plotly.js:
- specifier: ^2.6.0
- version: 2.6.0(patch_hash=e65888c226f3ef8f42923cbb3b026d2f4811da3e96fa0d7dee3cb62df02c6189)([email protected]([email protected])([email protected]([email protected])))([email protected])
react-resizable-panels:
specifier: 2.1.9
@@ -578,15 +572,15 @@ importers:
'@types/node':
specifier: ^24.10.9
version: 24.10.9
+ '@types/plotly.js':
+ specifier: ^2.33.4
+ version: 2.35.13
'@types/react':
specifier: ^19.2.10
version: 19.2.10
'@types/react-dom':
specifier: ^19.2.3
version: 19.2.3(@types/[email protected])
- '@types/react-plotly.js':
- specifier: ^2.6.4
- version: 2.6.4
'@types/timestring':
specifier: ^6.0.5
version: 6.0.5
@@ -4195,8 +4189,8 @@ packages:
resolution: {integrity: sha512-eLYXDbZWXh2uxf+w8sXS8d6KSoXTswfps6fvCUuVAGN8eRpfe7h9eSRydxiSJvo9Bf+GzifsDOr9TMQlmJdmkw==}
- '@types/[email protected]':
- resolution: {integrity: sha512-84eSnXD5gTM36iaOv3/4cduF2X0eptMnK9CcghH56A4uhSiFjAIgqhFGmdq+0yDh2jLQzL1s34CsBU5KD3MvPA==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-j5G1TJQKOOsEqvypTtWwU1WMcWUmYfgGpz4WVDVJ4/j5giIEgj4uLeG85SFzhlaO2xxtX07Cx/ZpB0LPNqYx7Q==}
resolution: {integrity: sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==}
@@ -4209,9 +4203,6 @@ packages:
resolution: {integrity: sha512-Cw7+sb3yyjtmxwwJiXtEXcu5h4cgs+sCGkHwHXsFmPyV30bf14LeD/fa2LwQovuD2HWxCcjIdNhDlcYGj95qGA==}
- '@types/[email protected]':
- resolution: {integrity: sha512-AU6w1u3qEGM0NmBA69PaOgNc0KPFA/+qkH6Uu9EBTJ45/WYOUoXi9AF5O15PRM2klpHSiHAAs4WnlI+OZAFmUA==}
-
resolution: {integrity: sha512-8TV6R3h2j7a91c+1DXdJi3Syo69zzIZbz7Lg5tORM5LEJG7X/E6a1V3drRyBRZq7/utz7A+c4OgYLiLcYGHG6w==}
peerDependencies:
@@ -8524,12 +8515,6 @@ packages:
react: ^19.2.4
react-dom: ^19.2.4
- resolution: {integrity: sha512-g93xcyhAVCSt9kV1svqG1clAEdL6k3U+jjuSzfTV7owaSU9Go6Ph8bl25J+jKfKvIGAEYpe4qj++WHJuc9IaeA==}
- peerDependencies:
- plotly.js: '>1.34.0'
- react: ^19.2.4
-
resolution: {integrity: sha512-+PbtI3VuDV0l6CleQMsx2gtK0JZbZKbpdu5ynr+lbsuvtmgbNcS3VM0tuY2QjFNOcWxvXeHjDpy42RO+4U2rug==}
@@ -14642,7 +14627,7 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]': {}
+ '@types/[email protected]': {}
'@types/[email protected]': {}
@@ -14654,11 +14639,6 @@ snapshots:
dependencies:
'@types/react': 19.2.10
- '@types/[email protected]':
- dependencies:
- '@types/plotly.js': 3.0.2
- '@types/react': 19.2.10
-
dependencies:
'@types/react': 19.2.10
@@ -19798,12 +19778,6 @@ snapshots:
react: 19.2.4
react-dom: 19.2.4([email protected])
- [email protected](patch_hash=e65888c226f3ef8f42923cbb3b026d2f4811da3e96fa0d7dee3cb62df02c6189)([email protected]([email protected])([email protected]([email protected])))([email protected]):
- dependencies:
- prop-types: 15.8.1
- react: 19.2.4
-