-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathusage-grid-social-network.py
70 lines (56 loc) · 1.69 KB
/
usage-grid-social-network.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
import requests
import dash
from dash import html
import dash_cytoscape as cyto
app = dash.Dash(__name__)
# Request the data from the sample network
url = "https://raw.githubusercontent.com/plotly/dash-cytoscape/master/demos/data/sample_network.txt"
data = requests.get(url, timeout=100).text.split("\n")
nodes = set()
cy_edges, cy_nodes = [], []
edges = data[:500]
colors = ["red", "blue", "green", "yellow", "pink"]
for edge in edges:
source, target = edge.split(" ")
color = colors[len(cy_nodes) % 5]
if source not in nodes: # Add the source node
nodes.add(source)
cy_nodes.append({"data": {"id": source}, "classes": color})
if target not in nodes: # Add the target node
nodes.add(target)
cy_nodes.append({"data": {"id": target}, "classes": color})
cy_edges.append(
{ # Add the Edge Node
"data": {"source": source, "target": target},
"classes": color,
}
)
default_stylesheet = [
{
"selector": "node",
"style": {
"opacity": 0.9,
"height": 15,
"width": 15,
"background-color": "#222222",
},
},
{
"selector": "edge",
"style": {"curve-style": "bezier", "opacity": 0.3, "width": 2},
},
*[{"selector": "." + color, "style": {"line-color": color}} for color in colors],
]
app.layout = html.Div(
[
cyto.Cytoscape(
id="cytoscape",
elements=cy_edges + cy_nodes,
stylesheet=default_stylesheet,
layout={"name": "grid", "rows": 15},
style={"height": "95vh", "width": "100%"},
)
]
)
if __name__ == "__main__":
app.run(debug=True)