-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
287 lines (263 loc) · 10.9 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
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from styles import style
import dash_leaflet as dl
import dash_leaflet.express as dlx
business_univariate_df = pd.read_csv('./data/business_univariate_stats.csv')
workers_univariate_df = pd.read_csv('./data/workers_univariate_stats.csv')
business_bivariate_df = pd.read_csv('./data/business_bivariate_stats.csv')
workers_bivariate_df = pd.read_csv('./data/workers_bivariate_stats.csv')
business_variables_map = pd.read_excel('./data/generated_business_variable_map.xlsx')
business_variables_map.loc[business_variables_map['group']=='general', 'queue_index'] = [37, 38, 39]
business_variables_map.sort_values('queue_index', inplace=True)
workers_variables_map = pd.read_excel('./data/generated_workers_variable_map.xlsx')
maps_df = pd.read_csv('./data/map_visualization_data.csv')
maps_variable = ()
groups = business_univariate_df['variablegroup'].unique()[:-1]
all_options = {
'workforce': ['None'] + workers_variables_map[workers_variables_map['group']=='general']['variable'].values.tolist(),
'business': ['None'] + business_variables_map[business_variables_map['group']=='general']['variable'].values.tolist()
}
filter_dict = {
'm_gender': 'Gender',
'm_age': 'Age',
'm_edu_levl': 'Education',
'm_years_of_experience': 'Experience',
'm_biz_years_in_operation': 'Years in operation',
'm_biz_type': 'Type',
'b_n_emplyes_pre_covid': 'No of employees',
'None': 'None'
}
config = {'displayModeBar': False}
# external_stylesheets = ['./style.css']
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True)
server = app.server
app.layout = dbc.Col(html.Div([
dcc.Location(id='url', refresh=False),
html.Div([html.Div('C2M2',
style=style['c2m2']),
html.Div('KATHMANDU',
style=style['kathmandu']),
html.Div([dcc.Link('Charts', href='/charts', style=style['url']), dcc.Link('Maps', href='/maps', style=style['url'])], style=style['link'])],
style=style['header']),
html.Br(),
html.Div(id='page-content')
]))
# Update the index
@app.callback(dash.dependencies.Output('page-content', 'children'),
[dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/charts':
return charts_layout
elif pathname == '/maps':
return maps_layout
charts_layout = html.Div([html.Div([
dcc.Tabs(id='dropdown-survey', value='business', children=[
dcc.Tab(label='Business', value='business', style=style['tab-1']),
dcc.Tab(label='Workforce', value='workforce', style=style['tab-2']),
], style= style['tabs'])
]),
dbc.Col([
dbc.Row([
dbc.Col([
dbc.Label("Explore"),
dcc.Dropdown(
id="dropdown-research",
options=[{"label": x.upper(), "value": x} for x in groups],
value=groups[0],
clearable=False
)
], md=3),
dbc.Col([
dbc.Label("By"),
dcc.Dropdown(
id="dropdown-filter",
options=[{"label": x.upper(), "value": x} for x in groups],
value=groups[0],
clearable=False
)
], md=3)
],
style= {'marginTop':'50px'}),
html.Hr(),
html.Br(),
html.Div(id='charts')])])
map_variables = maps_df['variable'].unique().tolist()
@app.callback(
Output('map-label', 'options'),
Input('map-variable', 'value'))
def update_label(variable):
return([{'label':i, 'value':i} for i in maps_df[maps_df['variable']==variable]['label_en'].unique().tolist()])
@app.callback(
Output('map-label', 'value'),
Input('map-label', 'options'))
def update_value(options):
return options[0]['value']
controls = dbc.Card(
[
dbc.FormGroup(
[
dbc.Label("X variable"),
dcc.Dropdown(
id="map-variable",
options=[{"label": col, "value": col} for col in map_variables],
value= map_variables[0],
),
]
),
dbc.FormGroup(
[
dbc.Label("Y variable"),
dcc.Dropdown(
id="map-label",
),
]
),
],
body=True,
style={'height': '85vh'}
)
@app.callback(
Output('maps', 'children'),
Input('map-label', 'value'))
def update_maps(label):
maps_df_unique = maps_df[maps_df['label_en']==label].drop_duplicates(subset='coordinates')
map = dl.Map([
dl.TileLayer(),
dl.GeoJSON(data=dlx.dicts_to_geojson([dict(lat=lat, lon=lon) for lat, lon in maps_df_unique[['latitude', 'longitude']].values.tolist()]),
cluster=True, zoomToBoundsOnClick=True, superClusterOptions={"radius": 100}),
], center=[28.5, 84], zoom=7, minZoom=7, style={'width': '100%', 'height': '85vh', 'margin': "auto", "display": "block"})
return map
maps_layout = html.Div([dbc.Row([
controls,
dbc.Col(id='maps', md=9)])], style=style['tabs'])
@app.callback(
Output('dropdown-filter', 'options'),
Input('dropdown-survey', 'value'))
def set_cities_options(selected_country):
return [{'label': filter_dict[i], 'value': i} for i in all_options[selected_country]]
@app.callback(
Output('dropdown-filter', 'value'),
Input('dropdown-filter', 'options'))
def set_cities_value(available_options):
return available_options[0]['value']
@app.callback(
Output("charts", "children"),
[ Input("dropdown-survey", "value"),
Input("dropdown-research", "value"),
Input("dropdown-filter", "value")
])
def update_bar_chart(survey, group, filter):
if survey=='business':
if filter == "None":
df = business_univariate_df
else:
df = business_bivariate_df
labels_map = business_variables_map
elif survey=='workforce':
if filter == "None":
df = workers_univariate_df
else:
df = workers_bivariate_df
labels_map = workers_variables_map
group_df = df[df["variablegroup"]==group.lower()]
if filter != 'None':
group_df = df[df["variablegroup"]==group.lower()]
group_df = group_df[group_df['xvariable']== filter]
labels_df = labels_map[labels_map['group']==group.lower()]
subplot_titles=labels_map[labels_map['group']==group.lower()]['ques_en'].values
subgroup = labels_map[labels_map['group']==group.lower()]['subGroups'].values
plot_titles = labels_map[labels_map['group']==group.lower()]['highlights'].values
variables = labels_df['variable'].unique()
if filter != 'None':
output=[]
for idx , i in enumerate(variables):
label_cond=list(labels_map[labels_map['variable']==i]['askedCondition'])[0]
label_total=list(labels_map[labels_map['variable']==i]['askedTotal'])[0]
if label_cond =='general':
total = 'showing '+str(label_total)+ ' responses'
else:
total = 'showing '+str(label_total) + ' responses of ' + label_cond
data = group_df[group_df['yvariable']==i]
fig = px.bar(
x=data['total'],
y=data['ylabel_en'],
color=data['xlabel_en'],
)
fig.update_xaxes(title_text=total, autorange=False, range=[0,100])
fig.update_yaxes(ticklabelposition='inside', color='white')
fig.update_layout(
plot_bgcolor = "rgb(250, 250, 250)",
title = subplot_titles[idx],
height=(600),
width=830,
)
output.append(
html.Div(
[dbc.Col([
dbc.Row([
dbc.Col([
html.H5(subgroup[idx],style={'color':'rgb(189, 34, 34)', 'marginTop':'50px', 'fontFamily': 'Benne'} ),
html.H3(plot_titles[idx], style={'color':'rgb(189, 34, 34)', 'fontWeight':'Bold', 'fontFamily': 'Dosis'})
],style={'backgroundColor': 'rgb(235, 235, 235)'} ,md=4),
dbc.Col(
dcc.Graph(id=i,figure =fig,config=config), md=8)
])
], style={'boxShadow': '0px 1px 5px 1px #ccc'}),
html.Br(),
html.Br(),
html.Br()
], style={'marginLeft': '80px', 'marginRight':'80px'}
)
)
else:
output = []
for idx, i in enumerate(variables):
label_cond=list(labels_df[labels_df['variable']==i]['askedCondition'])[0]
label_total=list(labels_map[labels_map['variable']==i]['askedTotal'])[0]
if label_cond =='general':
total = 'showing '+str(label_total)+ ' responses'
else:
total = 'showing '+str(label_total) + ' responses of ' + label_cond
data = group_df[group_df['variable']==i]
fig = px.bar(
x=data['percoftotal']*100,
y=data['label_en'],
color_discrete_sequence = ['rgb(189, 80, 80)']*len(data))
fig.update_xaxes(title_text=total, autorange=False, range=[0,100])
fig.update_yaxes(ticklabelposition='inside', color='white')
fig.update_layout(
plot_bgcolor = "rgb(250, 250, 250)",
title = subplot_titles[idx],
height=(600),
width=830,
)
output.append(
html.Div(
[dbc.Col([
dbc.Row([
dbc.Col([
html.H5(subgroup[idx],style={'color':'rgb(189, 34, 34)', 'marginTop':'50px', 'fontFamily': 'Benne'} ),
html.H3(plot_titles[idx], style={'color':'rgb(189, 34, 34)', 'fontWeight':'Bold', 'fontFamily': 'Dosis'})
],style={'backgroundColor': 'rgb(235, 235, 235)'} ,md=4),
dbc.Col(
dcc.Graph(id=i,figure =fig,config=config), md=8)
])
], style={'boxShadow': '0px 1px 5px 1px #ccc'}),
html.Br(),
html.Br(),
html.Br()
], style={'marginLeft': '80px', 'marginRight':'80px'}
)
)
return output
if __name__ == '__main__':
app.run_server(debug=True)