-
Notifications
You must be signed in to change notification settings - Fork 0
/
LM_line_life_expectancy.py
186 lines (155 loc) · 4.07 KB
/
LM_line_life_expectancy.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
#!/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
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]
# Dash
external_stylesheets = [dbc.themes.DARKLY]
# 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",
},
)
graph1 = dcc.Graph(
id="life_exp_scatter", config={"displayModeBar": True, "displaylogo": False}
)
button = dbc.Button(
style={
"fontSize": 18,
"marginLeft": "20px",
"marginRight": "80px",
"backgroundColor": "#111",
"color": "#ffffff",
},
id="next-button-state",
n_clicks=0,
children="Next",
color="Primary",
className="me-1",
href="/page2",
)
# Layout
scatter_layout = go.Layout(
title="Life Expectancy (Yearly Basis)",
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",
)
layout = html.Div(
style={
"fontFamily": "Sawasdee",
"fontSize": 22,
"backgroundColor": "#111111",
},
children=[
html.Div(
[
html.Br(),
dropdown,
html.Br(),
graph1,
html.Br(),
],
),
html.Br(),
year_slider,
html.Br(),
html.Div(
[
button,
],
className="d-grip gap-2 d-md-flex justify-content-md-end",
),
],
)
@app.callback(
Output("life_exp_scatter", "figure"),
Input("countries", "value"),
# State("year-slider", "value"),
)
def color_countries_and_region(
country,
# years
):
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]
# df2_region = df[df["map_ref"] == region]
line_fig = px.line(
df2,
x="Year",
y="Life_expectancy",
color=df2.index,
color_discrete_sequence=px.colors.qualitative.G10,
# mode="markers",
)
line_fig.update_layout(scatter_layout)
return line_fig