From d47b96603674417efa2f036c587dd4a880badd06 Mon Sep 17 00:00:00 2001 From: Otso Lehdonvirta Date: Mon, 23 Nov 2026 14:34:11 +0200 Subject: [PATCH 1/2] Preserve pending dcc.Graph relayout updates --- CHANGELOG.md | 5 ++ .../src/fragments/Graph.react.js | 35 ++++++-- .../integration/graph/test_relayout_sync.py | 85 +++++++++++++++++++ .../tests/unit/graph/relayout.test.ts | 79 +++++++++++++++++ 4 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 components/dash-core-components/tests/integration/graph/test_relayout_sync.py create mode 100644 components/dash-core-components/tests/unit/graph/relayout.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c33f9cf..918822611e 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` discarding earlier layout changes when multiple `Plotly.relayout` calls run before React delivers the updated figure prop. + ## [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..907615382b 100644 --- a/components/dash-core-components/src/fragments/Graph.react.js +++ b/components/dash-core-components/src/fragments/Graph.react.js @@ -197,6 +197,7 @@ class PlotlyGraph extends Component { this._hasPlotted = false; this._prevGd = null; this._queue = Promise.resolve(); + this._pendingFigures = []; this.parentElement = React.createRef(); @@ -505,7 +506,12 @@ class PlotlyGraph extends Component { // copy and the figure prop (used as the base for Patch // updates) would otherwise go stale. if (eventData && gd.layout) { - const {figure = {}} = this.props; + // React may batch several relayouts before delivering props. + // Build on the latest sync rather than losing its changes. + const figure = + this._pendingFigures[this._pendingFigures.length - 1] ?? + this.props.figure ?? + {}; const updates = {}; for (const eventKey of Object.keys(eventData)) { // 'xaxis.range[0]' -> 'xaxis', 'shapes[1].x0' -> 'shapes' @@ -521,15 +527,15 @@ class PlotlyGraph extends Component { } } if (Object.keys(updates).length) { - setProps({ - figure: { - ...figure, - layout: { - ...figure?.layout, - ...updates, - }, + const syncedFigure = { + ...figure, + layout: { + ...figure?.layout, + ...updates, }, - }); + }; + this._pendingFigures.push(syncedFigure); + setProps({figure: syncedFigure}); } } }); @@ -571,6 +577,7 @@ class PlotlyGraph extends Component { UNSAFE_componentWillReceiveProps(nextProps) { const idChanged = this.props.id !== nextProps.id; if (idChanged) { + this._pendingFigures = []; /* * then the dom needs to get re-rendered with a new ID. * the graph will get updated in componentDidUpdate @@ -578,6 +585,16 @@ class PlotlyGraph extends Component { return; } + if (this.props.figure !== nextProps.figure) { + // An acknowledgement may skip batched figures or arrive before + // a later sync. An external figure supersedes all pending syncs. + const syncedIndex = this._pendingFigures.indexOf(nextProps.figure); + this._pendingFigures = + syncedIndex === -1 + ? [] + : this._pendingFigures.slice(syncedIndex + 1); + } + let p = this._queue; if ( diff --git a/components/dash-core-components/tests/integration/graph/test_relayout_sync.py b/components/dash-core-components/tests/integration/graph/test_relayout_sync.py new file mode 100644 index 0000000000..52d791fc77 --- /dev/null +++ b/components/dash-core-components/tests/integration/graph/test_relayout_sync.py @@ -0,0 +1,85 @@ +import pytest +from dash import Dash, Input, Output, dcc, html + + +@pytest.mark.parametrize("nested", [False, True], ids=["consecutive", "nested"]) +def test_grls001_consecutive_relayouts(dash_duo, nested): + app = Dash(__name__) + app.layout = html.Div( + [ + html.Button("Replace figure", id="replace"), + dcc.Graph( + id="graph", + figure={ + "data": [{"y": [-8, 3, 16, 5]}], + "layout": {"width": 600, "height": 400}, + }, + ), + ] + ) + + @app.callback( + Output("graph", "figure"), + Input("replace", "n_clicks"), + prevent_initial_call=True, + ) + def replace(_): + return { + "data": [{"y": [10, 11, 12, 13]}], + "layout": { + "width": 600, + "height": 400, + "title": {"text": "Replacement"}, + "yaxis": {"range": [9, 14]}, + }, + } + + dash_duo.start_server(app) + dash_duo.wait_for_element("#graph .main-svg") + dash_duo.driver.execute_async_script( + """ + const nested = arguments[0]; + const done = arguments[arguments.length - 1]; + const gd = document.querySelector('#graph .js-plotly-plot'); + if (nested) { + gd.once('plotly_relayout', () => { + Plotly.relayout(gd, {'xaxis.range': [0, 1]}); + }); + } + Plotly.relayout(gd, {'yaxis.range': [0, 5]}); + if (!nested) { + Plotly.relayout(gd, {'xaxis.range': [0, 1]}); + } + // Wait for the sync-triggered Plotly.react and subsequent frames. + gd.once('plotly_afterplot', () => { + requestAnimationFrame(() => requestAnimationFrame(done)); + }); + """, + nested, + ) + ranges = dash_duo.driver.execute_script(""" + const gd = document.querySelector('#graph .js-plotly-plot'); + const figure = window.dash_component_api.getLayout('graph').props.figure; + return [gd._fullLayout.yaxis.range, gd._fullLayout.xaxis.range, + figure.layout.yaxis?.range, figure.layout.xaxis?.range]; + """) + assert ranges == [[0, 5], [0, 1], [0, 5], [0, 1]] + + dash_duo.find_element("#replace").click() + dash_duo.wait_for_text_to_equal("#graph .gtitle", "Replacement") + dash_duo.driver.execute_async_script(""" + const done = arguments[arguments.length - 1]; + const gd = document.querySelector('#graph .js-plotly-plot'); + Plotly.relayout(gd, {'xaxis.range': [1, 2]}); + gd.once('plotly_afterplot', () => { + requestAnimationFrame(() => requestAnimationFrame(done)); + }); + """) + figure = dash_duo.driver.execute_script( + "return window.dash_component_api.getLayout('graph').props.figure;" + ) + assert figure["data"][0]["y"] == [10, 11, 12, 13] + assert figure["layout"]["title"]["text"] == "Replacement" + assert figure["layout"]["yaxis"]["range"] == [9, 14] + assert figure["layout"]["xaxis"]["range"] == [1, 2] + assert dash_duo.get_logs() == [] diff --git a/components/dash-core-components/tests/unit/graph/relayout.test.ts b/components/dash-core-components/tests/unit/graph/relayout.test.ts new file mode 100644 index 0000000000..377f5bea82 --- /dev/null +++ b/components/dash-core-components/tests/unit/graph/relayout.test.ts @@ -0,0 +1,79 @@ +import PlotlyGraph from '../../../src/fragments/Graph.react'; + +function setup() { + const figure = { + data: [{y: [1, 2, 3]}], + layout: {title: {text: 'original'}}, + }; + const props = {id: 'graph', figure, setProps: jest.fn()}; + const graph = new PlotlyGraph(props); + const events: Record void> = {}; + const gd = { + layout: {...figure.layout} as Record, + on: (name: string, handler: (data: object) => void) => { + events[name] = handler; + }, + }; + Object.assign(graph.gd, {current: gd}); + graph.plot = jest.fn(() => Promise.resolve()); + graph.amendTraces = jest.fn(promise => promise); + graph.bindEvents(); + const relayout = (key: string, value: object) => { + gd.layout[key] = value; + events.plotly_relayout({[key]: value}); + return props.setProps.mock.calls + .filter(([update]) => update.figure) + .pop()[0].figure; + }; + const receive = (figure = graph.props.figure) => { + const nextProps = {...graph.props, figure}; + // eslint-disable-next-line new-cap + graph.UNSAFE_componentWillReceiveProps(nextProps); + Object.assign(graph, {props: nextProps}); + }; + return {figure, graph, props, relayout, receive}; +} + +describe('relayout figure synchronization', () => { + it('keeps changes from consecutive events before props arrive', () => { + const {figure, relayout} = setup(); + relayout('yaxis', {range: [0, 5]}); + const synced = relayout('xaxis', {range: [0, 1]}); + expect(synced.layout.yaxis.range).toEqual([0, 5]); + expect(synced.layout.xaxis.range).toEqual([0, 1]); + expect(synced.data).toBe(figure.data); + expect(figure.layout).toEqual({title: {text: 'original'}}); + }); + + it('retains pending changes through unrelated prop updates', () => { + const {relayout, receive} = setup(); + relayout('yaxis', {range: [0, 5]}); + receive(); + const synced = relayout('xaxis', {range: [0, 1]}); + expect(synced.layout.yaxis.range).toEqual([0, 5]); + }); + + it('retains later changes when an earlier sync is acknowledged', () => { + const {relayout, receive} = setup(); + const first = relayout('yaxis', {range: [0, 5]}); + relayout('xaxis', {range: [0, 1]}); + receive(first); + const synced = relayout('title', {text: 'updated'}); + expect(synced.layout.yaxis.range).toEqual([0, 5]); + expect(synced.layout.xaxis.range).toEqual([0, 1]); + }); + + it('uses an external figure as the base for subsequent events', () => { + const {relayout, receive} = setup(); + relayout('yaxis', {range: [0, 5]}); + const external = { + data: [{y: [5, 4, 3]}], + layout: {title: {text: 'external'}}, + }; + receive(external); + const synced = relayout('xaxis', {range: [0, 1]}); + expect(synced.data).toBe(external.data); + expect(synced.layout.title.text).toBe('external'); + expect(synced.layout.yaxis).toBeUndefined(); + }); +}); From 7f9121f420aa320c3667e2899fc9ecd11b7b2ec0 Mon Sep 17 00:00:00 2001 From: Otso Lehdonvirta Date: Mon, 23 Nov 2026 14:36:33 +0200 Subject: [PATCH 2/2] Link relayout fix changelog to its pull request --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 918822611e..6a05f1bff7 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` discarding earlier layout changes when multiple `Plotly.relayout` calls run before React delivers the updated figure prop. +- [#4174](https://github.com/plotly/dash/pull/4174) Fix `dcc.Graph` discarding earlier layout changes when multiple `Plotly.relayout` calls run before React delivers the updated figure prop. Fixes [#4172](https://github.com/plotly/dash/issues/4172). ## [4.5.1] - 2026-11-19