# LIBRARY IMPORTS import sys import streamlit as st from streamlit import cli as stcli import plotly.express as px import pandas as pd # FUNCTIONS def data_import(): """ Imports 2019 U.S. state energy data from https://www.eia.gov/state/. """ df = pd.read_csv('SelectedStateRankingsData.csv') return df def data_transformation(df): """ Transforms the data to prepare for analysis and visualization. """ # Drop last column df_tr = df.iloc[:, :-1] # Get list of original column names you want to change col_names = list(df_tr.columns) col_names.remove('State') # Create list of revised column names col_names2 = [ 'Production (U.S. Share %)', 'Production (Rank)', 'Consumption per Capita (Million BTU)', 'Consumption per Capita (Rank)', 'Expenditures per Capita ($)', 'Expenditures per Capita (Rank)' ] # Create dictionary of original and revised column names col_names_new = dict(zip(col_names, col_names2)) # Rename columns using previously created dictionary df_tr = df_tr.rename(columns=col_names_new) return df_tr, col_names_new def display_header(): """ Displays the header section of the app. """ st.title("Energy Production, Consumption, & Expenditure By State") st.subheader("This app displays data mined from the EIA.gov website for 2019 energy production, consumption, " "and expenditure by state. Use the dropdown menu to filter the heatmap, which is interactive so you " "can hover over each state to view the filtered information.") st.write("A separate Qlik dashboard to explore this data can be viewed here: [link](https://yq99nw4o7qdge1s.us.qlikcloud.com/single/?appid=68edb70d-68b9-4050-b956-34c12bc2460c&sheet=37d34302-8565-4551-afd2-abd2dcffa243&theme=horizon&opt=ctxmenu,currsel&select=clearall)") def display_filter(col_names): # Take values from col_names dictionary and convert to a list col_names_list = list(col_names.values()) # Create dropdown menu to filter data for chart filter_selection = st.selectbox('Filter the data', col_names_list) return filter_selection # noinspection PyShadowingBuiltins def display_chart(df_tr, filter): """ Displays various charts of the data. """ # Determine filter value in order to set proper color scale for chart if 'rank' in filter.lower(): color_scale = 'plasma_r' else: color_scale = 'plasma' #te # Create chart fig = px.choropleth(df_tr, locations='State', color=filter, locationmode='USA-states', # Set to plot as US States title='2019 U.S. Energy {filter}'.format(filter=filter), color_continuous_scale=color_scale, ) # Add title and set USA as boundary for map fig.update_layout(title_x=0.4, geo_scope='usa', # Plot only the USA instead of globe height=800, width=1000 ) # Display chart using Streamlit st.plotly_chart(fig) def main(): """ Main function for the app which calls all other functions to display the app. """ # IMPORT DATA energy_rankings = data_import() # TRANSFORM DATA energy_rankings_tr, col_names_new = data_transformation(energy_rankings) # DISPLAY DATA display_header() filter_selection = display_filter(col_names_new) display_chart(energy_rankings_tr, filter_selection) if __name__ == '__main__': if st._is_running_with_streamlit: main() else: sys.argv = ['streamlit', 'run', sys.argv[0]] sys.exit(stcli.main())