From eff593e68c9e573d23763254ab4cff1aad60fcae Mon Sep 17 00:00:00 2001 From: Nicolas Paris Date: Mon, 23 Nov 2026 22:41:03 +0100 Subject: [PATCH 1/2] Keep dcc.Graph user zoom across new figures with the same uirevision Since #3882, plotly_relayout syncs the user edit (e.g. a zoomed axis range) into the figure prop, which replots it. plotly.js then sees the edited value as an app-provided one: it no longer matches the pre-edit value stored in _preGUI, so the edit loses its uirevision protection and the next figure from a callback resets the zoom. Before Plotly.react, put back the _preGUI value for every key whose incoming value is the current user value, while uirevision is unchanged. plotly.js keeps the user edit, and Patch updates built on the synced figure (#3810) still keep it too. Fixes #4093 --- CHANGELOG.md | 5 ++ .../src/fragments/Graph.react.js | 38 +++++++++++++- .../integration/graph/test_graph_basics.py | 50 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c33f9cf..a144478e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `dash` will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/). +## [UNRELEASED] + +### Fixed +- Fix `dcc.Graph` resetting a user zoom or pan when a callback returns a new figure with an unchanged `uirevision` (regression introduced in 4.4.1 by [#3882](https://github.com/plotly/dash/pull/3882)). Fixes [#4093](https://github.com/plotly/dash/issues/4093). + ## [4.5.1] - 2026-11-19 ### Removed diff --git a/components/dash-core-components/src/fragments/Graph.react.js b/components/dash-core-components/src/fragments/Graph.react.js index 7eeda2b456..2dc47d0615 100644 --- a/components/dash-core-components/src/fragments/Graph.react.js +++ b/components/dash-core-components/src/fragments/Graph.react.js @@ -1,6 +1,8 @@ import lazyLoadMathJax from '../utils/LazyLoader/third-party/mathjax'; import React, {Component} from 'react'; import { + assocPath, + dissocPath, equals, filter, has, @@ -8,6 +10,7 @@ import { isNil, mergeDeepRight, omit, + path, type, clone, } from 'ramda'; @@ -65,6 +68,36 @@ const toPlainArray = value => { return value; }; +// 'xaxis.range[0]' -> ['xaxis', 'range', 0], the key format of plotly.js `_preGUI` +const guiEditPath = key => + key + .split(/\.|\[(\d+)\]/) + .filter(Boolean) + .map(part => (/^\d+$/.test(part) ? Number(part) : part)); + +// plotly.js keeps a user edit only while the incoming value equals its `_preGUI` +// one; the relayout sync puts the edit in the figure, so restore the pre-edit value. +const restoreGuiEdits = (gd, layout) => { + const preGUI = gd?._fullLayout?._preGUI; + if (!layout || !preGUI || layout.uirevision !== gd.layout?.uirevision) { + return layout; + } + let restored = layout; + for (const key of Object.keys(preGUI)) { + const keyPath = guiEditPath(key); + const incoming = path(keyPath, restored); + if ( + incoming !== undefined && + equals(incoming, path(keyPath, gd.layout)) + ) { + restored = isNil(preGUI[key]) + ? dissocPath(keyPath, restored) + : assocPath(keyPath, preGUI[key], restored); + } + } + return restored; +}; + const filterEventData = (gd, eventData, event) => { let filteredEventData; if (includes(event, ['click', 'hover', 'selected'])) { @@ -226,7 +259,10 @@ class PlotlyGraph extends Component { const figureClone = { data: figure?.data, - layout: this.getLayout(figure?.layout, responsive), + layout: restoreGuiEdits( + gd, + this.getLayout(figure?.layout, responsive) + ), frames: figure?.frames, config: configClone, }; diff --git a/components/dash-core-components/tests/integration/graph/test_graph_basics.py b/components/dash-core-components/tests/integration/graph/test_graph_basics.py index 04e7167ae9..bde8b9934c 100644 --- a/components/dash-core-components/tests/integration/graph/test_graph_basics.py +++ b/components/dash-core-components/tests/integration/graph/test_graph_basics.py @@ -897,3 +897,53 @@ def show_hover(data): assert {key: point[key] for key in expected} == expected assert not {"data", "fullData", "xaxis", "yaxis"}.intersection(point) assert dash_dcc.get_logs() == [] + + +def test_grbs015_graph_new_figure_keeps_user_zoom(dash_dcc): + """Regression test for https://github.com/plotly/dash/issues/4093. + + A mouse drag, not Plotly.relayout: only a GUI edit is kept by uirevision.""" + app = Dash(__name__) + + def figure(showlegend): + return { + "data": [{"x": list(range(10)), "y": list(range(10))}], + "layout": {"uirevision": True, "showlegend": showlegend}, + } + + app.layout = html.Div( + [ + html.Button("Toggle legend", id="toggle"), + dcc.Graph(id="graph", figure=figure(True), style={"width": 600}), + ] + ) + + @app.callback( + Output("graph", "figure"), + Input("toggle", "n_clicks"), + prevent_initial_call=True, + ) + def toggle(n_clicks): + return figure(n_clicks % 2 == 0) + + dash_dcc.start_server(app) + dash_dcc.wait_for_element("#graph .main-svg") + + def rendered(expression): + return dash_dcc.driver.execute_script( + f"return document.querySelector('#graph .js-plotly-plot').{expression}" + ) + + full_range = rendered("_fullLayout.xaxis.range") + drag = dash_dcc.find_element("#graph .nsewdrag") + ActionChains(dash_dcc.driver).move_to_element_with_offset( + drag, -100, 0 + ).click_and_hold().move_by_offset(150, 0).release().perform() + wait.until(lambda: rendered("_fullLayout.xaxis.range") != full_range, 3) + zoomed = rendered("_fullLayout.xaxis.range") + + dash_dcc.find_element("#toggle").click() + wait.until(lambda: rendered("_fullLayout.showlegend") is False, 3) + assert rendered("_fullLayout.xaxis.range") == zoomed + + assert dash_dcc.get_logs() == [] From bb3a4284dd13156ee6dd041c59447dda43b19bb0 Mon Sep 17 00:00:00 2001 From: Nicolas Paris Date: Mon, 23 Nov 2026 22:41:27 +0100 Subject: [PATCH 2/2] Link the changelog entry to its PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a144478e9c..26f3317a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [UNRELEASED] ### Fixed -- Fix `dcc.Graph` resetting a user zoom or pan when a callback returns a new figure with an unchanged `uirevision` (regression introduced in 4.4.1 by [#3882](https://github.com/plotly/dash/pull/3882)). Fixes [#4093](https://github.com/plotly/dash/issues/4093). +- [#4183](https://github.com/plotly/dash/pull/4183) Fix `dcc.Graph` resetting a user zoom or pan when a callback returns a new figure with an unchanged `uirevision` (regression introduced in 4.4.1 by [#3882](https://github.com/plotly/dash/pull/3882)). Fixes [#4093](https://github.com/plotly/dash/issues/4093). ## [4.5.1] - 2026-11-19