This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_draw.py
106 lines (94 loc) · 2.31 KB
/
test_draw.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
99
100
101
102
103
104
105
106
import json
# Load the JSON data
graph_data = """
{
"nodes": {
"0": {
"label": "Quaid-e-Azam Muhammad Ali Jinnah",
"category": "related"
},
"2": {
"label": "Angela Merkel",
"category": "related"
},
"4": {
"label": "abc",
"category": "related"
},
"5": {
"label": "Elon Musk",
"category": "related"
},
"7": {
"label": "SpaceX",
"category": "related"
}
},
"edges": [
{
"from": "4",
"to": "5",
"label": "founded by",
"category": "related"
},
{
"from": "5",
"to": "4",
"label": "owner of",
"category": "related"
},
{
"from": "5",
"to": "7",
"label": "owner of",
"category": "related"
}
]
}
"""
data = json.loads(graph_data)
# Create nodes and edges data for vis.js
nodes = []
edges = []
for node_id, node_data in data['nodes'].items():
node = {'id': node_id, 'label': node_data['label']}
nodes.append(node)
for edge in data['edges']:
edge = {'from': edge['from'], 'to': edge['to'], 'label': edge['label']}
edges.append(edge)
# Create HTML content
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>Graph Visualization</title>
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<link href="https://unpkg.com/vis-network/styles/vis-network.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="network"></div>
<script type="text/javascript">
var nodes = new vis.DataSet({json.dumps(nodes)});
var edges = new vis.DataSet({json.dumps(edges)});
var container = document.getElementById('network');
var data = {{
nodes: nodes,
edges: edges
}};
var options = {{
arrows: {{
to: {{enabled: true, scaleFactor: 1, type: 'arrow'}}
}},
physics: {{
enabled: false
}}
}};
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
"""
# Save the HTML content to a file
with open('graph.html', 'w') as html_file:
html_file.write(html_content)
print("HTML file generated successfully!")