forked from plotly/dash-cytoscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage-responsive-graph.py
98 lines (83 loc) · 2.65 KB
/
usage-responsive-graph.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Original Demo: http://js.cytoscape.org/demos/cose-layout/
Note: This implementation looks different from the original implementation,
although the input paramters are exactly the same.
"""
import urllib.request
import json
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_cytoscape as cyto
app = dash.Dash(__name__)
server = app.server
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
# Load Data
with urllib.request.urlopen('https://js.cytoscape.org/demos/colajs-graph/data.json') as url:
elements = json.loads(url.read().decode())
with urllib.request.urlopen('https://js.cytoscape.org/demos/colajs-graph/cy-style.json') as url:
stylesheet = json.loads(url.read().decode())
styles = {
'container': {
'position': 'fixed',
'display': 'flex',
'flex-direction': 'column',
'height': '100%',
'width': '100%'
},
'cy-container': {
'flex': '1',
'position': 'relative'
},
'cytoscape': {
'position': 'absolute',
'width': '100%',
'height': '100%',
'z-index': 999
}
}
# App
app.layout = html.Div(style=styles['container'], children=[
html.Div([
html.Button("Responsive Toggle", id='toggle-button'),
html.Div(id='toggle-text')
]),
html.Div(className='cy-container', style=styles['cy-container'], children=[
cyto.Cytoscape(
id='cytoscape',
elements=elements,
stylesheet=stylesheet,
style=styles['cytoscape'],
layout={
'name': 'cose',
'idealEdgeLength': 100,
'nodeOverlap': 20,
'refresh': 20,
'fit': True,
'padding': 30,
'randomize': False,
'componentSpacing': 100,
'nodeRepulsion': 400000,
'edgeElasticity': 100,
'nestingFactor': 5,
'gravity': 80,
'numIter': 1000,
'initialTemp': 200,
'coolingFactor': 0.95,
'minTemp': 1.0
},
responsive=True
)
])
])
@app.callback(Output('cytoscape', 'responsive'), [Input('toggle-button', 'n_clicks')])
def toggle_responsive(n_clicks):
n_clicks = 2 if n_clicks is None else n_clicks
toggle_on = n_clicks % 2 == 0
return toggle_on
@app.callback(Output('toggle-text', 'children'), [Input('cytoscape', 'responsive')])
def update_toggle_text(responsive):
return '\t' + 'Responsive ' + ('On' if responsive else 'Off')
if __name__ == '__main__':
app.run_server(debug=True)