-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
156 lines (129 loc) · 6.96 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pandas as pd
import requests
import streamlit as st
import plotly.graph_objs
import plotly.express as px
import os
from datetime import datetime
import locale
locale.setlocale(locale.LC_ALL, "de_DE")
def get_timestamp(file):
ts = os.path.getmtime(file)
return ts
@st.cache(suppress_st_warning=True, allow_output_mutation=True)
def load_data(file, timestamp, **kwargs):
df = pd.read_csv(file, **kwargs)
return df
@st.cache(suppress_st_warning=True, hash_funcs={plotly.graph_objs.Figure: lambda _: None}, allow_output_mutation=True)
def create_map(df, api_url):
# get geometry information for plotting
response = requests.get(api_url)
kreis_geo = []
for kreis in response.json()["features"]:
AdmUnitId = kreis["properties"]["AdmUnitId"]
geometry = kreis["geometry"]
kreis_geo.append({
"type": "Feature",
"geometry": geometry,
"id": AdmUnitId
})
geo = {'type': 'FeatureCollection', 'features': kreis_geo}
# keep only districts
df = df[df.AdmUnitId > 16]
# create map
fig = px.choropleth(df,
geojson=geo,
scope="europe",
color_continuous_scale="Burgyl",
locations="AdmUnitId",
color="inzidenz7Tage",
template="simple_white",
hover_name="verwaltungseinheit",
hover_data=("inzidenz7Tage", "infektionenNeu", "todeNeu"),
labels={"AdmUnitId": "Landkreis",
"verwarltungseinheit": "Landkreis",
"inzidenz7Tage": "7-Tage Inzidenz",
"todeNeu": "Todesfälle",
"infektionenNeu": "Neuinfektionen"}
)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
return fig
# load data and create map object
ts_daily = get_timestamp("covid_data.csv")
ts_history = get_timestamp("history.csv")
updated = datetime.fromtimestamp(ts_daily).strftime("%d.%m.%Y %H:%M")
df = load_data("covid_data.csv", timestamp=ts_daily, index_col=0)
history = load_data("history.csv", timestamp=ts_history, index_col="date")
fig = create_map(df, "https://opendata.arcgis.com/datasets/917fc37a709542548cc3be077a786c17_0.geojson")
##### ACTUAL APP:
st.title("Covid-19 Dashboard: Inzidenz in Deutschland")
st.markdown("*Datenstand: " + updated + "*")
st.markdown("<hr style=\"height:3px;border:none;background-color:darkgrey\">", unsafe_allow_html=True)
# filter data using input
choices = list(df.verwaltungseinheit.unique())
input = st.selectbox("Wählen Sie eine Aggregationsebene. Sie können entweder einzelne Stadt- und Landkreise auswählen oder die gesamte BRD oder Bundesländer auswählen.", choices, index=choices.index('Bundesrepublik Deutschland'))
# today's numbers:
inzidenz = round(float(df.loc[df["verwaltungseinheit"]==input].inzidenz7Tage), 1)
fall7tage = int(df.loc[df["verwaltungseinheit"]==input].infektionen7Tage)
neuinfektionen = int(df.loc[df["verwaltungseinheit"]==input].infektionenNeu)
gesamtinfektionen = int(df.loc[df["verwaltungseinheit"]==input].infektionenGesamt)
todeNeu = int(df.loc[df["verwaltungseinheit"]==input].todeNeu)
todeGesamt = int(df.loc[df["verwaltungseinheit"]==input].todeGesamt)
# numbers for comparison:
inzidenz1ago = round(float(history.loc[history["verwaltungseinheit"]==input].inzidenz7Tage[-2]), 1)
deaths1ago = round(float(history.loc[history["verwaltungseinheit"]==input].todeNeu[-2]), 1)
neuinfektionen7ago = round(float(history.loc[history["verwaltungseinheit"]==input].infektionenNeu[-8]), 1)
### KEY FACTS FOR SELECTED AGGREGATION
st.markdown("In Klammern dargestellt sind die Zahlen vom Vortag (7-Tage-Inzidenz, Neue Todesfälle) bzw. vor 7 Tagen (Neuinfektionen).")
col1, col2, col3 = st.beta_columns(3)
with col1:
st.markdown("### *7-Tage-Inzidenz:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(inzidenz), unsafe_allow_html=True)
st.markdown("### <font color=‘#8b0000’><strong>({:n})</strong></font>".format(inzidenz1ago), unsafe_allow_html=True)
st.markdown("### *7-Tage-Fallzahl:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(fall7tage), unsafe_allow_html=True)
st.write("\n")
with col2:
st.markdown("### *Neuinfektionen:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(neuinfektionen), unsafe_allow_html=True)
st.markdown("### <font color=‘#8b0000’><strong>({:n})</strong></font>".format(neuinfektionen7ago), unsafe_allow_html=True)
st.markdown("### *Gesamtzahl Fälle:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(gesamtinfektionen), unsafe_allow_html=True)
st.write("\n")
with col3:
st.markdown("### *Neue Todesfälle:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(todeNeu), unsafe_allow_html=True)
st.markdown("### <font color=‘#8b0000’><strong>({:n})</strong></font>".format(deaths1ago), unsafe_allow_html=True)
st.markdown("### *Gesamtzahl Todesfälle:*")
st.markdown("### <font color=‘#8b0000’><strong>{:n}</strong></font>".format(todeGesamt), unsafe_allow_html=True)
st.write("\n")
st.markdown("<hr style=\"height:3px;border:none;background-color:darkgrey\">", unsafe_allow_html=True)
### CHOROPLETH MAP FOR GERMANY
st.subheader("Infektionsgeschehen in Deutschland:")
st.write("\n")
st.plotly_chart(fig)
st.write("\n")
st.markdown("<hr style=\"height:3px;border:none;background-color:darkgrey\">", unsafe_allow_html=True)
### TOP 5 / BOTTOM 5
st.subheader("Landkreise mit den höchsten und niedrigsten Kennzahlen:")
df2 = df.set_index("verwaltungseinheit").copy()
df2.rename(columns={"infektionenGesamt":"Gesamtzahl Infektionen", "todeGesamt": "Gesamtzahl Todesfälle",
"infektionenNeu": "Neuinfektionen", "todeNeu": "Neue Todesfälle",
"infektionen7Tage":"Gesamtzahl Infektionen vergangene 7 Tage", "inzidenz7Tage": "7-Tage-Inzidenz"},
inplace=True)
metrics = ["7-Tage-Inzidenz", "Gesamtzahl Infektionen vergangene 7 Tage", "Neuinfektionen", "Gesamtzahl Infektionen",
"Neue Todesfälle", "Gesamtzahl Todesfälle"]
input_number = st.selectbox("Wählen Sie eine Kennzahl.", metrics, index=0)
df2 = df2.loc[df2.AdmUnitId > 16] # exclude BRD and federal states aggregation
df2["7-Tage-Inzidenz"] = round(df2["7-Tage-Inzidenz"], 1)
df2 = df2[[input_number]].copy() # make copy of data with relevant data only
df2.index.name = None # remove index name
pd.options.display.float_format = "{:,.1f}".format
col4, col5 = st.beta_columns(2)
with col4:
df2.sort_values(input_number, ascending=True, inplace=True)
st.table(df2.head().style.format("{:n}"))
with col5:
df2.sort_values(input_number, ascending=False, inplace=True)
st.table(df2.head().style.format("{:n}"))