Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graph animate frames #1915

Merged
merged 8 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
15 changes: 13 additions & 2 deletions components/dash-core-components/src/fragments/Graph.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class PlotlyGraph extends Component {
const {animate, animation_options, responsive} = props;

const gd = this.gd.current;

figure = props._dashprivate_transformFigure(figure, gd);
config = props._dashprivate_transformConfig(config, gd);

Expand All @@ -155,7 +154,19 @@ class PlotlyGraph extends Component {
this._hasPlotted &&
figure.data.length === gd.data.length
) {
return Plotly.animate(gd, figure, animation_options);
// in case we've have figure frames,
// we need to recreate frames before animation
let result;
if (figure.frames) {
result = Plotly.deleteFrames(gd).then(() => {
return Plotly.addFrames(gd, figure.frames).then(() => {
return Plotly.animate(gd, figure, animation_options);
});
});
} else {
result = Plotly.animate(gd, figure, animation_options);
}
return result;
nickmelnikov82 marked this conversation as resolved.
Show resolved Hide resolved
}

const configClone = this.getConfig(config, responsive);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import pytest
import pandas as pd
from multiprocessing import Value, Lock
import numpy as np
from time import sleep

import plotly.graph_objects as go
from dash import Dash, Input, Output, dcc, html

import dash.testing.wait as wait
Expand Down Expand Up @@ -162,3 +163,121 @@ def update_graph(n_clicks):
dash_dcc.wait_for_element("#my-graph:not([data-dash-is-loading])")

assert dash_dcc.get_logs() == []


def test_grbs005_graph_update_frames(dash_dcc):
app = Dash(__name__)

def get_scatter(multiplier, offset):
return go.Scatter(
x=list(map(lambda n: n * multiplier, [0, 1, 2])),
y=list(map(lambda n: n + offset, [0, 1, 2])),
mode="markers",
)

def get_figure(data, frames, title):
return go.Figure(
data=data,
layout=go.Layout(
title=title,
yaxis=dict(range=[-1, 5]),
xaxis=dict(range=[-3, 3]),
updatemenus=[
dict(
type="buttons",
buttons=[
dict(
label="Play",
method="animate",
args=[
None,
{
"frame": {"duration": 100, "redraw": True},
"fromcurrent": False,
"transition": {
"duration": 500,
"easing": "quadratic-in-out",
},
},
],
)
],
)
],
),
frames=frames,
)

app.layout = html.Div(
[
html.Label("Choose dataset"),
dcc.RadioItems(
id="change-data",
options=[
{"label": "No data", "value": 0},
{"label": "Data A", "value": 1},
{"label": "Data B", "value": 2},
],
value=0,
),
dcc.Graph(
id="test-change",
animate=True,
animation_options={"frame": {"redraw": True}},
),
html.Div(id="relayout-data"),
]
)

@app.callback(
Output("relayout-data", "children"),
[Input("test-change", "figure")],
)
def show_relayout_data(data):
frames = data.get("frames", [])
if frames:
return json.dumps(frames[0]["data"][0]["x"])
return ""

@app.callback(
Output("test-change", "figure"),
Input("change-data", "value"),
)
def set_data(dataset):
if dataset == 1:
title = "Dataset A"
data = get_scatter(1, 0)
frames = [
go.Frame(data=get_scatter(1, 1)),
]
elif dataset == 2:
title = "Dataset B"
data = get_scatter(-1, 0)
frames = [
go.Frame(data=get_scatter(-1, 1)),
]
else:
title = "Select a dataset"
data = []
frames = []

fig = get_figure(data, frames, title)
return fig

dash_dcc.start_server(app)
dash_dcc.wait_for_element("#test-change")

dash_dcc.find_elements('input[type="radio"]')[0].click()
assert dash_dcc.wait_for_text_to_equal(
"#relayout-data", ""
), "initial graph data must contain empty string"

dash_dcc.find_elements('input[type="radio"]')[1].click()
assert dash_dcc.wait_for_text_to_equal(
"#relayout-data", "[0, 1, 2]"
), "graph data must contain frame [0,1,2]"

dash_dcc.find_elements('input[type="radio"]')[2].click()
assert dash_dcc.wait_for_text_to_equal(
"#relayout-data", "[0, -1, -2]"
), "graph data must contain frame [0,-1,-2]"