-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
283 lines (244 loc) · 9.23 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
from datetime import datetime as dt
from datetime import timedelta
import os
import io
import flask
import pandas as pd
from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.palettes import Set2_5 as palette
from bokeh.models import Legend, HoverTool
from bokeh.models.widgets import Panel, Tabs
from bokeh.models import DatetimeTickFormatter
import madison_lake_levels as mll
app = flask.Flask(__name__)
lldb = mll.db.LakeLevelDB(
**mll.db.config_from_dburl(os.getenv('DATABASE_URL'))
)
def _main_page(df, date=''):
df = df.sort_index()
req_levels = mll.required_levels.required_levels
req_maxes = req_levels['summer_maximum']
lakes = ['mendota', 'monona', 'waubesa', 'kegonsa']
is_high = []
heights = []
for lake in lakes:
df_lake = df[lake].dropna()
if not df_lake.size:
return flask.render_template(
'main.html',
info=[],
high_lakes='No data available at this time.',
date=date
)
heights.append(df_lake.iloc[-1])
is_high.append(df_lake.iloc[-1] > req_maxes[lake])
total_diff = abs(pd.Series(dict(zip(lakes, heights))) - req_maxes[lakes])
feet_diff = total_diff.astype(int)
inches_diff = (total_diff - feet_diff) * 12
info = zip(
[lake.title() for lake in lakes],
is_high,
feet_diff.tolist(),
inches_diff.tolist(),
)
high_lakes = [lake.title() for lake, high in zip(lakes, is_high) if high]
if len(high_lakes) == 1:
verb = 'is' if date == '' else 'was'
else:
verb = 'are' if date == '' else 'were'
if not high_lakes:
msg = f'All lakes {verb} below their state-required maximum.'
elif len(high_lakes) == 1:
msg = f'{high_lakes[0]} {verb} above its state-required maximum.'
else:
msg = ', '.join(high_lakes[:-1]) + f', and {high_lakes[-1]}'
msg += f' {verb} above their state-required maximums.'
bokeh_script, bokeh_div = plot_year()
return flask.render_template(
'main.html', info=info, high_lakes=msg, date=date,
plot_div=bokeh_div, bokeh_script=bokeh_script
)
@app.route('/favicon.png')
def favicon():
return flask.redirect(flask.url_for('static', filename='favicon.png'))
@app.route('/')
def main():
df = lldb.to_df()
return _main_page(df)
@app.route('/date/<date>')
def specific_date(date):
df = lldb.to_df()
date = pd.to_datetime(date)
df = df[df.index <= date]
return _main_page(
df, date=f'<h5>Status on {date.strftime(r"%b %d, %Y")}</h5>'
)
@app.route('/db')
def database_dump():
df = lldb.to_df()
return flask.send_file(
io.BytesIO(df.to_csv().encode('utf-8')),
mimetype='text/csv',
attachment_filename='madison_lake_levels.csv',
as_attachment=True
)
@app.route('/plot-year')
def old_plot_year():
return flask.redirect('/')
def plot_year():
df = lldb.to_df()
req_levels = mll.required_levels.required_levels
height = 450
tabs = []
for lake, color in zip(df.columns, palette):
p = figure(title=lake.title(),
x_axis_label=None,
x_axis_type='datetime',
y_axis_label='Lake Height (feet above sea level)',
tools=[],
toolbar_location=None,
height=height,
sizing_mode='stretch_width',
css_classes=['no-interaction'])
curr_year = df.index.year.max()
df = df[df.index.year >= curr_year - 8]
gb = df.copy().groupby(df.index.year)
curr_year_df = gb.get_group(curr_year)
curr_day_of_year = curr_year_df.index.dayofyear.max()
curr_day_of_year = min(curr_day_of_year, 365) # leap years don't exist
first_day_of_year = curr_year_df.index[0]
max_line = p.line([curr_year_df.index.min(), curr_year_df.index.max()],
2 * [req_levels.loc[lake, 'summer_maximum']],
color='#000000',
line_width=2,
line_dash=[5, 5],
line_alpha=0.5)
for year, idx in gb.groups.items():
year_df = df.loc[idx].copy()
year_df = year_df[year_df.index.dayofyear <= curr_day_of_year]
year_df.index = year_df.index.shift(
1, first_day_of_year - year_df.index[0]
)
line = p.line(year_df.index, year_df[lake],
color=color, line_width=2,
line_alpha=1 if year == curr_year else 0.3)
if year == curr_year:
this_year_line = line
else:
other_year_line = line
p.xaxis.formatter = DatetimeTickFormatter(
days=['%b %d'],
months=['%b'],
years=['%b'],
)
legend = Legend(
items=[
('This year', [this_year_line]),
('Past 7 years', [other_year_line]),
('State max', [max_line])
],
location=(0, 20)
)
p.add_layout(legend, 'below')
p.legend.orientation = "vertical"
tabs.append(Panel(child=p, title=lake.title()))
script, div = components(Tabs(tabs=tabs))
return script, div
@app.route('/plot-timeline')
def plot_timeline():
df = lldb.to_df()
req_levels = mll.required_levels.required_levels
height = 700
hover = HoverTool(
names=[lake.title() for lake in df],
tooltips=[('lake', '$name'), ('date', '$x{%F}'),
('height above sea level', '$y{0.00} ft')],
formatters={'$x': 'datetime'}
)
p = figure(title="Madison Lake Levels",
x_axis_label='date',
x_axis_type='datetime',
y_axis_label='Lake Height (feet above sea level)',
tools=["pan,wheel_zoom,box_zoom,reset,previewsave", hover],
height=height,
sizing_mode='stretch_width')
p.toolbar.logo = None
levels = []
maxes = []
for lake, color in zip(df.columns, palette):
levels.append(p.line(df.index, df[lake],
color=color, line_width=2, name=lake.title()))
maxes.append(p.line([df.index.min(), df.index.max()],
2 * [req_levels.loc[lake, 'summer_maximum']],
color=color,
line_width=2,
line_dash=[5, 5],
line_alpha=0.8))
_msg = p.circle([], [], color='#ffffff')
legend_items = [('Click to hide', [_msg])]
for lake, level, _max in zip(df.columns, levels, maxes):
lake = lake.title()
legend_items.extend([(lake, [level]), (lake + ' max', [_max])])
legend = Legend(items=legend_items, location=(0, 0))
legend.click_policy = 'hide'
p.add_layout(legend, 'left')
tab1 = Panel(child=p, title="Absolute levels")
hover = HoverTool(
names=[lake.title() for lake in df],
tooltips=[('lake', '$name'), ('date', '$x{%F}'),
('height vs. max', '$y{+0.00} ft')],
formatters={'$x': 'datetime'}
)
p = figure(title="Madison Lake Levels - difference from state max",
x_axis_label='date',
x_axis_type='datetime',
y_axis_label='Lake Height (feet above State Max)',
tools=["pan,wheel_zoom,box_zoom,reset,previewsave", hover],
height=height,
sizing_mode='stretch_width')
p.toolbar.logo = None
levels = []
for lake, color in zip(df.columns, palette):
levels.append(p.line(df.index,
df[lake] - req_levels.loc[lake, 'summer_maximum'],
name=lake.title(), color=color, line_width=2))
_msg = p.circle([], [], color='#ffffff')
legend_items = [('Click to hide', [_msg])]
legend_items.append((
'State Max',
[p.line([df.index.min(), df.index.max()],
[0, 0],
color='black',
line_dash=[5, 5])]
))
for lake, level in zip(df.columns, levels):
lake = lake.title()
legend_items.append((lake, [level]))
legend = Legend(items=legend_items, location=(0, 0))
legend.click_policy = 'hide'
p.add_layout(legend, 'left')
tab2 = Panel(child=p, title='Levels compared to state maximum')
tabs = Tabs(tabs=[tab1, tab2])
script, div = components(tabs)
return flask.render_template('plot.html', bokeh_script=script,
plot_div=div)
@app.route('/update/', defaults={'start': None, 'end': None},
methods=['GET', 'POST'])
@app.route('/update/<start>/<end>', methods=['GET', 'POST'])
def update_db(start, end):
if start is None:
try:
start_dt = lldb.most_recent().index[0].to_pydatetime()
except IndexError:
start_dt = dt.utcnow() - timedelta(days=1)
else:
start_dt = pd.to_datetime(start, utc=True).to_pydatetime()
if end is None:
end_dt = dt.utcnow()
else:
end_dt = pd.to_datetime(end, utc=True).to_pydatetime()
mll.scrape.backfill(start_dt, end_dt, lldb, True)
return flask.redirect('/')
if __name__ == '__main__':
app.run()