Skip to content

Commit 164c0b4

Browse files
committed
added cached functions
1 parent f4e3876 commit 164c0b4

File tree

2 files changed

+185
-330
lines changed

2 files changed

+185
-330
lines changed

content/posts/renewables/world-energy/analysis.ipynb

+113-304
Large diffs are not rendered by default.

content/posts/renewables/world-energy/main.py

+72-26
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,65 @@ def load_data():
2323
'nuclear_electricity' : "Nuclear"
2424
}, inplace=True)
2525

26-
df.drop(columns=[])
26+
df = df[~df.country.isin([
27+
"G20 (Ember)",
28+
"OECD (Ember)",
29+
"Asia (Ember)",
30+
"G7 (Ember)",
31+
"OECD (EI)",
32+
"Asia Pacific (EI)",
33+
"High-income countries",
34+
"Europe (Ember)",
35+
"Non-OECD (EI)",
36+
"Asia",
37+
"North America (Ember)",
38+
"Upper-middle-income countries",
39+
"Europe (EI)",
40+
"Oceania (Ember)",
41+
"ASEAN (Ember)",
42+
"Lower-middle-income countries",
43+
"European Union (27)",
44+
"North America (EI)"
45+
])]
2746

2847
return df.sort_values('year')
2948

49+
50+
@st.cache_data
51+
def load_world_data(map_data):
52+
world = gpd.read_file("ne_110m_admin_0_countries/ne_110m_admin_0_countries.dbf")
53+
# data processing
54+
world.rename(columns={"ADMIN": "country"}, inplace=True)
55+
# replace country name to fit the other dataset
56+
world.country = world.country.replace(
57+
["United States of America", "Democratic Republic of the Congo", "Republic of the Congo", "United Republic of Tanzania", "The Bahamas", "Czechia", "eSwatini", "Republic of Serbia"],
58+
["United States", "Democratic Republic of Congo", "Congo", "Tanzania", "Bahamas", "Czechoslovakia", "Eswatini", "Serbia"]
59+
)
60+
61+
# world.SOV_A3 = world.SOV_A3.replace(["US1", "CH1", "FR1", "KA1", "GB1", "NZ1", "AU1"], ["USA", "CHN", "FRA", "KAZ", "GBR", "NZL", "AUS"])
62+
# latest_year = recent_data['year'].max() - pd.Timedelta(days=365*2)
63+
# latest_data = recent_data[recent_data['year'] == latest_year]
64+
world = world.merge(map_data, on=['country'])
65+
return world
66+
67+
68+
@st.cache_data
69+
def get_map_plot(energy_type_map, year_map):
70+
match energy_type_map:
71+
case "Solar":
72+
color_scale = "Oranges"
73+
case "Wind":
74+
color_scale = "Greens"
75+
case "Hydro":
76+
color_scale = "Blues"
77+
case _ :
78+
color_scale = "Viridis"
79+
80+
fig = px.choropleth(world, locations='ADM0_A3', color=energy_type_map,
81+
hover_name='country', projection='natural earth2', color_continuous_scale=color_scale,
82+
title=f'{energy_type_map.replace("_", " ").title()} Production in {year_map}')
83+
84+
return fig
3085
df = load_data()
3186

3287

@@ -41,7 +96,7 @@ def load_data():
4196

4297
# Filter data based on selected years
4398
filtered_df = df[(df['year'].dt.year >= start_year) & (df['year'].dt.year <= end_year)]
44-
# st.write(filtered_df)
99+
45100
# Global trend plot
46101
st.header('Global Energy Production Trends')
47102
energy_types = st.multiselect(
@@ -60,34 +115,23 @@ def load_data():
60115
# Map of energy production
61116
st.header('Energy Production Map')
62117
energy_type_map = st.selectbox('Select Energy Type for Map', energy_types)
63-
year_map = st.slider('Select Year for Map', start_year, end_year, end_year)
118+
year_map = st.slider('Select Year for Map', start_year, end_year, end_year-10)
64119

65-
map_data = filtered_df[filtered_df['year'].dt.year == year_map]
66-
world = gpd.read_file("ne_110m_admin_0_countries/ne_110m_admin_0_countries.dbf")
67-
# data processing
68-
world.rename(columns={"ADMIN": "country"}, inplace=True)
69-
# replace country name to fit the other dataset
70-
world.country = world.country.replace(
71-
["United States of America", "Democratic Republic of the Congo", "Republic of the Congo", "United Republic of Tanzania", "The Bahamas", "Czechia", "eSwatini", "Republic of Serbia"],
72-
["United States", "Democratic Republic of Congo", "Congo", "Tanzania", "Bahamas", "Czechoslovakia", "Eswatini", "Serbia"]
73-
)
74120

75-
# world.SOV_A3 = world.SOV_A3.replace(["US1", "CH1", "FR1", "KA1", "GB1", "NZ1", "AU1"], ["USA", "CHN", "FRA", "KAZ", "GBR", "NZL", "AUS"])
76-
# latest_year = recent_data['year'].max() - pd.Timedelta(days=365*2)
77-
# latest_data = recent_data[recent_data['year'] == latest_year]
78-
world = world.merge(map_data, on=['country'])
121+
map_data = filtered_df[filtered_df['year'].dt.year == year_map]
122+
world = load_world_data(map_data)
79123

80-
fig = px.choropleth(world, locations='ADM0_A3', color=energy_type_map,
81-
hover_name='country', projection='natural earth2', color_continuous_scale='Viridis',
82-
title=f'{energy_type_map.replace("_", " ").title()} Production in {year_map}')
83-
# fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
124+
fig = get_map_plot(energy_type_map, year_map)
125+
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
84126
st.plotly_chart(fig)
85127

86128
# Top countries comparison
87129
st.header('Top Countries Comparison')
88-
n_countries = st.slider('Number of top countries to compare', 1, 10, 5)
130+
n_countries = st.slider('Number of top countries to compare', 1, 20, 15)
89131
top_countries = filtered_df.groupby('country')[energy_types[0]].mean().nlargest(n_countries).index.tolist()
90132

133+
st.write(top_countries)
134+
91135
top_countries_data = filtered_df[filtered_df['country'].isin(top_countries)]
92136

93137
fig = px.line(top_countries_data, x='year', y=energy_types[0], color='country',
@@ -96,21 +140,23 @@ def load_data():
96140

97141
# Energy mix comparison
98142
st.header('Energy Mix Comparison')
99-
selected_country = st.selectbox('Select a Country', df['country'].unique(), index=None,
100-
placeholder="Select a country ...",)
143+
selected_country = st.selectbox('Select a Country', df['country'].unique(), index=None)
144+
if selected_country == None:
145+
selected_country = "Italy"
146+
101147
country_data = filtered_df[filtered_df['country'] == selected_country]
102-
st.write(country_data)
148+
# st.write(country_data)
103149

104150

105151
energy_mix = country_data[energy_types]
106-
# energy_mix = energy_mix.div(energy_mix.sum(axis=1), axis=0) * 100
107152
energy_mix['year'] = country_data.year
108153

109154
fig = px.area(energy_mix.dropna(), x='year', y=energy_types,
110155
title=f'Energy Mix for {selected_country}')
111156
fig.update_yaxes(title="Electricity Production (TWh)")
112157
st.plotly_chart(fig)
113-
st.write(energy_mix)
158+
# st.write(energy_mix)
159+
114160

115161
st.write("""
116162
This Streamlit app provides an interactive analysis of global energy production trends.

0 commit comments

Comments
 (0)