-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tree_website.py
65 lines (48 loc) · 1.56 KB
/
tree_website.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
from flask import Flask, send_file, request, render_template
import networkx as nx
import matplotlib.pyplot as plt
import io
from PIL import Image
import json
import os
import pickle
app = Flask(__name__)
def get_data(root, layers):
os.system(f"python3 connection_tree_v2.py {root} {layers}")
f = open(f".{root}_{layers}".replace("/", "o"), "rb")
data = pickle.loads(f.read())
f.close()
return data
def add_nodes_edges(graph, node, parent=None):
"""
Recursively add nodes and edges to the graph.
"""
if parent:
graph.add_edge(parent, node['name'])
if 'children' in node:
for child in node['children']:
add_nodes_edges(graph, child, node['name'])
def plot_tree(data, output_file='tree.png'):
"""
Plot a tree graph from the data and save it as an image.
"""
G = nx.DiGraph()
add_nodes_edges(G, data)
pos = nx.spring_layout(G, seed=42, k=0.5) # Position nodes using spring layout
plt.figure(figsize=(14, 10))
nx.draw(G, pos, with_labels=True, arrows=True, node_size=2000, node_color='lightblue', font_size=6, font_weight='bold', edge_color='gray')
plt.title('Tree Graph')
plt.savefig(output_file)
@app.route('/')
def index():
return render_template('tree.html')
@app.route('/generate_tree')
def generate_tree():
root = request.headers.get('root')
layers = int(request.headers.get('layers'))
data = get_data(root, layers)
data = data[root]
plot_tree(data)
return send_file("tree.png", mimetype='image/png')
if __name__ == '__main__':
app.run()