Skip to content

Commit ad00124

Browse files
author
Xing Han Lu
committed
Add usage examples for colored social networks
1 parent 969e302 commit ad00124

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import requests
2+
3+
import dash
4+
import dash_html_components as html
5+
import dash_cytoscape as cyto
6+
7+
app = dash.Dash(__name__)
8+
9+
# Request the data from the sample network
10+
url = 'https://raw.githubusercontent.com/plotly/dash-cytoscape/master/demos/data/sample_network.txt'
11+
data = requests.get(url).text.split('\n')
12+
13+
14+
nodes = set()
15+
cy_edges, cy_nodes = [], []
16+
edges = data[:8000]
17+
colors = ['red', 'blue', 'green', 'yellow', 'pink']
18+
19+
for edge in edges:
20+
source, target = edge.split(" ")
21+
color = colors[len(cy_nodes) % 5]
22+
23+
if source not in nodes: # Add the source node
24+
nodes.add(source)
25+
cy_nodes.append({"data": {"id": source}, "classes": color})
26+
27+
if target not in nodes: # Add the target node
28+
nodes.add(target)
29+
cy_nodes.append({"data": {"id": target}, "classes": color})
30+
31+
cy_edges.append({ # Add the Edge Node
32+
'data': {'source': source, 'target': target},
33+
'classes': color
34+
})
35+
36+
37+
default_stylesheet = [
38+
{
39+
"selector": 'node',
40+
'style': {
41+
"opacity": 0.9,
42+
'height': 15,
43+
'width': 15,
44+
'background-color': '#222222'
45+
}
46+
},
47+
{
48+
"selector": 'edge',
49+
'style': {
50+
"curve-style": "bezier",
51+
"opacity": 0.2,
52+
'width': 1
53+
}
54+
},
55+
*[{
56+
"selector": '.' + color,
57+
'style': {'line-color': color}
58+
} for color in colors]
59+
]
60+
61+
app.layout = html.Div([
62+
cyto.Cytoscape(
63+
id='cytoscape',
64+
elements=cy_edges + cy_nodes,
65+
stylesheet=default_stylesheet,
66+
layout={'name': 'concentric'},
67+
style={'height': '95vh', 'width': '100%'}
68+
)
69+
])
70+
71+
72+
if __name__ == '__main__':
73+
app.run_server(debug=True)

demos/usage-grid-social-network.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import requests
2+
3+
import dash
4+
import dash_html_components as html
5+
import dash_cytoscape as cyto
6+
7+
app = dash.Dash(__name__)
8+
9+
# Request the data from the sample network
10+
url = 'https://raw.githubusercontent.com/plotly/dash-cytoscape/master/demos/data/sample_network.txt'
11+
data = requests.get(url).text.split('\n')
12+
13+
14+
nodes = set()
15+
cy_edges, cy_nodes = [], []
16+
edges = data[:500]
17+
colors = ['red', 'blue', 'green', 'yellow', 'pink']
18+
19+
for edge in edges:
20+
source, target = edge.split(" ")
21+
color = colors[len(cy_nodes) % 5]
22+
23+
if source not in nodes: # Add the source node
24+
nodes.add(source)
25+
cy_nodes.append({"data": {"id": source}, "classes": color})
26+
27+
if target not in nodes: # Add the target node
28+
nodes.add(target)
29+
cy_nodes.append({"data": {"id": target}, "classes": color})
30+
31+
cy_edges.append({ # Add the Edge Node
32+
'data': {'source': source, 'target': target},
33+
'classes': color
34+
})
35+
36+
37+
default_stylesheet = [
38+
{
39+
"selector": 'node',
40+
'style': {
41+
"opacity": 0.9,
42+
'height': 15,
43+
'width': 15,
44+
'background-color': '#222222'
45+
}
46+
},
47+
{
48+
"selector": 'edge',
49+
'style': {
50+
"curve-style": "bezier",
51+
"opacity": 0.3,
52+
'width': 2
53+
}
54+
},
55+
*[{
56+
"selector": '.' + color,
57+
'style': {'line-color': color}
58+
} for color in colors]
59+
]
60+
61+
app.layout = html.Div([
62+
cyto.Cytoscape(
63+
id='cytoscape',
64+
elements=cy_edges + cy_nodes,
65+
stylesheet=default_stylesheet,
66+
layout={
67+
'name': 'grid',
68+
'rows': 15
69+
},
70+
style={'height': '95vh', 'width': '100%'}
71+
)
72+
])
73+
74+
75+
if __name__ == '__main__':
76+
app.run_server(debug=True)

0 commit comments

Comments
 (0)