-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_graph.py
More file actions
65 lines (47 loc) · 1.71 KB
/
generate_graph.py
File metadata and controls
65 lines (47 loc) · 1.71 KB
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
from pyvis.network import Network
import hashlib
import json
INPUT_PATH = "database/output.json"
OUTPUT_PATH = "database/graph.html"
# Hash function from module name (purely aesthetics)
def letter_to_hex(str):
"""
A function that takes a string input and converts it to a hexadecimal colour value.
Parameters:
str (str): The input string to be converted to a hexadecimal colour value.
Returns:
str: The hexadecimal color value generated from the input string.
"""
sha256_hash = hashlib.sha256(str.encode()).hexdigest()
# Take the first six characters of the hash and convert to RGB values
r = int(sha256_hash[:2], 16)
g = int(sha256_hash[2:4], 16)
b = int(sha256_hash[4:6], 16)
# Convert the RGB values to a hexadecimal color
hex_color = "#{:02x}{:02x}{:02x}".format(r, g, b)
return hex_color
def load_lines(path):
with open(path) as f:
lines = f.readlines()
return lines
def generate_graph():
"""
Generate a graph using the Network class, add nodes and edges from the loaded lines, and display the graph.
"""
net = Network(directed=True, height=1200)
net.repulsion()
net.show_buttons(filter_=['physics'])
lines = load_lines(INPUT_PATH)
# Add nodes
for line in lines:
json_obj = json.loads(line)
if "name" in line:
net.add_node(json_obj['id'], label=json_obj['name'], color=letter_to_hex(json_obj['name']))
# Add edges
for line in lines:
json_obj = json.loads(line)
if "node_from" in line:
net.add_edge(json_obj['node_from'], json_obj['node_to'])
net.show(OUTPUT_PATH, notebook=False)
if __name__ == "__main__":
generate_graph()