-
Notifications
You must be signed in to change notification settings - Fork 2
/
flask_app.py
122 lines (91 loc) · 3.41 KB
/
flask_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
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
119
120
121
122
import traceback
import flask as f
import os
# workaround for git import errors
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = "/usr/bin/git"
import git
from timeout import time_limit, TimeoutException
from info import *
from search import temperament_search
# from werkzeug.middleware.profiler import ProfilerMiddleware
app = f.Flask(__name__)
# app.wsgi_app = ProfilerMiddleware(app.wsgi_app)
@app.route("/index")
@app.route("/")
def index():
if f.request.method == "GET":
args = f.request.args
return f.render_template("./temper.jinja", args=args)
return f.render_template("./temper.jinja")
@app.route("/search", methods=["GET"])
def search():
if f.request.method == "GET":
args = f.request.args.to_dict()
if len(args) == 0:
return f.render_template("./search.jinja")
for k in list(args.keys()):
if args[k] == "" or args[k].isspace():
del args[k]
try:
with time_limit(5):
res = temperament_search(args)
except TimeoutException as e:
raise TimeoutException("Calculation took too long!") from e
return f.render_template("./search.jinja", args=args, res=res)
raise ValueError("nothing submitted")
@app.route("/result", methods=["GET"])
def result():
if f.request.method == "GET":
args = f.request.args
args = args.to_dict()
for k in list(args.keys()):
if args[k] == "" or args[k].isspace():
del args[k]
args["tenney"] = "tenney" in args
basis, s_expanded = parse_subgroup(args["subgroup"])
if "submit_edo" in args:
assert "edos" in args, "no edos entered"
T, T_expanded = from_edos(args["edos"], basis, s_expanded)
elif "submit_comma" in args:
assert "commas" in args, "no commas entered"
T, T_expanded = from_commas(args["commas"], basis, s_expanded)
else:
raise ValueError("nothing submitted")
try:
with time_limit(5):
html_info = info(T, basis, T_expanded, s_expanded, args)
except TimeoutException as e:
raise TimeoutException("Calculation took too long!") from e
return f.render_template("./result.jinja", res=html_info, args=f.request.args)
raise ValueError("nothing submitted")
@app.route("/update", methods=["POST"])
def update():
if f.request.method == "POST":
wd = os.getcwd()
if wd.endswith("temper"):
repo = git.Repo(".")
else:
repo = git.Repo("./temper")
origin = repo.remotes.origin
if not "main" in repo.heads:
repo.create_head("main", origin.refs.main)
repo.heads.main.set_tracking_branch(origin.refs.main).checkout()
origin.pull()
return "", 200
return "", 400
@app.route("/xen")
def xen():
return '<img src="static/xen.png" width="550">'
@app.route("/test")
def test():
return "hey5"
@app.errorhandler(500)
def internal_error(_):
print("500 error caught")
return "<pre>" + traceback.format_exc() + "</pre>", 500
if __name__ == "__main__":
# app.run(host="127.0.0.1", debug=True)
app.run(debug=True)
# app.run(debug=True, threaded=True)
# app.run(threaded=True)