-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM_user_data.py
277 lines (253 loc) · 9.06 KB
/
LM_user_data.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 4 11:12:43 2021
@author: cgwork
"""
# Dataframes, DBs
import os
import dash_bootstrap_components as dbc
import pandas as pd
# Dashboards modules
from dash import dcc, html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from app import app
user_data_local = [""] * 8
# All the code for data filtering, processing, done in jupyterlab
# notebooks (already in github), but now we can bypass all the processing
# and go straight to the final SQLite3 DB
datapath = os.path.join(os.getcwd(), "resources", "dbs")
df = pd.read_sql_table(
"Deadline_database",
"sqlite:///" + os.path.join(datapath, "deadline_database_nonans_geo.db"),
index_col="Country",
)
# df.dropna(inplace=True)
df.sort_values(by=["Year"], inplace=True)
# problem is in some dbs, like nonans_geo, we have 600 years of data
# leading to nulls everywhere except the last 15 years or so for most cols
df = df[df["Year"] >= 2000]
countries = list(df.index.unique())
countries.sort()
country_options = [{"label": str(val), "value": str(val)} for val in countries]
# app.logger.info(country_options)
dropdown_style = {
"marginLeft": "20px",
"marginRight": "50px",
"color": "#ffffff",
"backgroundColor": "#000000",
}
# layout
layout = html.Form(
html.Div(
style={
"fontFamily": "Sawasdee",
"fontSize": 22,
"color": "#ffffff",
"backgroundColor": "#111111",
},
children=[
html.H1(style={"textAlign": "left"}, children=""),
# header
html.Br(),
html.P(
style={
"textAlign": "left",
"fontSize": 32,
"marginLeft": "20px",
},
children="Please fill-in the data:",
),
# input area frame
html.Div(
id="input area",
style={
"margin": "0 auto",
"width": "50%",
# #"backgroundColor": "#99d6ff",
"padding": "15px",
},
# child of input area frame | input fields
children=[
dcc.Input(
id="user_name",
style={
"fontSize": 22,
# "marginLeft": "20px",
"marginRight": "50px",
# "backgroundColor" : "#000000",
# "color" : "#ffffff"
},
value=None,
type="text",
inputMode="latin-name",
placeholder="Name",
),
# user age, should be int
html.Br(),
html.Br(),
dcc.Input(
id="user_age",
style={
"fontSize": 22,
# "marginLeft": "20px",
"marginRight": "50px",
# "backgroundColor" : "#000000",
# "color" : "#ffffff"
},
type="number",
min=0,
max=120,
step=1,
inputMode="numeric",
value=None,
placeholder="Age",
),
# period frequency, float?
html.Br(),
html.Br(),
dcc.Dropdown(
id="birthplace",
options=country_options,
value=None,
placeholder="Birthplace (Country)",
),
# period frequency, float?
html.Br(),
dcc.Dropdown(
id="residence",
options=country_options,
value=None,
placeholder="Country of Residence",
),
html.Br(),
dcc.Dropdown(
id="sex",
value=None,
options=[
{"label": "M", "value": "M"},
{"label": "F", "value": "F"},
],
placeholder="Biological Sex",
),
html.Br(),
dcc.Dropdown(
id="veggie",
value=None,
options=[
{"label": "Y", "value": "Y"},
{"label": "N", "value": "N"},
],
placeholder="Are you a vegetarian?",
),
html.Br(),
dcc.Dropdown(
id="driver",
value=None,
options=[
{"label": "Y", "value": "Y"},
{"label": "N", "value": "N"},
],
placeholder="Do you drive a car?",
),
html.Br(),
dcc.Dropdown(
id="smoker",
value=None,
options=[
{"label": "Y", "value": "Y"},
{"label": "N", "value": "N"},
],
placeholder="Are you a Smoker?",
),
],
),
html.Br(),
html.Div(id="my-output", style={"color":"red", "textAlign":"center"}),
html.Div(
dbc.Button(
id="submit-button-state",
style={
"fontSize": 22,
"marginLeft": "20px",
"marginRight": "80px",
"backgroundColor": "#111",
"color": "#ffffff",
},
n_clicks=0,
children="Submit",
color="Primary",
className="me-1",
href="/page1"
),
className="d-grip gap-2 d-md-flex justify-content-md-end",
),
html.Div(id="output_graph"),
html.Div(id="output_text", style={"textAlign": "center", "color": "blue"}),
],
)
)
# Narrow options on dropdown
@app.callback(Output("birthplace", "options"), Input("birthplace", "search_value"))
def update_options_b(search_value):
if not search_value:
raise PreventUpdate
return [o for o in country_options if search_value.lower() in o["label"].lower()]
@app.callback(Output("residence", "options"), Input("residence", "search_value"))
def update_options_r(search_value):
if not search_value:
raise PreventUpdate
return [o for o in country_options if search_value.lower() in o["label"].lower()]
# every selection change will update our dccstore.
# @todo@ right now some fields can be optional.
@app.callback(
Output("dccstore_user", "data"),
[
Input(component_id="user_name", component_property="value"),
Input(component_id="user_age", component_property="value"),
Input(component_id="birthplace", component_property="value"),
Input(component_id="residence", component_property="value"),
Input(component_id="sex", component_property="value"),
Input(component_id="veggie", component_property="value"),
Input(component_id="driver", component_property="value"),
Input(component_id="smoker", component_property="value"),
],
)
def sel_user_data(
user_name, user_age, birthplace, residence, sex, veggie, driver, smoker
):
if (
user_name is None
or user_age is None
or birthplace is None
or residence is None
or sex is None
or veggie is None
or driver is None
or smoker is None
):
raise PreventUpdate
user_data_local[0] = user_name
user_data_local[1] = user_age
user_data_local[2] = birthplace
user_data_local[3] = residence
user_data_local[4] = sex
user_data_local[5] = veggie
user_data_local[6] = driver
user_data_local[7] = smoker
# app.logger.info(user_data_local)
return user_data_local
# @app.callback(
# Output(component_id="my-output", component_property="children"),
# [
# Input(component_id="submit-button-state", component_property="n_clicks"),
# ],
# )
# def submit_user_details(n_clicks):
# # if n_clicks == 0:
# # raise PreventUpdate
# if '' in user_data_local:
# return "Please make sure all fields have been keyed in."
# else:
# return dcc.Location(pathname="/page5", id="my-output")