-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (45 loc) · 1.51 KB
/
app.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
from flask import Flask, jsonify, send_from_directory, render_template
from flask_cors import CORS
import os
import json
import time
from main import main
from main import build_graph
from conf import SD, OVP
app = Flask(__name__)
CORS(app)
@app.route("/")
def index():
return render_template("loading.html")
@app.route("/main")
def main_route():
main()
return render_template("index.html")
@app.route("/graph")
def get_graph():
graph_path = "graph.json"
if os.path.exists(graph_path):
with open(graph_path, "r", encoding="utf-8") as f:
saved_graph = json.load(f)
else:
saved_graph = None
current_graph = build_graph(OVP)
if (
saved_graph is None or
len(saved_graph["nodes"]) != len(current_graph["nodes"]) or
len(saved_graph["edges"]) != len(current_graph["edges"]) or
{node["id"] for node in saved_graph["nodes"]} != {node["id"] for node in current_graph["nodes"]}
):
with open(graph_path, "w", encoding="utf-8") as f:
json.dump(current_graph, f, ensure_ascii=False, indent=4)
return jsonify({"status": "updated", "graph": current_graph})
return jsonify(saved_graph)
@app.route("/libs/<path:filename>")
def serve_libs(filename):
return send_from_directory("libs", filename)
if __name__ == "__main__":
try:
app.run(debug=True, port=5001)
except OSError:
with open(os.path.join(SD, "flask.log"), "r") as f:
f.write(f"Error: port 5001 is alredy in use\n")