-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM_line_datafields.py
227 lines (197 loc) · 5.53 KB
/
LM_line_datafields.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 4 22:17:25 2021
@author: cgwork
"""
import os
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from dash import dcc, html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from app import app
# import numpy as np
colorscales = px.colors.named_colorscales()
# 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]
# Year/range slider
year_min = df["Year"].min()
year_max = df["Year"].max()
year_slider = dcc.RangeSlider(
id="year-slider",
min=year_min,
max=year_max,
value=[year_min, year_max],
marks={i: str(i) for i in range(year_min, year_max + 1, 10)},
)
dropdown = dcc.Dropdown(
# style= dropdown_style,
id="countries",
options=[{"label": str(val), "value": str(val)} for val in countries],
multi=True,
value=tuple(),
placeholder="Countries",
style={
"fontSize": 14,
# "width" : "70%",
"horizontalAlign": "middle",
"verticalAlign": "middle",
},
)
data_picker = dcc.Dropdown(
id="data-picker",
options=[
{
"label": str(val).replace("_", " ").title(),
"value": val,
}
for val in df.columns[2 : len(df.columns) - 3]
],
multi=False,
value=df.columns[2],
placeholder="Statistic",
style={
"fontSize": 14,
# "width" : "70%",
"horizontalAlign": "middle",
"verticalAlign": "middle",
},
)
button = dbc.Button(
id="next-button-state",
style={
"fontSize": 18,
"marginLeft": "20px",
"marginRight": "80px",
"backgroundColor": "#111",
"color": "#ffffff",
},
n_clicks=0,
children="Next",
color="Primary",
className="me-1",
href="/page3",
)
scatter_graph = dcc.Graph(
id="line_datafields", config={"displayModeBar": True, "displaylogo": False}
)
# Layout
scatter_layout = go.Layout(
xaxis={
# "type": "log",
# "title": "Year",
"gridcolor": "#181818",
"zerolinecolor": "#181818",
},
yaxis={
# "title": "Life Expectancy",
"gridcolor": "#181818",
"zerolinecolor": "#181818",
},
margin={"l": 60, "b": 60, "t": 60, "r": 60},
legend={"x": 0, "y": 1},
hovermode="closest",
plot_bgcolor="#111111",
paper_bgcolor="#111111",
font_family="Sawasdee",
font_color="#ffffff",
template="plotly_dark",
)
# Create the app layout
layout = html.Div(
style={
"fontFamily": "Sawasdee",
"fontSize": 22,
"backgroundColor": "#111111",
},
children=[
html.Div(
children=[
html.Div(
[
html.Div(
[
dropdown,
],
style={"padding": 10, "flex": 1, "width": "50%"},
),
html.Div(
[
data_picker,
],
style={"padding": 10, "flex": 1, "width": "50%"},
),
],
style={"display": "flex"},
),
html.Div(
[
html.Br(),
scatter_graph,
html.Br(),
]
),
html.Div(
[
year_slider,
],
style={"padding": 10, "flex": 1},
),
html.Br(),
html.Div(
[
button,
],
className="d-grip gap-2 d-md-flex justify-content-md-end",
),
],
),
],
)
@app.callback(
Output("line_datafields", "figure"),
[
Input("countries", "value"),
Input("year-slider", "value"),
Input("data-picker", "value"),
Input("next-button-state", "n_clicks"),
],
)
def color_countries_and_region(country, years, datafield, n_clicks):
if country is None:
raise PreventUpdate
mask = (
(df.index.isin(country)) & (df["Year"] >= years[0]) & (df["Year"] <= years[1])
)
# logging.info(msg=locals())
df2 = df[mask]
line_fig = px.line(
df2,
x="Year",
y=datafield,
color=df2.index,
color_discrete_sequence=px.colors.qualitative.G10,
# mode="markers",
)
line_fig.update_layout(scatter_layout)
line_fig.update_layout(title=datafield.replace("_", " ").title())
line_fig.update_layout(yaxis_title=datafield.replace("_", " ").title())
return line_fig