-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
313 lines (284 loc) · 8.94 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
from dotenv import load_dotenv
import os
from pathlib import Path
import numpy as np
import pandas as pd
import dash
import dash_auth
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from data_input import get_dashboard_data
from make_figures import make_table, make_revenue_chart, make_attendance_table
# load environment variables
username = os.environ.get('USERNAME')
password = os.environ.get('PASSWORD')
ga_tracking_id = os.environ.get('GA_TRACKING_ID')
# load data
df_dashboard, latest_date, is_data_insufficient, days_req_for_warnings = get_dashboard_data()
min_revenue_sum = df_dashboard['min_revenue'].sum()
potential_revenue_sum = df_dashboard['potential_revenue'].sum()
max_approved_revenue_sum = df_dashboard['max_revenue'].sum()
# figures
child_table = make_table(df_dashboard)
revenue_chart = make_revenue_chart(df_dashboard)
summary_table = make_attendance_table(df_dashboard)
# dash app
app = dash.Dash(__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = 'Dashboard - Pie for Providers'
auth = dash_auth.BasicAuth(
app,
{username: password}
)
server = app.server
# navbar
navbar = dbc.Navbar(
dbc.Container(
[
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(
html.Img(src='/assets/pie_logo.png', height='50px')
),
],
align='center',
no_gutters=True,
)
],
fluid=True
),
color="light"
)
# summary cards
attendance_summary_card = dbc.Card(
[
dbc.CardBody(
[ html.H3('Total Attendance',
style={'font-size': '1.5rem'}),
summary_table
]
)
],
className='h-100',
)
revenue_summary_card = dbc.Card(
[
dbc.CardBody(
[
html.H3('Total Revenue',
style={'font-size': '1.5rem'}),
revenue_chart
]
)
],
className='h-100',
)
# detail cards
attendance_copy_card = dbc.Card(
[
dbc.CardHeader(
html.H2(
dbc.Button(
'More details on attendance risk',
color='link',
id='toggle-1'
)
)
),
dbc.Collapse(
dbc.CardBody(
[
html.P(
[
html.Strong('Sure bet: '),
html.Span('maximum payment expected! Based on attendance rate for full and part days')
]
),
html.P(
[
html.Strong('On track: '),
html.Span('likely to meet attendance rate for full payment')
]
),
html.P(
[
html.Strong('At risk: '),
html.Span('may not meet attendance rate for full payment - encourage family to attend')
]
),
html.P(
[
html.Strong('Not met: '),
html.Span("full payment not possible; you'll get paid for days attended only")
]
),
html.P(
[
html.Strong('Not enough info: '),
html.Span('email us ' + str(days_req_for_warnings)
+ '+ days of attendance records to get projections')
]
)
]
),
id='collapse-1'
)
]
)
revenue_copy_card = dbc.Card(
[
dbc.CardHeader(
html.H2(
dbc.Button(
'More details on revenue',
color='link',
id='toggle-2'
)
)
),
dbc.Collapse(
dbc.CardBody(
[
html.P(
[
html.Strong('Guaranteed revenue: '),
html.Span('based on days already attended')
]
),
html.P(
[
html.Strong('Potential revenue: '),
html.Span('maximum possible, based on attendance to date')
]
),
html.P(
[
html.Strong('Max. approved revenue: '),
html.Span('if all families meet 50% attendance rate')
]
),
html.P(
[
html.Strong('Potential e-learning revenue: '),
html.Span('if approved part days become full days for e-learning')
]
),
]
),
id='collapse-2'
)
]
)
accordion = html.Div(
[attendance_copy_card, revenue_copy_card], className='accordion'
)
# email copy
email_copy = html.Div(
html.P(
[
'For the most accurate estimates, email all your attendance records for this month to ',
html.A(
href='mailto:[email protected]',
target="_blank"),
'. You can pull a report from software you already use, ',
html.A(
'or fill this spreadsheet.',
href='https://docs.google.com/spreadsheets/d/1OsNy6BgH09dDf2PlZ32qT7mgJMSmd6sRqxH1bpfccgI/edit#gid=246587558',
target="_blank"
)
]
)
)
app.layout = html.Div(
[
navbar,
dbc.Container(
[
html.H1(children='Your dashboard'),
html.H2('Estimates as of ' + latest_date,
style={'font-size': '1.5rem'}),
html.Div(
dbc.Alert('At-risk case warnings will be available with '
+ str(days_req_for_warnings)
+ ' days of attendance data',
color='warning',
is_open=is_data_insufficient)
),
# Summary statistics
html.Div(
[
dbc.Row(
[
dbc.Col(
attendance_summary_card,
width=4
),
dbc.Col(
revenue_summary_card,
width=8
)
],
no_gutters=True,
align='stretch'
),
]
),
accordion,
html.Br(),
# Child level table
html.Div(
child_table
),
email_copy
]
)
]
)
# callbacks
@app.callback(
[Output(f"collapse-{i}", "is_open") for i in range(1, 3)],
[Input(f"toggle-{i}", "n_clicks") for i in range(1, 3)],
[State(f"collapse-{i}", "is_open") for i in range(1, 3)],
)
def toggle_accordion(n1, n2, is_open1, is_open2):
ctx = dash.callback_context
if not ctx.triggered:
return False, False
else:
button_id = ctx.triggered[0]["prop_id"].split(".")[0]
if button_id == "toggle-1" and n1:
return not is_open1, False
elif button_id == "toggle-2" and n2:
return False, not is_open2
return False, False
app.index_string=f"""<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={ga_tracking_id}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{dataLayer.push(arguments);}}
gtag('js', new Date());
gtag('config', '{ga_tracking_id}');
</script>
{{%metas%}}
<title>{{%title%}}</title>
{{%favicon%}}
{{%css%}}
</head>
<body>
{{%app_entry%}}
<footer>
{{%config%}}
{{%scripts%}}
{{%renderer%}}
</footer>
</body>
</html>"""
if __name__ == '__main__':
app.run_server(debug=True)