Skip to content

Commit a620a16

Browse files
committed
Fix bug where graph would become unresponsive if an invalid figure was passed
1 parent 35ced08 commit a620a16

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

components/dash-core-components/src/fragments/Graph.react.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ class PlotlyGraph extends Component {
173173
configClone.typesetMath = mathjax;
174174

175175
const figureClone = {
176-
data: figure.data,
177-
layout: this.getLayout(figure.layout, responsive),
178-
frames: figure.frames,
176+
data: figure?.data,
177+
layout: this.getLayout(figure?.layout, responsive),
178+
frames: figure?.frames,
179179
config: configClone,
180180
};
181181

components/dash-core-components/tests/integration/graph/test_graph_basics.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,41 @@ def handleClick(clickData):
370370
data = json.loads(data)
371371
assert "customdata" in data["points"][0], "graph clickData must contain customdata"
372372
assert data["points"][0]["customdata"][0] == expected_value
373+
374+
375+
def test_grbs008_graph_with_empty_figure(dash_dcc):
376+
app = Dash(__name__)
377+
app.layout = html.Div(
378+
[
379+
html.Button("Toggle graph", id="btn"),
380+
dcc.Graph(
381+
id="graph",
382+
figure=None,
383+
)
384+
]
385+
)
386+
387+
@app.callback(Output("graph", "figure"), [Input("btn", "n_clicks")])
388+
def toggle_figure(n_clicks):
389+
if int(n_clicks or 0) % 2 == 0:
390+
# a valid figure
391+
return go.Figure([], layout=go.Layout(title="Valid Figure"))
392+
else:
393+
# an invalid figure
394+
return None
395+
396+
397+
dash_dcc.start_server(app)
398+
399+
# Click the toggle button a couple of times and expect the graph to change between the
400+
# valid and invalid figures, using the "title" as the indicator.
401+
dash_dcc.wait_for_element("#graph")
402+
wait.until(lambda: dash_dcc.find_element(".gtitle").text == "Valid Figure", timeout=2)
403+
404+
dash_dcc.find_element("#btn").click()
405+
wait.until(lambda: len(dash_dcc.find_elements(".gtitle")) == 0, timeout=2)
406+
407+
dash_dcc.find_element("#btn").click()
408+
wait.until(lambda: dash_dcc.find_element(".gtitle").text == "Valid Figure", timeout=2)
409+
410+
assert dash_dcc.get_logs() == []

0 commit comments

Comments
 (0)