I need to animate a Plotly’s Sankey but it doesn’t update with the new data. A Sunburst plot based on the same data is OK, as you can see in this example.
Am I missing something here?
(If your dash server is slow, don’t forget to increase interval
in dcc.Interval
to a bigger value in msec, otherwise the callback is called again before the previous call is completed).
import plotly.graph_objs as go
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Output, Input
import random
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Graph(
id="fig_sunburst",
figure=go.Figure(),
animate=True,
),
dcc.Graph(
id="fig_sankey",
figure=go.Figure(),
animate=True,
),
dcc.Interval(
id=f"update",
interval=1000,
n_intervals=0
),
],
)
@app.callback(
Output('fig_sunburst', 'figure'),
Output('fig_sankey', 'figure'),
Input('update', 'n_intervals')
)
def traces_update(n_intervals):
print(f"update {n_intervals}")
# random data
a1 = random.randint(10, 100)
a2 = random.randint(10, 100)
a3 = random.randint(10 ,100)
a0 = a1+a2+a3
b1 = random.randint(10, 100)
b2 = random.randint(10, 100)
b0 = b1+b2
tot = a0+b0
fig_sunburst = go.Figure(
go.Sunburst(
ids=['tot', 'a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'],
labels=['tot', 'a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'],
parents=['', 'tot', 'a0', 'a0', 'a0', 'tot', 'b0', 'b0'],
values=[tot, a0, a1, a2, a3, b0, b1, b2],
branchvalues="total",
)
)
fig_sunburst.update_layout(go.Layout(width=400, height=400,
margin=dict(t=20, l=20, r=20, b=20),))
fig_sankey = go.Figure(
go.Sankey(
node=dict(
label=['tot', 'a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'],
),
link=dict(
source=[0, 0, 1, 1, 1, 5, 5],
target=[1, 5, 2, 3, 4, 6, 7],
value=[a0, b0, a1, a2, a3, b1, b2],
),
)
)
fig_sankey.update_layout(go.Layout(width=400, height=400,
margin=dict(t=20, l=20, r=20, b=20),))
return fig_sunburst, fig_sankey
app.run_server()
Also Posted without answer in Stackoverflow (python - Sankey is not updated on callback - Stack Overflow)