This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
255 lines (213 loc) · 8.99 KB
/
main.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
"""
MIT License
Copyright (c) 2022 UnB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from plotly.graph_objects import Figure # type: ignore
from dash import Dash, dcc, html, ctx # type: ignore
from dash.dependencies import Input, Output # type: ignore
import dash_daq as daq # type: ignore
from data_migrator import df
from utils import (
filter_columns,
filter_values,
filter_range,
closest_value,
)
from graphs import migration, population, fertility, average, urban_population
app = Dash(__name__)
# Layout
app.layout = html.Div(children=[
html.H1(
children="População em Nível Continental", # type: ignore
style={'textAlign': 'center'} # type: ignore
),
# Taxa de Migração
html.Div(children=[
dcc.Graph(id="migration-rate", figure=migration.chart), # type: ignore
dcc.RangeSlider(
min=1955, # type: ignore
max=2020, # type: ignore
id="migration-rate-slider", # type: ignore
step=1, # type: ignore
marks=None, # type: ignore
pushable=True, # type: ignore
value=[1955, 2020], # type: ignore
tooltip={ # type: ignore
"placement": "bottom",
"always_visible": True,
},
),
]),
# População Urbana
html.Div(children=[
dcc.Graph(
id="urban-population", # type: ignore
figure=urban_population.chart, # type: ignore
),
html.Button("<",id="backward", n_clicks=0), # type: ignore
html.Button(">", id="forward", n_clicks=0), # type: ignore
]),
# Taxa de Fertilidade
html.Div(children=[
dcc.Graph(id="fertility-rate", figure=fertility.chart), # type: ignore
dcc.RangeSlider(
min=1955, # type: ignore
max=2020, # type: ignore
id="fertility-rate-year-slider", # type: ignore
step=1, # type: ignore
marks=None, # type: ignore
pushable=True, # type: ignore
value=[1955, 2020], # type: ignore
tooltip={ # type: ignore
"placement": "bottom",
"always_visible": True,
},
),
dcc.RangeSlider(
min=1, # type: ignore
max=7, # type: ignore
id="fertility-rate-slider", # type: ignore
step=1, # type: ignore
marks=None, # type: ignore
pushable=True, # type: ignore
value=[1.43, 6.71], # type: ignore
tooltip={ # type: ignore
"placement": "bottom",
"always_visible": True,
},
),
]),
# Percentual Populacional
html.Div(children=[
dcc.Graph(id="population-percentage", figure=population.chart), # type: ignore
daq.NumericInput( # type: ignore
min=1955, # type: ignore
max=2020, # type: ignore
id="population-percentage-input", # type: ignore
label="Alterar ano", # type: ignore
labelPosition="top", # type: ignore
size=80, # type: ignore
value=population.current_year, # type: ignore
)
]),
# Média de Idades
html.Div(children=[
dcc.Graph(id="age-average", figure=average.chart), # type: ignore
dcc.Dropdown( # type: ignore
id="age-average-dropdown", # type: ignore
placeholder="Atualizando região...", # type: ignore
options=average.current_regions, # type: ignore
value=average.current_regions, # type: ignore
multi=True # type: ignore
)
])
])
# Callbacks
@app.callback( # type: ignore
Output(component_id="migration-rate", component_property="figure"),
Input(component_id="migration-rate-slider", component_property="value"),
)
def update_migration_rate(value: list[int]) -> Figure: # type: ignore
# Filtramos o `DataFrame` com os valores do intervalo escolhido.
new_df = filter_values(df, "year", *list(range(*value)))
# Criamos e retornamos o gráfico com os novos valores.
return migration.create_chart(new_df) # type: ignore
@app.callback( # type: ignore
Output(component_id="urban-population", component_property="figure"),
Input(component_id="backward", component_property="n_clicks"),
Input(component_id="forward", component_property="n_clicks"),
)
def update_urban_population(
backward: int,
forward: int,
) -> Figure: # type: ignore
# Definimos os valores para os botões `forward` e `backward`.
values: dict[str | None, int] = {"backward": -5, "forward": 5, None: 0}
triggered_id = ctx.triggered_id # type: ignore
current_year = urban_population.current_year
config = urban_population.config
if 1955 <= current_year + values[triggered_id] <= 2020: # type: ignore
urban_population.current_year += values[triggered_id] # type: ignore
title_text = f"População Urbana ({current_year})"
config["title"]["text"] = title_text
filtered_df = filter_values(df, "year", current_year)
return urban_population.create_chart(filtered_df, config=config) # type: ignore
@app.callback( # type: ignore
Output(component_id="fertility-rate", component_property="figure"),
Input(
component_id="fertility-rate-year-slider",
component_property="value",
),
Input(component_id="fertility-rate-slider", component_property="value"),
)
def update_fertility_rate(
years_values: list[int],
rate_values: list[int],
) -> Figure: # type: ignore
# Filtramos o `DataFrame` com os valores do intervalo escolhido.
filtered_df = filter_columns(df, "year", "region", "fertility_rate")
new_df = filter_range(filtered_df, "year", years_values)
new_df = filter_range(new_df, "fertility_rate", rate_values)
# Criamos e retornamos o gráfico com os novos valores.
return fertility.create_chart(new_df) # type: ignore
@app.callback( # type: ignore
Output(component_id="population-percentage", component_property="figure"),
Input(
component_id="population-percentage-input",
component_property="value",
),
)
def update_population_percentage(value: int) -> Figure:
population_df = population.filtered_columns
config = population.config
rows = population_df.values.tolist() # type: ignore
# Criamos uma lista com todos os anos disponíveis no DataFrame.
years_list: list[int] = []
for row in rows:
row_year: int = row[0]
if not row_year in years_list:
years_list.append(row[0])
# Verificamos se o ano escolhido pelo usuário é valido.
# Caso não esteja na lista `years_list`, retornará o ano mais próximo.
year: int = closest_value(years_list, value)
# Filtramos o DataFrame para o ano escolhido.
new_df = filter_values(population_df, "year", year)
# Atualizamos o parâmetro `ano` para as configurações do gráfico.
title_text = f"Percentual populacional por continente de {year}"
config["title"]["text"] = title_text
# Retornamos o gráfico com os novos valores.
return population.create_chart(new_df)
@app.callback( # type: ignore
Output(component_id="age-average", component_property="figure"),
Output(component_id="age-average-dropdown", component_property="value"),
Input(component_id="age-average-dropdown", component_property="value")
)
def update_age_average(value: list[str]) -> tuple[Figure, list[str]]:
# É necessário ter ao menos uma região para mostrar o gráfico.
# Então, caso `value` for vazio, retornamos o valor para a região
# escolhida anteriormente.
if len(value) > 0:
average.current_regions = value
# Filtramos o DataFrame para a região escolhida.
new_df = filter_values(average.filtered_df, "region", *average.current_regions)
# Retornamos o gráfico com os novos valores e, junto com ele,
# retornamos também o valor das regiões escolhidas para caso
# `value` for vazio.
return average.create_chart(new_df), average.current_regions
if __name__ == "__main__":
app.run_server(debug=True) # type: ignore