-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
118 lines (83 loc) · 2.81 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
import json
import random
import os
import networkx as nx
from io import BytesIO
import base64
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from flask import Flask, request, render_template, redirect, url_for, session
import graph as g
app = Flask(__name__)
app.secret_key = os.urandom(24)
def getSessionGraph():
return nx.adjacency_graph(session['graph'])
def setSessionGraph(graph):
session['graph'] = nx.adjacency_data(graph)
def updateSessionGraph(update = lambda _: None, **kw):
graph = getSessionGraph()
update(graph,**kw)
setSessionGraph(graph)
@app.before_request
def before_request():
# store empty graph
if 'graph' not in session:
session['graph'] = nx.adjacency_data(nx.Graph())
@app.route("/")
def graphInterfaceView():
graph = getSessionGraph()
# draw graph
nx.draw_networkx(graph,font_color='white', pos=nx.circular_layout(graph))
# base64 encode graph image
figfile = BytesIO()
plt.savefig(figfile, format='png')
plt.close()
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue()).decode('ascii')
return render_template('index.html',
image_base64 = figdata_png,
matrix = str(nx.to_numpy_array(graph)).replace('.',',').replace('\n',','),
graph = graph,
data = json.dumps(nx.node_link_data(graph)) )
# add and remove node
@app.route("/addnode")
def addnode():
updateSessionGraph(g.add)
return redirect(url_for('graphInterfaceView'))
@app.route("/removenode")
def removenode():
updateSessionGraph(g.remove, node=int(request.args.get('label')))
return redirect(url_for('graphInterfaceView'))
# add and remove edge
@app.route("/addedge")
def addedge():
updateSessionGraph(lambda graph: graph.add_edge(request.args.get('label1'),request.args.get('label2')))
return redirect(url_for('graphInterfaceView'))
@app.route("/toggleedge")
def toggleedge():
updateSessionGraph(g.toggle,edge=[int(n) for n in request.args.get('label').split('_')])
return redirect(url_for('graphInterfaceView'))
# complement graph
@app.route("/complement")
def complementgraph():
graph = getSessionGraph()
setSessionGraph(nx.complement(graph))
return redirect(url_for('graphInterfaceView'))
# complement edges for single node
@app.route("/complementnode")
def complementnode():
updateSessionGraph(g.complement,node=int(request.args.get('label')))
return redirect(url_for('graphInterfaceView'))
# clear edges
@app.route("/clearedges")
def clearedges():
updateSessionGraph(lambda graph: graph.remove_edges_from(graph.edges()))
return redirect(url_for('graphInterfaceView'))
# clear graph
@app.route("/clear")
def cleargraph():
updateSessionGraph(lambda graph: graph.clear())
return redirect(url_for('graphInterfaceView'))
if __name__ == '__main__':
app.run()