In [1]:
# from https://github.com/thu-vu92/python-dashboard-panel/blob/main/Interactive_dashboard.ipynb
In [1]:
### this may work as a pyscript example later
In [2]:
import pandas as pd
import numpy as np
import panel as pn
pn.extension('tabulator')
# pn.extension(sizing_mode = 'stretch_width')
import hvplot.pandas
In [3]:
# cache data to improve dashboard performance
if 'data' not in pn.state.cache.keys():
df = pd.read_csv('https://raw.githubusercontent.com/owid/co2-data/master/owid-co2-data.csv')
#df = pd.read_csv("docs/owid-co2-data.csv")
pn.state.cache['data'] = df.copy()
else:
df = pn.state.cache['data']
In [4]:
df
Out[4]:
| iso_code | country | year | co2 | co2_per_capita | trade_co2 | cement_co2 | cement_co2_per_capita | coal_co2 | coal_co2_per_capita | ... | ghg_excluding_lucf_per_capita | methane | methane_per_capita | nitrous_oxide | nitrous_oxide_per_capita | population | gdp | primary_energy_consumption | energy_per_capita | energy_per_gdp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | AFG | Afghanistan | 1949 | 0.015 | 0.002 | NaN | NaN | NaN | 0.015 | 0.002 | ... | NaN | NaN | NaN | NaN | NaN | 7624058.0 | NaN | NaN | NaN | NaN |
| 1 | AFG | Afghanistan | 1950 | 0.084 | 0.011 | NaN | NaN | NaN | 0.021 | 0.003 | ... | NaN | NaN | NaN | NaN | NaN | 7752117.0 | 9.421400e+09 | NaN | NaN | NaN |
| 2 | AFG | Afghanistan | 1951 | 0.092 | 0.012 | NaN | NaN | NaN | 0.026 | 0.003 | ... | NaN | NaN | NaN | NaN | NaN | 7840151.0 | 9.692280e+09 | NaN | NaN | NaN |
| 3 | AFG | Afghanistan | 1952 | 0.092 | 0.012 | NaN | NaN | NaN | 0.032 | 0.004 | ... | NaN | NaN | NaN | NaN | NaN | 7935996.0 | 1.001733e+10 | NaN | NaN | NaN |
| 4 | AFG | Afghanistan | 1953 | 0.106 | 0.013 | NaN | NaN | NaN | 0.038 | 0.005 | ... | NaN | NaN | NaN | NaN | NaN | 8039684.0 | 1.063052e+10 | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 25984 | ZWE | Zimbabwe | 2016 | 10.738 | 0.765 | 1.415 | 0.639 | 0.046 | 6.959 | 0.496 | ... | 2.076 | 11.50 | 0.820 | 6.21 | 0.443 | 14030338.0 | 2.096179e+10 | 46.666 | 3326.073 | 2.226 |
| 25985 | ZWE | Zimbabwe | 2017 | 9.582 | 0.673 | 1.666 | 0.678 | 0.048 | 5.665 | 0.398 | ... | 2.023 | 11.62 | 0.816 | 6.35 | 0.446 | 14236599.0 | 2.194784e+10 | 45.936 | 3226.617 | 2.093 |
| 25986 | ZWE | Zimbabwe | 2018 | 11.854 | 0.821 | 1.308 | 0.697 | 0.048 | 7.101 | 0.492 | ... | 2.173 | 11.96 | 0.828 | 6.59 | 0.456 | 14438812.0 | 2.271535e+10 | 47.502 | 3289.887 | 2.091 |
| 25987 | ZWE | Zimbabwe | 2019 | 10.949 | 0.748 | 1.473 | 0.697 | 0.048 | 6.020 | 0.411 | ... | NaN | NaN | NaN | NaN | NaN | 14645473.0 | NaN | 49.427 | 3374.877 | NaN |
| 25988 | ZWE | Zimbabwe | 2020 | 10.531 | 0.709 | NaN | 0.697 | 0.047 | 6.257 | 0.421 | ... | NaN | NaN | NaN | NaN | NaN | 14862927.0 | NaN | NaN | NaN | NaN |
25989 rows × 60 columns
In [5]:
df[df['country'] == 'North America']
Out[5]:
| iso_code | country | year | co2 | co2_per_capita | trade_co2 | cement_co2 | cement_co2_per_capita | coal_co2 | coal_co2_per_capita | ... | ghg_excluding_lucf_per_capita | methane | methane_per_capita | nitrous_oxide | nitrous_oxide_per_capita | population | gdp | primary_energy_consumption | energy_per_capita | energy_per_gdp | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 16785 | NaN | North America | 1785 | 0.004 | NaN | NaN | NaN | NaN | 0.004 | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 16786 | NaN | North America | 1786 | 0.004 | NaN | NaN | NaN | NaN | 0.004 | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 16787 | NaN | North America | 1787 | 0.004 | NaN | NaN | NaN | NaN | 0.004 | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 16788 | NaN | North America | 1788 | 0.004 | NaN | NaN | NaN | NaN | 0.004 | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 16789 | NaN | North America | 1789 | 0.004 | NaN | NaN | NaN | NaN | 0.004 | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 17016 | NaN | North America | 2016 | 6471.360 | 11.353 | 309.904 | 70.858 | 0.124 | 1509.631 | 2.648 | ... | NaN | NaN | NaN | NaN | NaN | 573814461.0 | NaN | NaN | NaN | NaN |
| 17017 | NaN | North America | 2017 | 6412.982 | 11.155 | 335.009 | 72.914 | 0.127 | 1467.602 | 2.553 | ... | NaN | NaN | NaN | NaN | NaN | 578582963.0 | NaN | NaN | NaN | NaN |
| 17018 | NaN | North America | 2018 | 6588.630 | 11.366 | 345.915 | 74.787 | 0.129 | 1398.979 | 2.413 | ... | NaN | NaN | NaN | NaN | NaN | 582921781.0 | NaN | NaN | NaN | NaN |
| 17019 | NaN | North America | 2019 | 6460.726 | 11.056 | 354.353 | 76.925 | 0.132 | 1214.329 | 2.078 | ... | NaN | NaN | NaN | NaN | NaN | 587512621.0 | NaN | NaN | NaN | NaN |
| 17020 | NaN | North America | 2020 | 5775.159 | 9.805 | NaN | 76.870 | 0.130 | 965.430 | 1.639 | ... | NaN | NaN | NaN | NaN | NaN | 592062319.0 | NaN | NaN | NaN | NaN |
236 rows × 60 columns
In [6]:
# Fill NAs with 0s and create GDP per capita column
df = df.fillna(0)
df['gdp_per_capita'] = np.where(df['population']!= 0, df['gdp']/ df['population'], 0)
In [7]:
# Make DataFrame Pipeline Interactive
idf = df.interactive()
(1) CO2 emission over time by continent¶
In [8]:
# Define Panel widgets
year_slider = pn.widgets.IntSlider(name='Year slider', start=1750, end=2020, step=5, value=1850)
year_slider
Out[8]:
In [9]:
# Radio buttons for CO2 measures
yaxis_co2 = pn.widgets.RadioButtonGroup(
name='Y axis',
options=['co2', 'co2_per_capita',],
button_type='success'
)
In [10]:
continents = ['World', 'Asia', 'Oceania', 'Europe', 'Africa', 'North America', 'South America', 'Antarctica']
co2_pipeline = (
idf[
(idf.year <= year_slider) &
(idf.country.isin(continents))
]
.groupby(['country', 'year'])[yaxis_co2].mean()
.to_frame()
.reset_index()
.sort_values(by='year')
.reset_index(drop=True)
)
In [11]:
co2_pipeline
Out[11]:
In [12]:
co2_plot = co2_pipeline.hvplot(x = 'year', by='country', y=yaxis_co2,line_width=2, title="CO2 emission by continent")
co2_plot
Out[12]:
(2) Table - CO2 emission over time by continent¶
In [13]:
co2_table = co2_pipeline.pipe(pn.widgets.Tabulator, pagination='remote', page_size = 10, sizing_mode='stretch_width')
co2_table
WARNING:param.main: DataTabulator was not imported on instantiation and may not render in a notebook. Restart the notebook kernel and ensure you load it as part of the extension using:
pn.extension('tabulator')
Out[13]:
(3) CO2 vs GDP scatterplot¶
In [14]:
co2_vs_gdp_scatterplot_pipeline = (
idf[
(idf.year == year_slider) &
(~ (idf.country.isin(continents)))
]
.groupby(['country', 'year', 'gdp_per_capita'])['co2'].mean()
.to_frame()
.reset_index()
.sort_values(by='year')
.reset_index(drop=True)
)
In [15]:
co2_vs_gdp_scatterplot_pipeline
Out[15]:
In [16]:
co2_vs_gdp_scatterplot = co2_vs_gdp_scatterplot_pipeline.hvplot(x='gdp_per_capita',
y='co2',
by='country',
size=80, kind="scatter",
alpha=0.7,
legend=False,
height=500,
width=500)
co2_vs_gdp_scatterplot
Out[16]:
(4) Bar chart with CO2 sources by continent¶
In [17]:
yaxis_co2_source = pn.widgets.RadioButtonGroup(
name='Y axis',
options=['coal_co2', 'oil_co2', 'gas_co2'],
button_type='success'
)
continents_excl_world = ['Asia', 'Oceania', 'Europe', 'Africa', 'North America', 'South America', 'Antarctica']
co2_source_bar_pipeline = (
idf[
(idf.year == year_slider) &
(idf.country.isin(continents_excl_world))
]
.groupby(['year', 'country'])[yaxis_co2_source].sum()
.to_frame()
.reset_index()
.sort_values(by='year')
.reset_index(drop=True)
)
In [18]:
co2_source_bar_plot = co2_source_bar_pipeline.hvplot(kind='bar',
x='country',
y=yaxis_co2_source,
title='CO2 source by continent')
co2_source_bar_plot
Out[18]:
Creating Dashboard¶
In [ ]:
#Layout using Template
#before: template = pn.template.FastListTemplate(
template = pn.template.fast.list.FastListTemplate(
title='World CO2 emission dashboard',
sidebar=[pn.pane.Markdown("# CO2 Emissions and Climate Change"),
pn.pane.Markdown("#### Carbon dioxide emissions are the primary driver of global climate change. It’s widely recognised that to avoid the worst impacts of climate change, the world needs to urgently reduce emissions. But, how this responsibility is shared between regions, countries, and individuals has been an endless point of contention in international discussions."),
pn.pane.PNG('https://github.com/thu-vu92/python-dashboard-panel/raw/main/climate_day.png', sizing_mode='scale_both'),
pn.pane.Markdown("## Settings"),
year_slider],
main=[pn.Row(pn.Column(yaxis_co2,
co2_plot.panel(width=600), margin=(0,25)),
co2_table.panel(width=400)),
pn.Row(pn.Column(co2_vs_gdp_scatterplot.panel(width=600), margin=(0,25)),
pn.Column(yaxis_co2_source, co2_source_bar_plot.panel(width=500)))],
accent_base_color="#88d8b0",
header_background="#88d8b0",
)
# template.show()
template.servable()