This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
/
app.py
313 lines (251 loc) · 9.04 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import copy
import json
import os
from pathlib import Path
import numpy as np
# imports for "input2"
import pandas as pd
# imports for "/graphics"
from bokeh.embed import components
# imports for "/input1"
from flask import (Flask, flash, redirect, render_template, # , session
request, url_for)
import backend
from graphics import Graphic # import our graphic class
# from flask import session
app = Flask(__name__)
app.secret_key = "super secret key"
session = {}
def reset_session():
"""
sets default values for session variables (only for those variables that require it)
"""
session.clear()
# stored for /graphics
session["tab"] = None # graphic view tab; i.e. all, positive or negative only
session["year_from"] = None
session["year_to"] = None
session[
"indices"
] = (
[]
) # stored the current graphic view indices as a list e.g. ["Amsterdam", "West"]
session["order"] = "descending" # current view, plot "ascending" or "descending"
session["totals"] = []
session["d"] = None
session["row_indexes"] = None
# stored for /input2
session["row_header_to"] = 1 # i.e. defaults to single index only
session[
"column_header_start"
] = "No subpopulations" # i.e. defaults to there being no sub-populations whatsoever
session["column_header_to"] = None
session["column_header_from"] = 0
session["year"] = None # year (date) column
session["year_min"] = None # dataset minimum possible year
session["year_max"] = None # dataset max possible year
# ---------------
# /input1
# ---------------
@app.route("/")
def upload_file():
return render_template("upload1.html")
@app.route("/upload1")
def read_file():
"""get filename and store as session variable"""
reset_session()
# read in the selected filename
args = request.args
if "file" in args:
session["file"] = args["file"]
return show_data()
# -----------------------
# /upload2
# -----------------------
@app.route("/upload2")
def show_data():
#
# acknowledge a change in page variables (row_header_to, column_header_start or column_header_to, year)
#
args = request.args
# handle a change to the number or row indices
if "row_header_to" in args:
session["row_header_to"] = int(args["row_header_to"])
# reset session["year"]
session["year"] = None
# handle a change to "column_header_start" page var. (i.e. first column index)
if "column_header_start" in args:
if args["column_header_start"] != "No subpopulations":
session["column_header_start"] = 1
session["column_header_to"] = 1
else:
session["column_header_start"] = "No subpopulations"
session["column_header_to"] = None
# handle a change in "column_header_to" page var (i.e. final column index)
if "column_header_to" in args:
session["column_header_to"] = int(args["column_header_to"])
#
# populate the lists of column/ row indices from the input variables
#
# row indices (i.e. single row -> [0], two rows -> [0,1])
session["row_indices"] = list(
range(0, session["row_header_to"])
) # list of row indices (consecutive) representing headers
# column indices (i.e. no subpops -> indices = None, indices_backend=[0], 1 subpop -> indices = [0], indices_backend = [0,1])
if session["column_header_start"] != "No subpopulations":
session.pop("column_indices_backend", None)
session["column_indices_backend"] = list(
range(0, session["column_header_to"] + 1)
)
else:
session["column_indices_backend"] = [0]
# generate dataframe for current variables for
new_file = pd.read_excel(
session["file"], header=session["row_indices"], index_col=[0]
)
new_file = new_file.reset_index()
new_file.columns = pd.MultiIndex.from_tuples(list(enumerate(new_file, 1)))
# get values associated with "year" pulldown
if "year" in args:
session["year"] = int(args["year"])
session["year_min"] = None
session["year_max"] = None # reset year range on new data column selection
try:
session["year_min"] = min(
[int(i) for i in new_file.iloc[:, session["year"] - 1]]
)
session["year_max"] = max(
[int(i) for i in new_file.iloc[:, session["year"] - 1]]
)
except:
pass
# generate tables for views
tables = [new_file.to_html(classes="data", index=False)]
# the max number of sub-population columns
columns = range(0, 5)
return render_template(
"upload2.html",
tables=tables,
titles=new_file.columns.values,
columns=columns,
year=session["year"],
row_header_to=session["row_header_to"],
column_header_to=session["column_header_to"],
column_header_start=session["column_header_start"],
year_min=session["year_min"],
year_max=session["year_max"],
)
# ---------------
# /graphics
# ---------------
@app.route("/graphics")
def graphics():
# create the range of possible years from selected year column max and min values
if session["year_min"] and session["year_max"]:
year_options = list(range(session["year_min"], session["year_max"] + 1))
else:
year_options = None
# set initial values of year_to and year_from ONLY IF year_options != None
if session["year_from"] == None and year_options:
session["year_from"] = year_options[0]
if session["year_to"] == None and year_options:
session["year_to"] = year_options[-1]
# ------------------
# update variables from HTML
# ------------------
args = request.args
# "tab" variables from HTML
if "tab" in args:
session["tab"] = args["tab"]
# year variables from
if "year_from" in args:
session["year_from"] = int(args["year_from"])
session["d"] = backend.backend(
session["file"],
session["row_indices"],
session["column_indices_backend"],
session["year"],
session["year_from"],
session["year_to"],
)
# year variables from
if "year_to" in args:
session["year_to"] = int(args["year_to"])
session["d"] = backend.backend(
session["file"],
session["row_indices"],
session["column_indices_backend"],
session["year"],
session["year_from"],
session["year_to"],
)
# year variables from
if "index" in args:
temp = args["index"].replace("_____", " ")
session["indices"].append(temp)
print("\n\n", session["indices"], "\n\n")
if "crumb" in args:
if args["crumb"] == "All Populations":
session["indices"] = []
else:
r = session["indices"].index(args["crumb"].replace("_____", " "))
session["indices"] = session["indices"][0 : r + 1]
if "order" in args:
session["order"] = args["order"]
# get data from backend
if not session["d"]:
session["d"] = backend.backend(
session["file"],
session["row_indices"],
session["column_indices_backend"],
session["year"],
session["year_from"],
session["year_to"],
)
# #output json file for testing
# with open('data.json', 'w') as f:
# json.dump(session["d"], f)
# Create the plot
graph = Graphic()
graph.get_data(session["d"], session["indices"])
graph.generate(session["tab"], by=session["order"])
# generate extra variables
if year_options:
year_from_options = year_options[
0 : year_options.index(int(session["year_to"])) + 1
]
year_to_options = year_options[year_options.index(int(session["year_from"])) :]
else:
year_from_options = None
year_to_options = None
# generate ordered version of sub-pop dictionary
colours = graph.get_colours()
subs = graph.get_subs()
ordered_subs = [s for s in subs if colours[s] != "lightgrey"] + [
s for s in subs if colours[s] == "lightgrey"
]
# Embed plot into HTML via Flask Render
script, div = components(graph.plot)
return render_template(
"graphics.html",
script=script,
div=div,
tab_options=["all", "positive", "negative"],
tab_current=session["tab"],
year_options=year_options,
year_from_options=year_from_options,
year_to_options=year_to_options,
year_from=session["year_from"],
year_to=session["year_to"],
subs=ordered_subs,
crumbs=session["indices"], # navigation index level as list
order=session["order"],
order_options=["ascending", "descending"],
colours=colours,
totals=graph.get_totals()
# subs = ordered_subs
)
# With debug=True, Flask server will auto-reload
# when there are code changes
if __name__ == "__main__":
app.run(port=5000, debug=True)