Skip to content

Instantly share code, notes, and snippets.

@FBosler
Created March 15, 2020 19:24
Show Gist options
  • Save FBosler/0c3e89ba8a4a5cc6d3765f257b669cd2 to your computer and use it in GitHub Desktop.
Save FBosler/0c3e89ba8a4a5cc6d3765f257b669cd2 to your computer and use it in GitHub Desktop.
covid-visualization
import dash
import os
import plotly.express as px
import pandas as pd
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import json
from urllib.request import urlopen
from dash.dependencies import Input, Output
from apscheduler.schedulers.background import BackgroundScheduler
def fetch_counties():
URL = "https://raw.githubusercontent.com/isellsoap/deutschlandGeoJSON/master/4_kreise/4_niedrig.geojson"
with urlopen(URL) as response:
counties = json.load(response)
return counties
def create_fig(df, counties):
fig = px.choropleth(
data_frame=df,
geojson=counties,
color="log(infected+1)",
color_continuous_scale='oranges',
hover_name='infected',
hover_data=['Landkreis'],
locations="Landkreis",
featureidkey="properties.NAME_3",
projection="mercator",
animation_frame='date'
)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(
height=1000,
margin={"r": 0, "t": 0, "l": 0, "b": 0}
)
return fig
def load_data():
global data
_ = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data.csv'))
available_dates = _[_['infected'] != 0].date.unique()
data = _[_.date.isin(available_dates)]
return data
data = load_data()
## dropdown config
BUNDESLAENDER = [{'label':'All', 'value':'All'}]
BUNDESLAENDER.extend([{'label': land, 'value': land} for land in data.Bundesland.unique()])
cron = BackgroundScheduler()
cron.add_job(func=load_data, trigger="interval", seconds=30)
cron.start()
counties = fetch_counties()
fig = create_fig(df=data, counties=counties)
external_stylesheets = [dbc.themes.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dbc.Container([
html.H1(children='Visualization of reported Covid-19 cases in Germany by Districts'),
dcc.Link(
'Date taken from the RKI page',
href='https://www.rki.de/DE/Content/InfAZ/N/Neuartiges_Coronavirus/Situationsberichte/Gesamt.html'
),
html.Br(),
dbc.Row([
# Select City
dbc.Label(
"Bundesland",
html_for="select-bundesland",
id='select-bundesland-label',
width=2
),
dbc.Tooltip(
"Select the county to zoom in",
target="select-bundesland-label",
),
dbc.Col([
dcc.Dropdown(
id='select-bundesland',
options=BUNDESLAENDER,
value='All'
),
], width=10)
]),
dbc.Row([
dbc.Col([
dcc.Graph(
id='infected-graph',
figure=fig
),
], md=12)
])
], fluid=True)
])
server = app.server
current_interval = 0
@app.callback(
Output('infected-graph', 'figure'),
[
Input('select-bundesland', 'value')
])
def update_figure(bundesland):
if bundesland == 'All':
filtered_df = data
else:
filtered_df = data[data.Bundesland == bundesland]
return create_fig(filtered_df, counties)
if __name__ == '__main__':
app.run_server()
@LeoVinciTZ
Copy link

missing a Data.csv file

@WilbertMarins
Copy link

I would like to know if it would be possible to make the file "data.csv" available for testing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment