File size: 3,816 Bytes
2abcd22
 
0920b76
2abcd22
 
78ab3eb
2abcd22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc7910
2abcd22
 
 
 
 
 
 
 
 
 
 
 
b4e4c1d
2abcd22
 
 
 
b4e4c1d
 
 
 
 
 
 
2abcd22
 
 
 
 
b4e4c1d
2abcd22
 
 
b4e4c1d
2abcd22
b4e4c1d
 
2abcd22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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())