Skip to content

Instantly share code, notes, and snippets.

@danielmk
Last active June 29, 2023 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmk/5cbbfbd0fef164a97df53265c334f551 to your computer and use it in GitHub Desktop.
Save danielmk/5cbbfbd0fef164a97df53265c334f551 to your computer and use it in GitHub Desktop.
Data dashboard in Jupyter visualizing Covid-19 hospitalization data from remote source.
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show, output_notebook
import ipywidgets as widgets
from IPython.display import display, clear_output
output_notebook()
"""Load Iris dataset and transform the pandas DataFrame"""
data = pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/hospitalizations/covid-hospitalizations.csv")
data['date'] = pd.to_datetime(data['date'])
"""Define callback function for the UI"""
def dropdown_cb(x):
p = create_figure(
country_dropdown.children[0].value,
indicator_dropdown.children[0].value,
data)
fig[0] = p
with output_figure:
clear_output(True)
show(fig[0])
return x
def create_figure(country, indicator, data):
"""This is a helper function that creates a new figure and
plots values from all three species. x_var and y_var control
the features on each axis."""
data = data[(data['entity'] == country) & (data['indicator'] == indicator)].sort_values(by='date')
p = figure(title="",
x_axis_label="Days",
y_axis_label=indicator,
x_axis_type='datetime')
line = p.line(data['date'], data['value'], line_width=2)
return p
# The output widget is where we direct our figures
output_figure = widgets.Output()
# Create the default figure
fig = [] # Storing the figure in a singular list is a bit of a
# hack. We need it to properly mutate the current
# figure in our callbacks.
p = create_figure(
"Algeria",
"Daily ICU occupancy",
data)
fig.append(p)
with output_figure:
show(fig[0])
# Dropdown menu for x-axis feature.
country_dropdown = widgets.interactive(dropdown_cb,
x=data['entity'].unique());
country_dropdown.children[0].description = 'Country'
country_dropdown.children[0].value = data['entity'].unique()[0]
# Dropdown menu for y-axis feature.
indicator_dropdown = widgets.interactive(dropdown_cb,
x=data['indicator'].unique());
indicator_dropdown.children[0].description = 'Indicator'
indicator_dropdown.children[0].value = data['indicator'].unique()[0]
# This creates the menu
menu=widgets.VBox([country_dropdown, indicator_dropdown])
"""Create the full app with menu and output"""
# The Layout adds some styling to our app.
# You can add Layout to any widget.
app_layout = widgets.Layout(display='flex',
flex_flow='row nowrap',
align_items='center',
border='none',
width='100%',
margin='5px 5px 5px 5px')
# The final app is just a box
app=widgets.Box([menu, output_figure], layout=app_layout)
# Display the app
display(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment