-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
194 lines (152 loc) · 5.92 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from flask import render_template, url_for, request, redirect, jsonify
import pandas as pd
from defaultfigure import generate_default_figs, gen_reference
from initialize import initialize_dir_region, initialize_dir_year, get_cities, get_allapptype, get_allrevtype, get_amountallyr
from generatefigure import generate_fig_rev, generate_fig_app
from forecast import forecasting
from knnalgo import imputearr_lst
from checktest import *
import plotly
import json
import ast
import zipfile
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return render_template("/index.html")
@app.route("/adminlogin", methods=["POST", "GET"])
def adminlogin():
if request.method == "POST":
password = request.form["passw"]
passw, cookie1, cookie2 = get_adminaccess()
if password == passw:
return render_template("admingetcookie.html", cok1=cookie1, cok2=cookie2)
else:
return render_template("adminlogin.html", err=1)
else:
return render_template("adminlogin.html")
@app.route("/checkcookies/<c1>/<c2>")
def checkcookies(c1, c2):
passw, cookie1, cookie2 = get_adminaccess()
bol1 = False
if c1 == cookie1 and c2 == cookie2:
bol1 = True
return jsonify({'check': bol1})
@app.route("/admin")
def admin():
return render_template("admin.html")
@app.route('/admin', methods=['POST'])
def upload_file():
uploaded_file = request.files['file']
add_s = request.form['add_select']
print(add_s)
if uploaded_file.filename != '':
uploaded_file.save("SCBAAsamp/tempuploadzip/"+uploaded_file.filename)
x = add_year(add_s)
with zipfile.ZipFile("SCBAAsamp/tempuploadzip/"+uploaded_file.filename, 'r') as zip_ref:
zip_ref.extractall(x)
os.remove("SCBAAsamp/tempuploadzip/"+uploaded_file.filename)
return redirect(url_for('admin'))
@app.route("/adminupd")
def adminupd():
miss, err = update_scan()
return jsonify({'miss': miss, 'lenmiss': len(miss), 'errpath': err['path'], 'errfix': err['fix'], 'lenerr': len(err['path'])})
@app.route("/canceladminupd")
def canceladminupd():
x = cancel_upd()
y = update_defaultgraph()
return jsonify({'none': x, 'none2': y})
@app.route("/confirmadminupd")
def confirmadminupd():
x = confirm_upd()
y = update_defaultgraph()
return jsonify({'none': x, 'none2': y})
@app.route("/admindelinit")
def admindelinit():
year = initialize_dir_year()
return jsonify({'oldest': year[0], 'newest': year[-1]})
@app.route("/delyear/<yr>")
def delyear(yr):
x = delete_year(yr)
y = update_defaultgraph()
return jsonify({'none': x})
@app.route("/adminaddinit")
def adminaddinit():
year = initialize_dir_year()
return jsonify({'oldest': int(year[0])-1, 'newest': int(year[-1])+1})
@app.route("/datavisdefault", methods=["POST", "GET"])
def datavisdefault():
if request.method == "POST":
data = request.form["data_select"]
reg = request.form["reg_select"]
city = request.form["cit_select"]
year = request.form["yr_select"]
return redirect(url_for("datavis", dt=data, rt=reg, ct=city, yt=year))
else:
default_template = generate_default_figs()
return default_template
@app.route('/city/<reg>/<yr>/<dt>/<pg>')
def city(reg, yr, dt, pg):
cities = get_cities(reg, yr, dt, pg)
return jsonify({'city': cities})
@app.route("/<dt>/<rt>/<ct>/<yt>")
def datavis(dt, rt, ct, yt):
link_init = "SCBAA/" + str(yt) + "/" + rt + ".xlsx"
reg_excel = pd.ExcelFile(link_init)
city_excel = pd.read_excel(
reg_excel, ct)
if dt == "Revenue":
rev_template = generate_fig_rev(city_excel, dt=dt, rt=rt, ct=ct, yt=yt)
return rev_template
else:
app_template = generate_fig_app(city_excel, dt=dt, rt=rt, ct=ct, yt=yt)
return app_template
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/forecast", methods=["POST", "GET"])
def forecast():
year = initialize_dir_year()
region = initialize_dir_region()
if request.method == "POST":
forec = request.form["forectype_select"]
inp_type = request.form["inptype_select"]
reg = request.form["reg_select"]
city = request.form["cit_select"]
input = request.form["inp1"]
year = [int(i) for i in year]
dict_samp = {"Year": year}
dict_samp[inp_type] = imputearr_lst(
get_amountallyr(reg, city, inp_type))
dict_samp[forec] = imputearr_lst(get_amountallyr(reg, city, forec))
return redirect(url_for("results", inp=input, rt=reg, ct=city, inpt=inp_type, forect=forec, dict_samp=dict_samp))
return render_template("forecastinput.html", year=year, region=region)
@app.route('/viewref/<r>/<c>/<i>')
def viewref(r, c, i):
fig = gen_reference(r, c, i)
graph1JSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return jsonify(graph1JSON)
@app.route('/gettype/<i>/<c>/<r>')
def gettype(i, c, r):
if(i == "Revenues"):
forel, forev = get_allapptype(r, c)
inpl, inpv = get_allrevtype(r, c)
elif(i == "Appropriations"):
inpl, inpv = get_allapptype(r, c)
forel, forev = get_allrevtype(r, c)
return jsonify({'inputl': inpl, 'inputv': inpv, 'forecastl': forel, 'forecastv': forev})
@ app.route("/results/<rt>/<ct>/<inp>/<inpt>/<forect>/<dict_samp>", methods=["POST", "GET"])
def results(inp, rt, ct, inpt, forect, dict_samp):
dict_samp = ast.literal_eval(dict_samp)
forecast_template, predict = forecasting(
inp, rt, ct, inpt, forect, dict_samp)
if request.method == "POST":
dict_samp[inpt].append(float(inp))
dict_samp[forect].append(predict[0])
dict_samp["Year"].append(dict_samp["Year"][-1]+1)
inp = request.form["inp1"]
return redirect(url_for("results", inp=inp, rt=rt, ct=ct, inpt=inpt, forect=forect, dict_samp=dict_samp))
return forecast_template
if __name__ == "__main__":
app.run(debug=True)