-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathusage-preset-animation.py
74 lines (61 loc) · 1.79 KB
/
usage-preset-animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Shows how to animate a graph using preset positions that are modified by a callback
"""
import dash
from dash import Input, Output, html, callback
import dash_cytoscape as cyto
app = dash.Dash(__name__)
nodes = [
{
"data": {"id": short, "label": label},
"position": {"x": 20 * lat, "y": -20 * longitude},
}
for short, label, longitude, lat in (
("la", "Los Angeles", 34.03, -118.25),
("nyc", "New York", 40.71, -74),
)
]
edges = [
{"data": {"source": source, "target": target}}
for source, target in (("la", "nyc"),)
]
elements = nodes + edges
default_stylesheet = [
{
"selector": "node",
"style": {"background-color": "BFD7B5", "label": "data(label)"},
},
{"selector": "edge", "style": {"line-color": "#A3C4BC"}},
]
app.layout = html.Div(
[
html.Div(html.Button("Change elements", id="button")),
cyto.Cytoscape(
id="cytoscape-elements-callbacks",
layout={"name": "preset", "animate": True, "animationDuration": 1000},
autoRefreshLayout=True,
stylesheet=default_stylesheet,
style={"width": "100%", "height": "450px"},
elements=elements,
),
]
)
@callback(Output("cytoscape-elements-callbacks", "layout"), Input("button", "n_clicks"))
def update_elements(n_clicks):
if not n_clicks:
n_clicks = 0
layout = {
"name": "preset",
"animate": True,
"animationDuration": 1000,
"positions": {
node_id: {"x": 20 * lat, "y": -20 * longitude}
for node_id, longitude, lat in (
("la", 34.03, -118.25 + 10 * n_clicks),
("nyc", 40.71, -74),
)
},
}
return layout
if __name__ == "__main__":
app.run(debug=False)