Exercise 2
Exercise: Total Emissions and Average Temperature
In this exercise, we will create visualizations using Streamlit to analyze the total emissions and average temperature for a specific country over a selected range of years. We will display key metrics such as total emissions, average temperature, and their percentage changes. Additionally, we will create a line plot to visualize the trends in emissions and temperature over time.
Problem Description:
You have been provided with a dataset that contains information about carbon emissions and average temperature for different countries over several years. Your task is to create visualizations that showcase the total emissions and average temperature for a selected country over a specific range of years. Additionally, you will display the percentage change in emissions and temperature compared to the previous year.
Instructions:
Follow the steps below to complete the exercise:
Start by adding a title to your Streamlit app using
st.title()
and set it to "Total Emissions and Average Temperature 🚀".Define the name of the table that contains the emission and temperature data. Use the variable
emission_temp
and set it to the appropriate table name, such as "EXERCISE_CO2_VS_TEMPERATURE.CARBON_EMISSIONS.AGGREGATE_COUNTRY_EMISSIONS_TEMPERATURES".Write a query to retrieve the necessary data from the specified table. The query should select the country, year, per capita emissions, share of global emissions, total emissions, and average temperature. Store the retrieved data in a variable named
emission_temperature
.Create a pandas DataFrame named
filtered_data
to store the emission and temperature data for the selected country. Use thest.selectbox()
function to allow the user to select a country from the available options. Filter theemission_temperature
DataFrame based on the selected country and store the filtered data infiltered_data
.Implement a slider using
st.slider()
to allow the user to select a range of years. Set the minimum and maximum values to the minimum and maximum years available in thefiltered_data
DataFrame. Set the default value to the minimum and maximum years. Store the selected year range in a variable namedyear_range
.Retrieve the data for the selected year from the
filtered_data
DataFrame. Create a new DataFrame namedselected_year_data
that contains only the rows where the 'YEAR' column matches the maximum year in theyear_range
. Extract the values for the average temperature and total emissions from this DataFrame and store them in the variablesaverage_temperature
andtotal_emissions
, respectively.Retrieve the data for the previous year from the
filtered_data
DataFrame. Create a new DataFrame namedprevious_year_data
that contains only the rows where the 'YEAR' column matches the maximum year minus 1. Extract the value for total emissions and average temperature from this DataFrame and store them in the variablesprevious_year_emissions
andprevious_year_temperature
, respectively.Calculate the percentage change in emissions and temperature compared to the previous year. Store the calculated values in the variables
percentage_change_emission
andpercentage_change_temperature
, respectively.Implement the
display_line_plot()
function. This function will use Plotly Express to create a line plot. Pass thefiltered_data
DataFrame along with the appropriate column names and titles to the function. Adjust the width of the plot to ensure proper display.Use the
col1.metric()
,col2.metric()
, andcol3.metric()
functions to display the key metrics: year, total emissions, average temperature, and their respective percentage changes. Adjust the formatting and labels to provide clear information.Run your Streamlit app and test the functionality. Select different countries, year ranges, and observe how the metrics