-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (121 loc) · 5.42 KB
/
app.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
import pandas as pd
import geojson as gj
import plotly.express as px
import urllib.request
import dash
from dash import dcc
from dash import html
import dash_bootstrap_components as dbc
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
# # # # # DATA # # # # # #
# loading dataframe - vaccinations
data_vac_path = 'https://github.com/sebastian-konicz/vaccinations/raw/main/data/interim/vaccinations_county_20211007.xlsx'
data_vac = pd.read_excel(data_vac_path, engine='openpyxl')
# restricting dataframe
data_vac = data_vac[['teryt', 'powiat', '%_zaszczepieni']]
# reshaping teryt
data_vac['teryt'] = data_vac['teryt'].apply(lambda x: str(x).zfill(4))
# loading dataframe - covid_cases
data_elec_path = 'https://github.com/sebastian-konicz/vaccinations/raw/main/data/interim/elections_county.xlsx'
data_elec = pd.read_excel(data_elec_path, engine='openpyxl')
# restricting dataframe
data_elec = data_elec[['teryt', 'powiat', '%_glosy']]
# reshaping teryt
data_elec['teryt'] = data_elec['teryt'].apply(lambda x: str(x).zfill(4))
# loading geojson
jsonurl = 'https://github.com/sebastian-konicz/covid-dashboard/raw/main/data/interim/geo/geo_county.geojson'
with urllib.request.urlopen(jsonurl) as url:
geojson = gj.load(url)
# # # # # # VACCINATION MAP # # # # # #
# get the maximum value to cap displayed values - vaccinations
max_log_vac = data_vac['%_zaszczepieni'].max()
min_val_vac = data_vac['%_zaszczepieni'].min()
max_val_vac = int(max_log_vac) + 1
fig_vac = px.choropleth_mapbox(data_vac,
geojson=geojson,
featureidkey='properties.JPT_KOD_JE',
locations='teryt',
hover_name='powiat',
# hover_data='%_zaszczepieni',
color='%_zaszczepieni',
title='procent osób zaszczepionych',
color_continuous_scale=px.colors.diverging.RdBu,
range_color=(min_val_vac, max_val_vac),
mapbox_style="carto-positron",
zoom=5, center={"lat": 52, "lon": 19},
opacity=0.5,
)
fig_vac.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
# # # # # # COVID CASES MAP # # # # # #
# get the maximum value to cap displayed values - vaccinations
max_log_cov = data_elec['%_glosy'].max()
min_val_cov = data_elec['%_glosy'].min()
max_val_cov = int(max_log_vac) + 1
fig_cov = px.choropleth_mapbox(data_elec,
geojson=geojson,
featureidkey='properties.JPT_KOD_JE',
locations='teryt',
color='%_glosy',
color_continuous_scale=px.colors.diverging.RdBu_r,
range_color=(min_val_cov, max_val_cov),
mapbox_style="carto-positron",
zoom=5, center={"lat": 52, "lon": 19},
opacity=0.5,
)
fig_cov.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
# # # # # LAYOUT # # # # # #
# app.layout = html.Div([
# html.H1('Mapa szczepień na COVID-19 w Polsce ',
# style={'textAlign': 'center'}),
# html.Div([
# dcc.Graph(
# id='vaccination_map',
# figure=fig_vac
# ),
# # dcc.Graph(
# # id='covid_cases_map',
# # figure=fig_cov
# # ),
# ]),
# ])
width = 6
app.layout = dbc.Container([
html.H1('Mapa szczepień na COVID-19 w Polsce ',
style={'textAlign': 'center'}),
dbc.Row([
dbc.Col(
dcc.Markdown('''
##### Procent osób zaszczepionych przeciwko COVID19 w powiatach
**Źródło**: Oficjalne dane dot. zaszczepienia przeciwko COVID19 w gminach z portalu [Otwarte dane](https://dane.gov.pl/pl/dataset/2476,odsetek-osob-zaszczepionych-przeciwko-covid19-w-gm?fbclid=IwAR059OLAARQT9Umr02jVnfn9abacBD0ZF12fNyHH7m1hHXUswt-tufdMDsA)
**Uwagi**: Dane źródłowe zostały zagregowane z poziomu gmin do powiatów
'''),
width=width),
dbc.Col(
dcc.Markdown('''
##### Procent osób głosujących na PiS w wyborach do parlamentu w 2019 roku w podziale na powiaty
**Źródło**: Dane Państwowej Komisji Wyborczej dot. wyborów do Sejmu i Senatu Rzeczypospolitej Polskiej 2019 [sejmsenat2019.pkw.gov.pl](https://sejmsenat2019.pkw.gov.pl/sejmsenat2019/data/csv/wyniki_gl_na_listy_po_powiatach_sejm_xlsx.zip)
'''),
width=width),
]),
dbc.Row(
[
dbc.Col([
dcc.Graph(
id='vaccination_map',
figure=fig_vac
)
], width=width),
dbc.Col([
dcc.Graph(
id='covid_cases_map',
figure=fig_cov
)
], width=width),
],
),
],
fluid=True,
)
if __name__ == '__main__':
app.run_server(debug=True)