-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.py
205 lines (190 loc) · 7.6 KB
/
app_test.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
import pandas as pd
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
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')
groups = business_univariate_df['variable_group'].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])
server = app.server
app.layout = dbc.Col(html.Div([
html.Div([html.Div('C2M2',
style=style['c2m2']),
html.Div('KATHMANDU',
style=style['kathmandu']),
],
style=style['header']),
html.Br(),
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.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(),
dcc.Graph(id="bar-chart", style={'marginLeft': '10px'}, config=config),
]),md='12')
@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("bar-chart", "figure"),
[ 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["variable_group"]==group.lower()]
if filter != 'None':
group_df = df[df["variable_group"]==group.capitalize()]
group_df = group_df[group_df['x_variable']== filter]
labels_df = labels_map[labels_map['group']==group.lower()]
subplot_titles=labels_map[labels_map['group']==group.lower()]['ques__en'].values
variables = labels_df['variable'].unique()
fig = make_subplots(rows=len(variables), cols=1,
subplot_titles=subplot_titles, vertical_spacing = 0.2/len(variables))
if filter != 'None':
data = group_df[group_df['y_variable']=='i_covid_effect_business']
fig = px.bar(
x=data['total'],
y=data['y_label__en'],
color=data['x_label__en'])
fig.update_layout(
height=(600),
width=1200,
)
# for idx , i in enumerate(variables):
# label_cond=list(labels_map[labels_map['variable']==i]['asked_condition'])[0]
# label_total=list(labels_map[labels_map['variable']==i]['asked_total'])[0]
# if label_cond =='general':
# total = 'showing '+str(label_total)+ ' responses'
# else:
# total = 'showing '+str(label_total) + ' responses of ' + label_cond
# mask = group_df[group_df['y_variable']==i]
# dt = {}
# for i in mask['x_label__en'].unique():
# dt[i] = mask[mask['x_label__en']==i]['total'].values.tolist()
# dt['label'] = mask['y_label__en'].unique().tolist()
# data = dt,
# traces = []
# base = [0,0,0,0,0]
# for key in list(data[0].keys())[:-1]:
# traces.append(go.Bar(
# name = key,
# x = data[0][key],
# y = data[0]['label'],
# offsetgroup = 1,
# base=base,
# orientation='h',
# showlegend=True
# ))
# base = [val1+val2 for val1, val2 in zip(base, data[0][key])]
# figure = go.Figure(
# data=traces,
# layout=go.Layout(
# title="Issue Types - Original and Models",
# yaxis_title="Number of Issues"
# )
# )
# for trc in figure['data']:
# fig.add_trace(trc, row=idx+1, col=1)
# fig.update_xaxes(title_text=total, row=idx+1, col=1)
# fig.update_yaxes(ticklabelposition='inside')
# fig.update_layout(
# height=(600* len(variables)),
# width=1200,
# )
else:
for idx, i in enumerate(variables):
label_cond=list(labels_map[labels_map['variable']==i]['asked_condition'])[0]
label_total=list(labels_map[labels_map['variable']==i]['asked_total'])[0]
if label_cond =='general':
total = 'showing '+str(label_total)+ ' responses'
else:
total = 'showing '+str(label_total) + ' responses of ' + label_cond
mask = group_df['variable']==i
fig.add_trace(go.Bar(
x=group_df[mask]["total"],
y=group_df[mask]["label__en"],
orientation='h',
marker_color = 'rgba(79, 167, 159, 0.726)',
), row=idx+1, col=1)
fig.update_xaxes(title_text=total, row=idx+1, col=1)
fig.update_yaxes(ticklabelposition='inside')
fig.update_layout(
height=(600* len(variables)),
width=1200,
showlegend=False,
)
return fig
if __name__ == '__main__':
app.run_server(debug=True)