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
/
Copy pathdraw.py
77 lines (67 loc) · 1.73 KB
/
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
import json
# Read JSON file
with open('diff.json', 'r') as file:
data = json.load(file)
# Prepare nodes and edges data for vis.js
nodes = [{'id': node_id, 'label': node_data['label']} for node_id, node_data in data['nodes'].items()]
edges = [{'from': str(edge['from']), 'to': str(edge['to']), 'label': edge['label']} for edge in data['edges']]
# Generate HTML code
html_code = f"""<!doctype html>
<html>
<head>
<title>Graph from JSON Data</title>
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<style type="text/css">
body, html {{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}}
#mynetwork {{
width: 100%;
height: 100%;
border: 1px solid lightgray;
}}
</style>
</head>
<body>
<div id="mynetwork"></div>
<script type="text/javascript">
var nodes = new vis.DataSet({nodes});
var edges = new vis.DataSet({edges});
var container = document.getElementById('mynetwork');
var data = {{
nodes: nodes,
edges: edges
}};
var options = {{
autoResize: true,
nodes: {{
font: {{
size: 15
}}
}},
smooth: {{ enabled: true }},
edges: {{
arrows: 'to'
}},
physics: {{
enabled: false,
hierarchicalRepulsion: {{
nodeDistance: -3000
}}
}}
}};
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
"""
# Substitute nodes and edges data into HTML code
html_code = html_code.replace('{json_nodes}', json.dumps(nodes))
html_code = html_code.replace('{json_edges}', json.dumps(edges))
# Write HTML code to a file
with open('graph.html', 'w') as file:
file.write(html_code)