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-*-
|
| 2 |
+
"""Dashboard covid.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1pg2FFvoMPYu0fPgxf4luzAkLoYiFDgJs
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install hvplot
|
| 11 |
+
!pip install jupyter_bokeh
|
| 12 |
+
!pip install panel
|
| 13 |
+
!pip install bokeh
|
| 14 |
+
!pip install holoviews
|
| 15 |
+
!pip install plotly --upgrade
|
| 16 |
+
!pip install pandas numpy plotly ipywidgets
|
| 17 |
+
|
| 18 |
+
import pandas as pd
|
| 19 |
+
import numpy as np
|
| 20 |
+
import matplotlib.pyplot as plt
|
| 21 |
+
import seaborn as sns
|
| 22 |
+
|
| 23 |
+
!gdown 'https://drive.google.com/uc?export=download&id=1L42Fjkh8-cPIwswMHK5GtyecYdF-_Xjz'
|
| 24 |
+
|
| 25 |
+
df = pd.read_csv('owid-covid-data.csv')
|
| 26 |
+
|
| 27 |
+
import pandas as pd
|
| 28 |
+
import plotly.express as px
|
| 29 |
+
import panel as pn
|
| 30 |
+
|
| 31 |
+
# Enable Panel Plotly extension
|
| 32 |
+
pn.extension('plotly')
|
| 33 |
+
|
| 34 |
+
# Read the dataset
|
| 35 |
+
df = pd.read_csv("owid-covid-data.csv")
|
| 36 |
+
|
| 37 |
+
# Convert 'date' column to datetime
|
| 38 |
+
df['date'] = pd.to_datetime(df['date'])
|
| 39 |
+
|
| 40 |
+
# Add a year column
|
| 41 |
+
df['year'] = df['date'].dt.year
|
| 42 |
+
|
| 43 |
+
# Drop rows with NaN in 'continent'
|
| 44 |
+
df = df.dropna(subset=['continent'])
|
| 45 |
+
|
| 46 |
+
# Get unique continents
|
| 47 |
+
continents_list = sorted(df['continent'].unique().tolist())
|
| 48 |
+
|
| 49 |
+
# Create Panel widgets for interactivity
|
| 50 |
+
continents_dropdown = pn.widgets.Select(name='Select Continent', options=continents_list)
|
| 51 |
+
countries_dropdown = pn.widgets.Select(name='Select Country', options=[])
|
| 52 |
+
|
| 53 |
+
# Create a year slider from 2020 to 2023
|
| 54 |
+
year_slider = pn.widgets.IntSlider(name='Select Year', start=2020, end=2023, step=1, value=2020)
|
| 55 |
+
|
| 56 |
+
# Callback to update the country dropdown based on the selected continent
|
| 57 |
+
def update_countries(event):
|
| 58 |
+
selected_continent = event.new
|
| 59 |
+
filtered_countries = (
|
| 60 |
+
df[df['continent'] == selected_continent]['location']
|
| 61 |
+
.dropna()
|
| 62 |
+
.unique()
|
| 63 |
+
.tolist()
|
| 64 |
+
)
|
| 65 |
+
filtered_countries.sort()
|
| 66 |
+
countries_dropdown.options = filtered_countries
|
| 67 |
+
|
| 68 |
+
# Attach the callback
|
| 69 |
+
continents_dropdown.param.watch(update_countries, 'value')
|
| 70 |
+
|
| 71 |
+
# Callback for Total Cases line chart, updated with the year slider
|
| 72 |
+
@pn.depends(countries_dropdown, year_slider)
|
| 73 |
+
def total_cases_plot(selected_country, selected_year):
|
| 74 |
+
if not selected_country:
|
| 75 |
+
return pn.pane.Markdown("**Please select a country.**")
|
| 76 |
+
|
| 77 |
+
filtered_data = df[(df['location'] == selected_country) & (df['year'] == selected_year)]
|
| 78 |
+
if filtered_data.empty:
|
| 79 |
+
return pn.pane.Markdown("**No data available for the selected country and year.**")
|
| 80 |
+
|
| 81 |
+
fig = px.line(
|
| 82 |
+
filtered_data,
|
| 83 |
+
x='date',
|
| 84 |
+
y='total_cases',
|
| 85 |
+
title=f"Total Cases in {selected_country} ({selected_year})",
|
| 86 |
+
labels={'total_cases': 'Total Cases'}
|
| 87 |
+
)
|
| 88 |
+
return pn.pane.Plotly(fig)
|
| 89 |
+
|
| 90 |
+
# Callback for Total Deaths bar plot, updated with the year slider
|
| 91 |
+
@pn.depends(countries_dropdown, year_slider)
|
| 92 |
+
def total_deaths_plot(selected_country, selected_year):
|
| 93 |
+
if not selected_country:
|
| 94 |
+
return pn.pane.Markdown("**Please select a country.**")
|
| 95 |
+
|
| 96 |
+
filtered_data = df[(df['location'] == selected_country) & (df['year'] == selected_year)]
|
| 97 |
+
if filtered_data.empty:
|
| 98 |
+
return pn.pane.Markdown("**No data available for the selected country and year.**")
|
| 99 |
+
|
| 100 |
+
fig = px.bar(
|
| 101 |
+
filtered_data,
|
| 102 |
+
x='date',
|
| 103 |
+
y='total_deaths',
|
| 104 |
+
title=f"Total Deaths in {selected_country} ({selected_year})",
|
| 105 |
+
labels={'total_deaths': 'Total Deaths'}
|
| 106 |
+
)
|
| 107 |
+
return pn.pane.Plotly(fig)
|
| 108 |
+
|
| 109 |
+
# Global vaccination trend plot, updated with the year slider
|
| 110 |
+
@pn.depends(year_slider)
|
| 111 |
+
def vaccination_trend_plot(selected_year):
|
| 112 |
+
filtered_data = df[(df['year'] == selected_year) & df['total_vaccinations'].notna()]
|
| 113 |
+
fig = px.line(
|
| 114 |
+
filtered_data,
|
| 115 |
+
x='date',
|
| 116 |
+
y='total_vaccinations',
|
| 117 |
+
color='continent',
|
| 118 |
+
title=f"Global Vaccination Trend in {selected_year}",
|
| 119 |
+
labels={'total_vaccinations': 'Total Vaccinations'}
|
| 120 |
+
)
|
| 121 |
+
return pn.pane.Plotly(fig)
|
| 122 |
+
|
| 123 |
+
# Correlation heatmap between total cases and total deaths, updated with the year slider
|
| 124 |
+
@pn.depends(year_slider)
|
| 125 |
+
def correlation_heatmap(selected_year):
|
| 126 |
+
filtered_data = df[(df['year'] == selected_year) & df['total_cases'].notna() & df['total_deaths'].notna()]
|
| 127 |
+
correlation_data = filtered_data[['total_cases', 'total_deaths']].dropna()
|
| 128 |
+
correlation_matrix = correlation_data.corr()
|
| 129 |
+
|
| 130 |
+
fig = px.imshow(
|
| 131 |
+
correlation_matrix,
|
| 132 |
+
text_auto=True,
|
| 133 |
+
color_continuous_scale='Blues',
|
| 134 |
+
title=f"Correlation Between Total Cases and Total Deaths ({selected_year})",
|
| 135 |
+
labels={'color': 'Correlation Coefficient'}
|
| 136 |
+
)
|
| 137 |
+
return pn.pane.Plotly(fig)
|
| 138 |
+
|
| 139 |
+
# Layout the dashboard
|
| 140 |
+
dashboard = pn.Column(
|
| 141 |
+
pn.pane.Markdown("# COVID-19 Interactive Dashboard"),
|
| 142 |
+
pn.pane.Markdown("### Select a Continent, Country, and Year to View Data"),
|
| 143 |
+
continents_dropdown,
|
| 144 |
+
countries_dropdown,
|
| 145 |
+
year_slider,
|
| 146 |
+
pn.Row(total_cases_plot, total_deaths_plot), # First row: Line + Bar plot
|
| 147 |
+
pn.Row(vaccination_trend_plot, correlation_heatmap) # Second row: Line + Heatmap
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Display the dashboard
|
| 151 |
+
dashboard.servable()
|
| 152 |
+
|