Spaces:
Running
Running
covid and policy
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
-
import
|
5 |
from raceplotly.plots import barplot
|
6 |
|
|
|
|
|
7 |
# Configuration and Constants
|
8 |
COUNTRY_MAPPING = {
|
9 |
"Italy, San Marino and the Holy See": "Italy",
|
@@ -17,27 +19,19 @@ COUNTRY_MAPPING = {
|
|
17 |
|
18 |
DEFAULT_COUNTRIES = ["Italy", "France", "Germany"]
|
19 |
YEAR_RANGE = (2000, 2020)
|
20 |
-
SQLITE_DB_PATH = "data.db" # Path to the SQLite database
|
21 |
|
22 |
# Data Loading and Processing Functions
|
23 |
@st.cache_data
|
24 |
-
def load_data(
|
25 |
-
|
26 |
-
conn = sqlite3.connect(SQLITE_DB_PATH)
|
27 |
-
query = f"SELECT * FROM '{table_name}'"
|
28 |
-
df = pd.read_sql(query, conn)
|
29 |
-
conn.close()
|
30 |
-
|
31 |
-
# Replace country names according to the mapping
|
32 |
df_mapped = df.copy()
|
33 |
df_mapped['Country'] = df_mapped['Country'].replace(COUNTRY_MAPPING)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
selected_cols = ['Country'] + year_cols
|
41 |
return df_mapped[selected_cols].copy()
|
42 |
|
43 |
def process_data_for_line_plot(df, selected_countries, year_range):
|
@@ -46,7 +40,7 @@ def process_data_for_line_plot(df, selected_countries, year_range):
|
|
46 |
|
47 |
df_melted = filtered_df.melt(
|
48 |
id_vars=['Country'],
|
49 |
-
value_vars=
|
50 |
var_name='Year',
|
51 |
value_name='Emissions'
|
52 |
)
|
@@ -70,7 +64,7 @@ def create_line_plot(data):
|
|
70 |
def create_animated_choropleth(data, start_year, end_year):
|
71 |
df_map = data.melt(
|
72 |
id_vars=['Country'],
|
73 |
-
value_vars=
|
74 |
var_name='Year',
|
75 |
value_name='Emissions'
|
76 |
)
|
@@ -83,7 +77,7 @@ def create_animated_choropleth(data, start_year, end_year):
|
|
83 |
animation_frame='Year',
|
84 |
title='CO2 Emissions per Capita Over Time',
|
85 |
color_continuous_scale='Reds',
|
86 |
-
range_color=[0,
|
87 |
labels={'Emissions': 'CO2 Emissions per Capita (Mton)'}
|
88 |
)
|
89 |
|
@@ -97,27 +91,29 @@ def create_animated_choropleth(data, start_year, end_year):
|
|
97 |
dict(label='Play',
|
98 |
method='animate',
|
99 |
args=[None, {'frame': {'duration': 500, 'redraw': True},
|
100 |
-
|
101 |
dict(label='Pause',
|
102 |
method='animate',
|
103 |
args=[[None], {'frame': {'duration': 0, 'redraw': False},
|
104 |
-
|
105 |
-
|
106 |
]
|
107 |
}]
|
108 |
)
|
109 |
return fig_map
|
110 |
|
|
|
111 |
def create_race_plot(df, year_range):
|
112 |
# Prepare data for race plot
|
|
|
113 |
df_race = df.melt(
|
114 |
id_vars=['Country'],
|
115 |
-
value_vars=
|
116 |
var_name='Year',
|
117 |
value_name='Emissions'
|
118 |
)
|
119 |
|
120 |
-
# Create the race plot
|
121 |
race_plot = barplot(
|
122 |
df_race,
|
123 |
item_column='Country',
|
@@ -126,6 +122,7 @@ def create_race_plot(df, year_range):
|
|
126 |
top_entries=10,
|
127 |
)
|
128 |
|
|
|
129 |
fig = race_plot.plot(
|
130 |
title='Top 10 Countries by CO2 Emissions',
|
131 |
orientation='horizontal',
|
@@ -134,28 +131,156 @@ def create_race_plot(df, year_range):
|
|
134 |
time_label='Year: ',
|
135 |
frame_duration=800
|
136 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
return fig
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
# Main App Function
|
141 |
def main():
|
142 |
st.set_page_config(page_title="CO2 Emissions Dashboard", layout="wide")
|
143 |
st.title("Global CO2 Emissions Dashboard")
|
144 |
|
145 |
-
# Load Data
|
146 |
df1 = load_data("fossil_CO2_per_capita_by_countr")
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
150 |
|
151 |
# Sidebar Controls
|
|
|
|
|
|
|
152 |
st.sidebar.image("dati/SIAM-logo.jpg", width=150)
|
|
|
153 |
visualization_type = st.sidebar.radio(
|
154 |
"Choose Visualization",
|
155 |
-
["Time Series Plot", "Animated World Map", "Bar Chart Race"]
|
156 |
)
|
157 |
|
158 |
-
# Year range selector (common to
|
159 |
year_range = st.sidebar.slider(
|
160 |
"Select Year Range",
|
161 |
min_value=YEAR_RANGE[0],
|
@@ -163,8 +288,9 @@ def main():
|
|
163 |
value=YEAR_RANGE
|
164 |
)
|
165 |
|
|
|
166 |
if visualization_type == "Time Series Plot":
|
167 |
-
st.subheader("CO2
|
168 |
|
169 |
# Show country selector only for time series
|
170 |
countries = df1['Country'].unique().tolist()
|
@@ -183,13 +309,54 @@ def main():
|
|
183 |
st.subheader("Global Emissions Map (Animated)")
|
184 |
fig_map = create_animated_choropleth(df1, year_range[0], year_range[1])
|
185 |
st.plotly_chart(fig_map, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
else:
|
187 |
st.subheader("Top 10 CO2 Emitters Race")
|
188 |
-
fig_race = create_race_plot(
|
189 |
st.plotly_chart(fig_race, use_container_width=True)
|
190 |
|
191 |
st.sidebar.markdown("---")
|
192 |
-
st.sidebar.markdown("""GRUPPO 5 (EMANUELA, FULVIO, MARCO, TINSAE)""")
|
193 |
|
194 |
if __name__ == "__main__":
|
195 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
+
#import bar_chart_race as bcr
|
5 |
from raceplotly.plots import barplot
|
6 |
|
7 |
+
|
8 |
+
|
9 |
# Configuration and Constants
|
10 |
COUNTRY_MAPPING = {
|
11 |
"Italy, San Marino and the Holy See": "Italy",
|
|
|
19 |
|
20 |
DEFAULT_COUNTRIES = ["Italy", "France", "Germany"]
|
21 |
YEAR_RANGE = (2000, 2020)
|
|
|
22 |
|
23 |
# Data Loading and Processing Functions
|
24 |
@st.cache_data
|
25 |
+
def load_data(sheet_name, year_range = (2000, 2020), sector=None):
|
26 |
+
df = pd.read_excel("dati/fossilco2emission.xlsx", sheet_name=sheet_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
df_mapped = df.copy()
|
28 |
df_mapped['Country'] = df_mapped['Country'].replace(COUNTRY_MAPPING)
|
29 |
|
30 |
+
year_cols = list(range(year_range[0], year_range[1] + 1))
|
31 |
+
if sector:
|
32 |
+
selected_cols = ['Country', 'Sector'] + year_cols
|
33 |
+
else:
|
34 |
+
selected_cols = ['Country'] + year_cols
|
|
|
35 |
return df_mapped[selected_cols].copy()
|
36 |
|
37 |
def process_data_for_line_plot(df, selected_countries, year_range):
|
|
|
40 |
|
41 |
df_melted = filtered_df.melt(
|
42 |
id_vars=['Country'],
|
43 |
+
value_vars=range(year_range[0], year_range[1] + 1),
|
44 |
var_name='Year',
|
45 |
value_name='Emissions'
|
46 |
)
|
|
|
64 |
def create_animated_choropleth(data, start_year, end_year):
|
65 |
df_map = data.melt(
|
66 |
id_vars=['Country'],
|
67 |
+
value_vars=range(start_year, end_year + 1),
|
68 |
var_name='Year',
|
69 |
value_name='Emissions'
|
70 |
)
|
|
|
77 |
animation_frame='Year',
|
78 |
title='CO2 Emissions per Capita Over Time',
|
79 |
color_continuous_scale='Reds',
|
80 |
+
range_color=[0, df_map['Emissions'].quantile(0.95)],
|
81 |
labels={'Emissions': 'CO2 Emissions per Capita (Mton)'}
|
82 |
)
|
83 |
|
|
|
91 |
dict(label='Play',
|
92 |
method='animate',
|
93 |
args=[None, {'frame': {'duration': 500, 'redraw': True},
|
94 |
+
'fromcurrent': True}]),
|
95 |
dict(label='Pause',
|
96 |
method='animate',
|
97 |
args=[[None], {'frame': {'duration': 0, 'redraw': False},
|
98 |
+
'mode': 'immediate',
|
99 |
+
'transition': {'duration': 0}}])
|
100 |
]
|
101 |
}]
|
102 |
)
|
103 |
return fig_map
|
104 |
|
105 |
+
|
106 |
def create_race_plot(df, year_range):
|
107 |
# Prepare data for race plot
|
108 |
+
# Convert year columns to rows for raceplotly format
|
109 |
df_race = df.melt(
|
110 |
id_vars=['Country'],
|
111 |
+
value_vars=range(year_range[0], year_range[1] + 1),
|
112 |
var_name='Year',
|
113 |
value_name='Emissions'
|
114 |
)
|
115 |
|
116 |
+
# Create the race plot
|
117 |
race_plot = barplot(
|
118 |
df_race,
|
119 |
item_column='Country',
|
|
|
122 |
top_entries=10,
|
123 |
)
|
124 |
|
125 |
+
# Plot with custom settings
|
126 |
fig = race_plot.plot(
|
127 |
title='Top 10 Countries by CO2 Emissions',
|
128 |
orientation='horizontal',
|
|
|
131 |
time_label='Year: ',
|
132 |
frame_duration=800
|
133 |
)
|
134 |
+
|
135 |
+
# fig.update_layout(
|
136 |
+
# height=700, # Make plot taller
|
137 |
+
# font=dict(size=12), # Increase base font size
|
138 |
+
# title_font_size=20, # Larger title
|
139 |
+
# xaxis_title_font_size=16, # Larger axis titles
|
140 |
+
# yaxis_title_font_size=16,
|
141 |
+
# yaxis_tickfont_size=14, # Larger tick labels
|
142 |
+
# xaxis_tickfont_size=14
|
143 |
+
# )
|
144 |
|
145 |
return fig
|
146 |
|
147 |
+
def create_covid_impact_plot(df1, df2):
|
148 |
+
# Create tabs for different COVID analyses
|
149 |
+
tab1, tab2 = st.tabs(["Global Impact", "Sectoral Impact"])
|
150 |
+
print(df2.head())
|
151 |
+
|
152 |
+
with tab1:
|
153 |
+
# Global emissions around COVID
|
154 |
+
years_covid = range(2017, 2023)
|
155 |
+
global_emissions = df1[df1['Country'] == 'GLOBAL TOTAL']
|
156 |
+
emissions_covid = global_emissions[list(years_covid)].values[0]
|
157 |
+
|
158 |
+
fig_global = px.line(
|
159 |
+
x=years_covid,
|
160 |
+
y=emissions_covid,
|
161 |
+
title='Global CO2 Emissions Around COVID-19',
|
162 |
+
labels={'x': 'Year', 'y': 'CO2 Emissions (Mt CO2)'}
|
163 |
+
)
|
164 |
+
fig_global.add_vline(x=2020, line_dash="dash", line_color="red",
|
165 |
+
annotation_text="COVID-19")
|
166 |
+
st.plotly_chart(fig_global, use_container_width=True)
|
167 |
+
|
168 |
+
# Calculate and display percentage changes
|
169 |
+
col1, col2 = st.columns(2)
|
170 |
+
with col1:
|
171 |
+
change_2020 = ((emissions_covid[3] - emissions_covid[2])/emissions_covid[2]*100)
|
172 |
+
st.metric("2020 Emissions Change", f"{change_2020:.1f}%")
|
173 |
+
with col2:
|
174 |
+
change_2021 = ((emissions_covid[4] - emissions_covid[3])/emissions_covid[3]*100)
|
175 |
+
st.metric("2021 Recovery", f"{change_2021:.1f}%")
|
176 |
+
|
177 |
+
with tab2:
|
178 |
+
# Sectoral analysis
|
179 |
+
sectors = ['Power Industry', 'Industrial Combustion', 'Transport', 'Processes']
|
180 |
+
sector_data = {}
|
181 |
+
|
182 |
+
for sector in sectors:
|
183 |
+
sector_emissions = df2[(df2['Sector'] == sector) &
|
184 |
+
(df2['Country'] == 'GLOBAL TOTAL')]
|
185 |
+
sector_data[sector] = sector_emissions[list(years_covid)].values[0]
|
186 |
+
|
187 |
+
# Create DataFrame for plotly
|
188 |
+
df_sectors = pd.DataFrame(sector_data, index=years_covid).reset_index()
|
189 |
+
df_sectors_melted = df_sectors.melt('index', var_name='Sector',
|
190 |
+
value_name='Emissions')
|
191 |
+
|
192 |
+
fig_sectors = px.line(
|
193 |
+
df_sectors_melted,
|
194 |
+
x='index',
|
195 |
+
y='Emissions',
|
196 |
+
color='Sector',
|
197 |
+
title='CO2 Emissions by Sector Around COVID-19'
|
198 |
+
)
|
199 |
+
fig_sectors.add_vline(x=2020, line_dash="dash", line_color="red",
|
200 |
+
annotation_text="COVID-19")
|
201 |
+
st.plotly_chart(fig_sectors, use_container_width=True)
|
202 |
+
|
203 |
+
# Display sector-specific impacts
|
204 |
+
st.subheader("Sector Impact (2019-2020)")
|
205 |
+
cols = st.columns(len(sectors))
|
206 |
+
for i, sector in enumerate(sectors):
|
207 |
+
change = ((sector_data[sector][3] - sector_data[sector][2])/
|
208 |
+
sector_data[sector][2]*100)
|
209 |
+
cols[i].metric(sector, f"{change:.1f}%")
|
210 |
+
|
211 |
+
def create_agreements_timeline(df1):
|
212 |
+
# Get the global total data
|
213 |
+
global_data = df1[df1['Country'] == 'GLOBAL TOTAL'].iloc[0]
|
214 |
+
|
215 |
+
# Get only the year columns (1970 to 2023)
|
216 |
+
year_columns = [col for col in df1.columns if str(col).isdigit()]
|
217 |
+
years = [int(col) for col in year_columns]
|
218 |
+
values = [global_data[year] for year in years]
|
219 |
+
|
220 |
+
# Create the plot using plotly
|
221 |
+
fig = px.line(
|
222 |
+
x=years,
|
223 |
+
y=values,
|
224 |
+
title='Global CO2 Emissions and Key Climate Agreements',
|
225 |
+
labels={'x': 'Year', 'y': 'CO2 Emissions (Mt CO2)'}
|
226 |
+
)
|
227 |
+
|
228 |
+
# Add vertical lines for key agreements
|
229 |
+
agreements = {
|
230 |
+
1997: 'Kyoto Protocol Adopted',
|
231 |
+
2005: 'Kyoto Protocol Enforced',
|
232 |
+
2015: 'Paris Agreement'
|
233 |
+
}
|
234 |
+
|
235 |
+
colors = {'1997': 'red', '2005': 'green', '2015': 'orange'}
|
236 |
+
|
237 |
+
for year, agreement in agreements.items():
|
238 |
+
fig.add_vline(
|
239 |
+
x=year,
|
240 |
+
line_dash="dash",
|
241 |
+
line_color=colors[str(year)],
|
242 |
+
annotation_text=agreement,
|
243 |
+
annotation_position="top"
|
244 |
+
)
|
245 |
+
|
246 |
+
# Customize layout
|
247 |
+
fig.update_layout(
|
248 |
+
hovermode='x unified',
|
249 |
+
showlegend=False,
|
250 |
+
height=600
|
251 |
+
)
|
252 |
+
|
253 |
+
return fig
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
# Main App Function
|
258 |
def main():
|
259 |
st.set_page_config(page_title="CO2 Emissions Dashboard", layout="wide")
|
260 |
st.title("Global CO2 Emissions Dashboard")
|
261 |
|
262 |
+
# Load Data
|
263 |
df1 = load_data("fossil_CO2_per_capita_by_countr")
|
264 |
+
df2 = load_data("fossil_CO2_totals_by_country")
|
265 |
+
df3 = load_data("fossil_CO2_by_sector_country_su", year_range=(2017, 2023), sector="Sector")
|
266 |
+
df_covid = load_data("fossil_CO2_totals_by_country", year_range=(2017, 2023))
|
267 |
+
df_poliicy = load_data("fossil_CO2_totals_by_country", year_range=(1970,2023))
|
268 |
+
|
269 |
+
df2 = df2[df2['Country'] != 'International Shipping']
|
270 |
+
df_only_countries = df2.copy()[:210]
|
271 |
|
272 |
# Sidebar Controls
|
273 |
+
#st.sidebar.header("Controls")
|
274 |
+
|
275 |
+
#st.sidebar.markdown("---")
|
276 |
st.sidebar.image("dati/SIAM-logo.jpg", width=150)
|
277 |
+
|
278 |
visualization_type = st.sidebar.radio(
|
279 |
"Choose Visualization",
|
280 |
+
["Time Series Plot", "Animated World Map", "Bar Chart Race", "COVID-19 Impact", "Climate Agreements Timeline"]
|
281 |
)
|
282 |
|
283 |
+
# Year range selector (common to both visualizations)
|
284 |
year_range = st.sidebar.slider(
|
285 |
"Select Year Range",
|
286 |
min_value=YEAR_RANGE[0],
|
|
|
288 |
value=YEAR_RANGE
|
289 |
)
|
290 |
|
291 |
+
# Conditional controls and display
|
292 |
if visualization_type == "Time Series Plot":
|
293 |
+
st.subheader("CO2 Emissions Time Series")
|
294 |
|
295 |
# Show country selector only for time series
|
296 |
countries = df1['Country'].unique().tolist()
|
|
|
309 |
st.subheader("Global Emissions Map (Animated)")
|
310 |
fig_map = create_animated_choropleth(df1, year_range[0], year_range[1])
|
311 |
st.plotly_chart(fig_map, use_container_width=True)
|
312 |
+
|
313 |
+
elif visualization_type == "Climate Agreements Timeline":
|
314 |
+
st.subheader("Global Emissions and Climate Agreements")
|
315 |
+
|
316 |
+
# Add some context about the agreements
|
317 |
+
with st.expander("About the Climate Agreements"):
|
318 |
+
st.markdown("""
|
319 |
+
- **Kyoto Protocol (1997)**: First legally binding agreement to reduce greenhouse gases
|
320 |
+
- **Kyoto Protocol Enforcement (2005)**: The protocol came into force
|
321 |
+
- **Paris Agreement (2015)**: Global agreement to limit temperature rise to well below 2°C
|
322 |
+
""")
|
323 |
+
|
324 |
+
# Create and display the plot
|
325 |
+
fig = create_agreements_timeline(df_poliicy)
|
326 |
+
st.plotly_chart(fig, use_container_width=True)
|
327 |
+
|
328 |
+
# Add some analysis
|
329 |
+
st.markdown("### Key Observations")
|
330 |
+
col1, col2, col3 = st.columns(3)
|
331 |
+
|
332 |
+
# Calculate some metrics
|
333 |
+
kyoto_change = ((float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][2005]) -
|
334 |
+
float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][1997])) /
|
335 |
+
float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][1997]) * 100)
|
336 |
+
|
337 |
+
paris_change = ((float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][2023]) -
|
338 |
+
float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][2015])) /
|
339 |
+
float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][2015]) * 100)
|
340 |
+
|
341 |
+
with col1:
|
342 |
+
st.metric("Emissions Change 1997-2005", f"{kyoto_change:.1f}%")
|
343 |
+
with col2:
|
344 |
+
st.metric("Emissions Change 2015-2023", f"{paris_change:.1f}%")
|
345 |
+
with col3:
|
346 |
+
latest_emissions = float(df_poliicy[df_poliicy['Country'] == 'GLOBAL TOTAL'][2023])
|
347 |
+
st.metric("Current Emissions (Mt CO2)", f"{latest_emissions:.1f}")
|
348 |
+
|
349 |
+
elif visualization_type == "COVID-19 Impact":
|
350 |
+
st.subheader("COVID-19 Impact Analysis")
|
351 |
+
create_covid_impact_plot(df_covid, df3)
|
352 |
+
|
353 |
else:
|
354 |
st.subheader("Top 10 CO2 Emitters Race")
|
355 |
+
fig_race = create_race_plot(df_only_countries, year_range)
|
356 |
st.plotly_chart(fig_race, use_container_width=True)
|
357 |
|
358 |
st.sidebar.markdown("---")
|
359 |
+
st.sidebar.markdown(""" GRUPPO 5 (EMANUELA, FULVIO, MARCO, TINSAE) """)
|
360 |
|
361 |
if __name__ == "__main__":
|
362 |
+
main()
|
data.db
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8352a360b4a80aa7a63b498b8c9e1e57b3e3663f6bf86b59b3f9cdd25a30103e
|
3 |
+
size 1150976
|