FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
dash-cytoscape/usage-elements.py at main · plotly/dash-cytoscape · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
plotly
/
dash-cytoscape
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
124
Star
678
Code
Issues
61
Pull requests
8
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
dash-cytoscape
/
usage-elements.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (237 loc) · 8.93 KB
Breadcrumbs
dash-cytoscape
/
usage-elements.py
Copy path
File metadata and controls
277 lines (237 loc) · 8.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import
json
import
dash
from
dash
import
Input
,
Output
,
State
,
dcc
,
html
,
callback
import
dash_cytoscape
as
cyto
from
demos
import
dash_reusable_components
as
drc
app
=
dash
.
Dash
(
__name__
)
server
=
app
.
server
# ###################### DATA PREPROCESSING ######################
# Load data
with
open
(
"demos/data/sample_network.txt"
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
network_data
=
f
.
read
().
split
(
"
\n
"
)
# We select the first 750 edges and associated nodes for an easier visualization
edges
=
network_data
[:
750
]
nodes
=
set
()
following_node_di
=
{}
# user id -> list of users they are following
following_edges_di
=
{}
# user id -> list of cy edges starting from user id
followers_node_di
=
{}
# user id -> list of followers (cy_node format)
followers_edges_di
=
{}
# user id -> list of cy edges ending at user id
cy_edges
=
[]
cy_nodes
=
[]
for
edge
in
edges
:
if
" "
not
in
edge
:
continue
source
,
target
=
edge
.
split
(
" "
)
cy_edge
=
{
"data"
: {
"id"
:
source
+
target
,
"source"
:
source
,
"target"
:
target
}}
cy_target
=
{
"data"
: {
"id"
:
target
,
"label"
:
"User #"
+
str
(
target
[
-
5
:])}}
cy_source
=
{
"data"
: {
"id"
:
source
,
"label"
:
"User #"
+
str
(
source
[
-
5
:])}}
if
source
not
in
nodes
:
nodes
.
add
(
source
)
cy_nodes
.
append
(
cy_source
)
if
target
not
in
nodes
:
nodes
.
add
(
target
)
cy_nodes
.
append
(
cy_target
)
# Process dictionary of following
if
not
following_node_di
.
get
(
source
):
following_node_di
[
source
]
=
[]
if
not
following_edges_di
.
get
(
source
):
following_edges_di
[
source
]
=
[]
following_node_di
[
source
].
append
(
cy_target
)
following_edges_di
[
source
].
append
(
cy_edge
)
# Process dictionary of followers
if
not
followers_node_di
.
get
(
target
):
followers_node_di
[
target
]
=
[]
if
not
followers_edges_di
.
get
(
target
):
followers_edges_di
[
target
]
=
[]
followers_node_di
[
target
].
append
(
cy_source
)
followers_edges_di
[
target
].
append
(
cy_edge
)
genesis_node
=
cy_nodes
[
0
]
genesis_node
[
"classes"
]
=
"genesis"
default_elements
=
[
genesis_node
]
default_stylesheet
=
[
{
"selector"
:
"node"
,
"style"
: {
"opacity"
:
0.65
,
"z-index"
:
9999
}},
{
"selector"
:
"edge"
,
"style"
: {
"curve-style"
:
"bezier"
,
"opacity"
:
0.45
,
"z-index"
:
5000
},
},
{
"selector"
:
".followerNode"
,
"style"
: {
"background-color"
:
"#0074D9"
}},
{
"selector"
:
".followerEdge"
,
"style"
: {
"mid-target-arrow-color"
:
"blue"
,
"mid-target-arrow-shape"
:
"vee"
,
"line-color"
:
"#0074D9"
,
},
},
{
"selector"
:
".followingNode"
,
"style"
: {
"background-color"
:
"#FF4136"
}},
{
"selector"
:
".followingEdge"
,
"style"
: {
"mid-target-arrow-color"
:
"red"
,
"mid-target-arrow-shape"
:
"vee"
,
"line-color"
:
"#FF4136"
,
},
},
{
"selector"
:
".genesis"
,
"style"
: {
"background-color"
:
"#B10DC9"
,
"border-width"
:
2
,
"border-color"
:
"purple"
,
"border-opacity"
:
1
,
"opacity"
:
1
,
"label"
:
"data(label)"
,
"color"
:
"#B10DC9"
,
"text-opacity"
:
1
,
"font-size"
:
12
,
"z-index"
:
9999
,
},
},
{
"selector"
:
":selected"
,
"style"
: {
"border-width"
:
2
,
"border-color"
:
"black"
,
"border-opacity"
:
1
,
"opacity"
:
1
,
"label"
:
"data(label)"
,
"color"
:
"black"
,
"font-size"
:
12
,
"z-index"
:
9999
,
},
},
]
# ################################# APP LAYOUT ################################
styles
=
{
"json-output"
: {
"overflow-y"
:
"scroll"
,
"height"
:
"calc(50% - 25px)"
,
"border"
:
"thin lightgrey solid"
,
},
"tab"
: {
"height"
:
"calc(98vh - 80px)"
},
}
app
.
layout
=
html
.
Div
(
[
html
.
Div
(
className
=
"eight columns"
,
children
=
[
cyto
.
Cytoscape
(
id
=
"cytoscape"
,
elements
=
default_elements
,
stylesheet
=
default_stylesheet
,
style
=
{
"height"
:
"95vh"
,
"width"
:
"100%"
},
)
],
),
html
.
Div
(
className
=
"four columns"
,
children
=
[
dcc
.
Tabs
(
id
=
"tabs"
,
children
=
[
dcc
.
Tab
(
label
=
"Control Panel"
,
children
=
[
drc
.
NamedDropdown
(
name
=
"Layout"
,
id
=
"dropdown-layout"
,
options
=
drc
.
DropdownOptionsList
(
"random"
,
"grid"
,
"circle"
,
"concentric"
,
"breadthfirst"
,
"cose"
,
),
value
=
"grid"
,
clearable
=
False
,
),
drc
.
NamedRadioItems
(
name
=
"Expand"
,
id
=
"radio-expand"
,
options
=
drc
.
DropdownOptionsList
(
"followers"
,
"following"
),
value
=
"followers"
,
),
],
),
dcc
.
Tab
(
label
=
"JSON"
,
children
=
[
html
.
Div
(
style
=
styles
[
"tab"
],
children
=
[
html
.
P
(
"Node Object JSON:"
),
html
.
Pre
(
id
=
"tap-node-json-output"
,
style
=
styles
[
"json-output"
],
),
html
.
P
(
"Edge Object JSON:"
),
html
.
Pre
(
id
=
"tap-edge-json-output"
,
style
=
styles
[
"json-output"
],
),
],
)
],
),
],
),
],
),
]
)
# ############################## CALLBACKS ####################################
@
callback
(
Output
(
"tap-node-json-output"
,
"children"
),
Input
(
"cytoscape"
,
"tapNode"
))
def
display_tap_node
(
data
):
return
json
.
dumps
(
data
,
indent
=
2
)
@
callback
(
Output
(
"tap-edge-json-output"
,
"children"
),
Input
(
"cytoscape"
,
"tapEdge"
))
def
display_tap_edge
(
data
):
return
json
.
dumps
(
data
,
indent
=
2
)
@
callback
(
Output
(
"cytoscape"
,
"layout"
),
Input
(
"dropdown-layout"
,
"value"
))
def
update_cytoscape_layout
(
layout
):
return
{
"name"
:
layout
}
@
callback
(
Output
(
"cytoscape"
,
"elements"
),
Input
(
"cytoscape"
,
"tapNodeData"
),
State
(
"cytoscape"
,
"elements"
),
State
(
"radio-expand"
,
"value"
),
)
def
generate_elements
(
nodeData
,
elements
,
expansion_mode
):
if
not
nodeData
:
return
default_elements
# If the node has already been expanded, we don't expand it again
if
nodeData
.
get
(
"expanded"
):
return
elements
# This retrieves the currently selected element, and tag it as expanded
for
element
in
elements
:
if
nodeData
[
"id"
]
==
element
.
get
(
"data"
).
get
(
"id"
):
element
[
"data"
][
"expanded"
]
=
True
break
if
expansion_mode
==
"followers"
:
followers_nodes
=
followers_node_di
.
get
(
nodeData
[
"id"
])
followers_edges
=
followers_edges_di
.
get
(
nodeData
[
"id"
])
if
followers_nodes
:
for
node
in
followers_nodes
:
node
[
"classes"
]
=
"followerNode"
elements
.
extend
(
followers_nodes
)
if
followers_edges
:
for
follower_edge
in
followers_edges
:
follower_edge
[
"classes"
]
=
"followerEdge"
elements
.
extend
(
followers_edges
)
elif
expansion_mode
==
"following"
:
following_nodes
=
following_node_di
.
get
(
nodeData
[
"id"
])
following_edges
=
following_edges_di
.
get
(
nodeData
[
"id"
])
if
following_nodes
:
for
node
in
following_nodes
:
if
node
[
"data"
][
"id"
]
!=
genesis_node
[
"data"
][
"id"
]:
node
[
"classes"
]
=
"followingNode"
elements
.
append
(
node
)
if
following_edges
:
for
follower_edge
in
following_edges
:
follower_edge
[
"classes"
]
=
"followingEdge"
elements
.
extend
(
following_edges
)
return
elements
if
__name__
==
"__main__"
:
app
.
run
(
debug
=
True
)
Back
|
FazBrowse Home
|
New Git URL