-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.py
148 lines (123 loc) · 4.7 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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
################################################################################################################
# LOAD AND PROCESS DATA
df0 = pd.read_csv('./data/IHME_GBD_2017_HEALTH_SDG_1990_2030_SCALED_Y2018M11D08.CSV')
loc_meta = pd.read_csv('./data/location_metadata.csv')
# Indicator Value by country in wide format
df = df0.pivot(index='location_name', columns='indicator_short', values='scaled_value')
df = pd.merge(loc_meta, df.reset_index())
indicators = df0.indicator_short.unique().tolist()
indicator_key = df0.drop_duplicates('indicator_short').set_index('indicator_short')[
'ihme_indicator_description'].to_dict()
################################################################################################################
top_markdown_text = '''
### Dash Tutorial - Sustainable Development Goals
#### Zane Rankin, 2/17/2019
The [Institute for Health Metrics and Evaluation](http://www.healthdata.org/) publishes estimates for 41 health-related SDG indicators for
195 countries and territories.
I downloaded the [data](http://ghdx.healthdata.org/record/global-burden-disease-study-2017-gbd-2017-health-related-sustainable-development-goals-sdg)
for a tutorial on Medium and Github
**Indicators are scaled 0-100, with 0 being worst observed (e.g. highest mortality) and 100 being best.**
'''
app.layout = html.Div([
# HEADER
dcc.Markdown(children=top_markdown_text),
# LEFT - CHOROPLETH MAP
html.Div([
dcc.Dropdown(
id='x-varname',
options=[{'label': i, 'value': i} for i in indicators],
value='SDG Index'
),
dcc.Markdown(id='x-description'),
dcc.Graph(id='county-choropleth'),
dcc.Markdown('*Hover over map to select country for plots*'),
], style={'float': 'left', 'width': '39%'}),
# RIGHT - SCATTERPLOT
html.Div([
dcc.Dropdown(
id='y-varname',
options=[{'label': i, 'value': i} for i in indicators],
value='Under-5 Mort'
),
dcc.Markdown(id='y-description'),
dcc.Graph(id='scatterplot'),
], style={'float': 'right', 'width': '59%'}),
])
@app.callback(
Output('x-description', 'children'),
[Input('x-varname', 'value')])
def x_description(i):
return f'{indicator_key[i]}'
@app.callback(
Output('y-description', 'children'),
[Input('y-varname', 'value')])
def y_description(i):
return f'{indicator_key[i]}'
@app.callback(
Output('county-choropleth', 'figure'),
[Input('x-varname', 'value')])
def update_map(x_varname):
return dict(
data=[dict(
locations=df['ihme_loc_id'],
z=df[x_varname],
text=df['location_name'],
autocolorscale=False,
reversescale=True,
type='choropleth',
)],
layout=dict(
title=x_varname,
height=400,
margin={'l': 0, 'b': 0, 't': 40, 'r': 0},
geo=dict(showframe=False,
projection={'type': 'Mercator'}))
)
@app.callback(
Output('scatterplot', 'figure'),
[Input('x-varname', 'value'),
Input('y-varname', 'value'),
Input('county-choropleth', 'hoverData'),])
def update_graph(x_varname, y_varname, hoverData):
if hoverData is None: # Initialize before any hovering
location_name = 'Nigeria'
else:
location_name = hoverData['points'][0]['text']
# Make size of marker respond to map hover
df['size'] = 12
df.loc[df.location_name == location_name, 'size'] = 30
return {
'data': [
go.Scatter(
x=df[df['super_region_name'] == i][x_varname],
y=df[df['super_region_name'] == i][y_varname],
text=df[df['super_region_name'] == i]['location_name'],
mode='markers',
opacity=0.7,
marker={
'size': df[df['super_region_name'] == i]['size'],
'line': {'width': 0.5, 'color': 'white'}
},
name=i
) for i in df.super_region_name.unique()
],
'layout': go.Layout(
height=400,
xaxis={'title': x_varname},
yaxis={'title': y_varname},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
# legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(debug=True)