-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
227 lines (209 loc) · 8.41 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
"""Test app for higher ed donation visualization."""
import json
import re
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output
from gen_dataframe import (get_donations_df, get_donationsMAP_df,
get_donationsTS_df)
from gen_figures import (get_donation_bar, get_donation_map,
get_donationTS_scatter)
# Global scope variables
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.COSMO])
server = app.server
db_path = 'sqlite:///merged_data_w_coord.sqlite'
with open('./school_abbrev.json', 'r') as f:
school_abbrev = json.load(f)
school_abbrev = pd.DataFrame(
school_abbrev.items(),
columns=['School', 'Abbrev'])
# Default data when app first load
overall_donations_df = get_donations_df(db_path)
donationTS_df = get_donationsTS_df(db_path, 'Carnegie Mellon University')
donation_map_df = get_donationsMAP_df(db_path, 'Carnegie Mellon University')
donation_bar = get_donation_bar(overall_donations_df)
donationTS_scatter = get_donationTS_scatter(donationTS_df,
'Carnegie Mellon University',
'cmu')
donation_map = get_donation_map(donation_map_df,
'Carnegie Melon University',
)
# Dashboard layout
app.layout = html.Div(
children=[
html.Div(
dbc.Navbar(
[
html.A(
dbc.Row(
dbc.Col(
dbc.NavbarBrand(
'Foreign Donations to US Universities',
className='display-3'
)
),
align='center',
no_gutters=True,
),
href='#',
),
dbc.NavbarToggler(id='navbar-toggler'),
dbc.Collapse(
dbc.Row(
[
dbc.Col(
dcc.Dropdown(
id='school-dropdown',
options=[{'label': school,
'value': abbrev}
for school, abbrev
in school_abbrev
.set_index('School')
.sort_index()['Abbrev']
.items()],
value='cmu',
style={'width': '30em'}
),
width={'offset': 11}
),
dbc.Col(
dbc.NavLink(
"About",
href="#",
className='ml-2'
),
width={'offset': 12}
),
],
no_gutters=True,
className='ml-auto, flex-nowrap mt-3 mt-md-0',
align='center',
),
id='navbar-collapse',
navbar=True,
),
],
className='navbar navbar-expand-lg navbar-light bg-light',
),
),
html.Div(
dbc.Jumbotron(
dcc.Markdown(
"""
This is a visualization tool for the disclosed foreign
donations received by US universities. Between 2012 and
2019. Choose or type a school name from the drop-down menu
see see donation received trends and sources. "Score" on
the map refers to the [Democracy Index]
(https://www.eiu.com/topic/democracy-index) as published
by [The Economist](https://www.economist.com/) in 2019. You
may also select a school from the total received foreign
donations by school bar chart at the bottom
(drag to zoom.). You may also isolate donations from a
particular country by double-clicking on the country at
the legend of the donation trend graph. The source of the
donation data is from the
[US Department of Education](https://catalog.data.gov/dataset/foreign-gifts-and-contracts-report-2011).
""",
# className='lead'
),
className='jumbotron'
),
),
html.Div(
children=[
html.Div(
dcc.Graph(
id='donation-map',
figure=donation_map,
clickData={
'points': [{'y': 'Carnegie Mellon University'}]
}
),
className='one-half column'
),
html.Div(
dcc.Graph(
id='donationTS',
figure=donationTS_scatter
),
className='one-half column'
),
],
className='row'
),
html.Div(
dcc.Graph(
id='master-bar-graph',
figure=donation_bar,
clickData={'points': [{'y': 'Carnegie Mellon University'}]},
selectedData={'points': [{'y': 'Carnegie Mellon University'}]},
),
# className='row',
),
html.Div(
html.Pre(
id='debug',
style={
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
)
)
]
)
@ app.callback(
[
Output('donation-map', 'figure'),
Output('donationTS', 'figure'),
],
[Input('master-bar-graph', 'clickData')]
)
def update_upper_figures(clickData):
school_re = re.compile(r'[^\"]')
school = ''.join(
school_re.findall(json.dumps(clickData['points'][0]['y']))
)
abbrev = school_abbrev.query(f'School == "{school}"')['Abbrev'].values[0]
new_donationsMap_df = get_donationsMAP_df(db_path, school)
new_donationTS_df = get_donationsTS_df(db_path, school)
figureTS = get_donationTS_scatter(new_donationTS_df, school, abbrev)
figure_map = get_donation_map(new_donationsMap_df, school)
return figure_map, figureTS
@ app.callback(
[
Output('master-bar-graph', 'figure'),
Output('master-bar-graph', 'clickData'),
],
[Input('school-dropdown', 'value')]
)
def drop_down_callbacks(value):
school = school_abbrev.query(f'Abbrev == "{value}"')['School'].values[0]
clickData = {'points': [{'y': school}]}
df = get_donations_df(db_path)
school_idx = df[df['School'] == school].index[0]
fig = get_donation_bar(df, school_idx)
return fig, clickData
@ app.callback(
Output('school-dropdown', 'value'),
[Input('master-bar-graph', 'selectedData')]
)
def update_dropdown(selectedData):
school_re = re.compile(r'[^\"]')
school = ''.join(
school_re.findall(json.dumps(selectedData['points'][0]['y']))
)
value = school_abbrev.query(f'School == "{school}"')['Abbrev'].values[0]
return value
# @app.callback(
# Output('debug', 'children'),
# [Input('master-bar-graph', 'selectedData')]
# )
# def selected_data_debug(selectedData):
# return json.dumps(selectedData)
if __name__ == '__main__':
app.run_server(debug=True)