-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·324 lines (273 loc) · 9.47 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
314
315
316
317
318
319
320
321
322
323
324
import dash_bootstrap_components as dbc
import diskcache
import pandas as pd
import json
import os
from dash import dcc, html, Dash, Input, Output, State, page_container, page_registry, no_update
from dash.long_callback import DiskcacheLongCallbackManager
from flask_login import logout_user, current_user
from auth import AUTH_ENABLED
from queries import QueryService
from server import server
from utils import get_login_path, get_prefixed_path
cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(cache)
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
app = Dash(
__name__,
server=server,
external_stylesheets=[dbc.themes.QUARTZ, dbc_css],
long_callback_manager=long_callback_manager,
suppress_callback_exceptions=True,
use_pages=True,
)
HEADER_STYLE = {
"display": "flex",
"flexDirection": "column",
"alignItems": "end",
"marginTop": "2rem",
"marginRight": "4rem",
"fontSize": "20px",
}
SIDEBAR_STYLE = {
"position": "fixed",
"top": 10,
"left": 0,
"bottom": 0,
"width": "14rem",
"padding": "2rem 1rem",
}
CONTENT_STYLE = {
"marginLeft": "14rem",
"marginRight": "2rem",
"padding": "2rem 1rem",
}
db = QueryService.get_instance()
header = html.Div(
[
html.Div(id="user-status-header", children=[
html.A(
children="logout",
n_clicks=0,
id="logout-button",
style={"display": "none"}
)
])
],
style=HEADER_STYLE
)
def get_relative_path(page_name):
return page_registry[f"pages.{page_name}"]["relative_path"]
sidebar = html.Div(
[
dbc.Row(
[
html.Img(
src=app.get_asset_url("rpf_logo.png"), style={"height": "100"}
)
]
),
html.Hr(),
dbc.Select(
id="country-select",
size="sm",
),
html.Hr(),
dbc.Nav(
[
dbc.NavLink("Overview", href=get_relative_path("home"), active="exact"),
dbc.NavLink(
"Education", href=get_relative_path("education"), active="exact"
),
dbc.NavLink("Health", href=get_relative_path("health"), active="exact"),
dbc.NavLink("About", href=get_relative_path("about"), active="exact"),
],
vertical=True,
pills=True,
),
],
style=SIDEBAR_STYLE,
)
content = html.Div(page_container, id="page-content", style=CONTENT_STYLE)
dummy_div = html.Div(id="div-for-redirect")
def layout():
html_contents = [
dcc.Location(id="url", refresh=False),
header,
sidebar,
content,
dummy_div
]
if not AUTH_ENABLED or current_user.is_authenticated:
html_contents.extend([
dcc.Store(id="stored-data"),
dcc.Store(id="stored-basic-country-data"),
dcc.Store(id="stored-data-subnational"),
dcc.Store(id="stored-data-func-econ"),
])
return (
html.Div(
html_contents
)
)
app.layout = layout
@app.callback(
[Output("url", "pathname"), Output("page-content", "children")],
[Input("url", "pathname"), Input("logout-button", "n_clicks")]
)
def display_page_or_redirect(pathname, logout_clicks):
login_path = get_login_path()
if logout_clicks:
logout_user()
return login_path, page_container
if not AUTH_ENABLED or current_user.is_authenticated:
if (
pathname == get_login_path() or
pathname is None or
pathname == os.getenv("DEFAULT_ROOT_PATH", "/")
):
return get_prefixed_path("home"), page_container
return pathname, page_container
else:
if pathname != login_path:
return login_path, page_container
return pathname, page_container
@app.callback(
Output("logout-button", "style"),
Input("url", "pathname")
)
def update_logout_button_visibility(pathname):
if AUTH_ENABLED and current_user.is_authenticated:
return {"display": "block", "text-decoration": "underline", "cursor": "pointer"}
else:
return {"display": "none"}
@app.callback(Output("stored-data", "data"), Input("stored-data", "data"))
def fetch_data_once(data):
if data is None:
df = db.get_expenditure_w_poverty_by_country_year()
countries = sorted(df["country_name"].unique())
return {
"countries": countries,
"expenditure_w_poverty_by_country_year": df.to_dict("records"),
}
return no_update
@app.callback(Output("stored-data-func-econ", "data"), Input("stored-data-func-econ", "data"))
def fetch_func_data_once(data):
if data is None:
func_econ_df = db.get_expenditure_by_country_func_econ_year()
agg_dict = {
"expenditure": "sum",
"real_expenditure": "sum",
"decentralized_expenditure": "sum",
"central_expenditure": "sum",
"per_capita_expenditure": "sum",
"per_capita_real_expenditure": "sum",
}
func_df = (
func_econ_df
.groupby(["country_name", "year", "func"], as_index=False)
.agg(agg_dict)
)
func_df["expenditure_decentralization"] = func_df["decentralized_expenditure"] / func_df["expenditure"]
econ_df = (
func_econ_df
.groupby(["country_name", "year", "econ"], as_index=False)
.agg(agg_dict)
)
econ_df["expenditure_decentralization"] = econ_df["decentralized_expenditure"] / econ_df["expenditure"]
return {
"expenditure_by_country_func_econ_year": func_econ_df.to_dict("records"),
"expenditure_by_country_func_year": func_df.to_dict("records"),
"expenditure_by_country_econ_year": econ_df.to_dict("records"),
}
return no_update
@app.callback(
Output("stored-data-subnational", "data"),
Input("stored-data-subnational", "data"),
Input("stored-data", "data"),
)
def fetch_subnational_data_once(data, country_data):
if data is None:
countries = country_data["countries"]
df = db.get_adm_boundaries(countries)
boundaries_geojson = {
"type": "FeatureCollection",
"features": [
{
"properties": {"country": x[0], "region": x[1]},
"geometry": json.loads(x[2]),
}
for x in zip(df.country_name, df.admin1_region, df.boundary)
],
}
subnational_poverty_df = db.get_subnational_poverty_index(countries)
geo1_year_df = db.get_expenditure_by_country_geo1_year()
return {
"subnational_poverty_index": subnational_poverty_df.to_dict("records"),
"boundaries": boundaries_geojson,
"expenditure_by_country_geo1_year": geo1_year_df.to_dict("records"),
}
return no_update
@app.callback(
Output("country-select", "options"),
Output("country-select", "value"),
Input("stored-data", "data"),
)
def display_data(data):
def get_country_select_options(countries):
options = list({"label": c, "value": c} for c in countries)
options[0]["selected"] = True
return options
if data is not None:
countries = data["countries"]
return get_country_select_options(countries), countries[0]
return ["No data available"], ""
@app.callback(
Output("stored-basic-country-data", "data"),
Input("country-select", "options"),
Input("stored-data-subnational", "data"),
Input("stored-basic-country-data", "data"),
)
def fetch_country_data_once(countries, subnational_data, country_data):
if country_data is None:
countries = [x["label"] for x in countries]
country_df = db.get_basic_country_data(countries)
country_info = country_df.set_index("country_name").T.to_dict()
expenditure_df = pd.DataFrame(
subnational_data["expenditure_by_country_geo1_year"],
columns=["country_name", "year"],
)
poverty_df = pd.DataFrame(
subnational_data["subnational_poverty_index"],
columns=["country_name", "year", "poor215"],
)
expenditure_years = (
expenditure_df.groupby("country_name")["year"]
.apply(lambda x: sorted(x.unique()))
.to_dict()
)
poverty_years = (
poverty_df.groupby("country_name")["year"]
.apply(lambda x: sorted(x.unique()))
.to_dict()
)
poverty_level_stats = (
pd.merge(country_df, poverty_df, on="country_name")
.groupby("income_level")["poor215"]
.agg(["min", "max"])
.reset_index()
)
poverty_level_stats = (
poverty_level_stats.set_index("income_level").apply(tuple, axis=1).to_dict()
)
for country, years in expenditure_years.items():
country_info[country]["expenditure_years"] = years
for country, years in poverty_years.items():
country_info[country]["poverty_years"] = years
for country, info in country_info.items():
country_income_level = info["income_level"]
info["poverty_bounds"] = poverty_level_stats[country_income_level]
return {"basic_country_info": country_info}
return no_update
if __name__ == "__main__":
app.run_server(debug=True)