Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- [#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

### Removed
Expand Down
38 changes: 37 additions & 1 deletion components/dash-core-components/src/fragments/Graph.react.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import lazyLoadMathJax from '../utils/LazyLoader/third-party/mathjax';
import React, {Component} from 'react';
import {
assocPath,
dissocPath,
equals,
filter,
has,
includes,
isNil,
mergeDeepRight,
omit,
path,
type,
clone,
} from 'ramda';
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() == []