-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphShow.py
94 lines (82 loc) · 2.48 KB
/
GraphShow.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
#!/usr/bin/env python3
# coding: utf-8
# File: GraphShow.py
# Author: lhy<[email protected],https://huangyong.github.io>
# Date: 18-7-24
'''创建展示页面'''
class GraphShow():
def __init__(self):
self.base = '''
<html>
<head>
<script type="text/javascript" src="VIS/dist/vis.js"></script>
<link href="VIS/dist/vis.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="VIS_draw"></div>
<script type="text/javascript">
var nodes = data_nodes;
var edges = data_edges;
var container = document.getElementById("VIS_draw");
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: {
shape: 'circle',
size: 15,
font: {
size: 15
}
},
edges: {
font: {
size: 10,
align: 'center'
},
color: 'red',
arrows: {
to: {enabled: true, scaleFactor: 1.2}
},
smooth: {enabled: true}
},
physics: {
enabled: true
}
};
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
'''
'''读取文件数据'''
def create_page(self, events):
nodes = []
for event in events:
nodes.append(event[0])
nodes.append(event[1])
node_dict = {node: index for index, node in enumerate(nodes)}
data_nodes = []
data_edges = []
for node, id in node_dict.items():
data = {}
data["group"] = 'Event'
data["id"] = id
data["label"] = node
data_nodes.append(data)
for edge in events:
data = {}
data['from'] = node_dict.get(edge[0])
data['label'] = ''
data['to'] = node_dict.get(edge[1])
data_edges.append(data)
self.create_html(data_nodes, data_edges)
return
'''生成html文件'''
def create_html(self, data_nodes, data_edges):
f = open('graph_show.html', 'w+')
html = self.base.replace('data_nodes', str(data_nodes)).replace('data_edges', str(data_edges))
f.write(html)
f.close()