Ejemplo n.º 1
0
_educational purposes only. A 5yr rolling mean transformation has been applied to the original data series values_
_and so is still representative of actual level values. Please do not redistribute this data without the express_
_permission of the owner, Oxford Economics._
'''

st.sidebar.header("Settings")
region_type = st.sidebar.selectbox('Select region type',
                                   ['Cities', 'Countries', 'Both'])
top_n = st.sidebar.selectbox('Select group',
                             ['All', 'Top 100', 'Top 50', 'Top 20'])

# GET DATA
gc_data = load_data(region_type)
geojson = load_geojson()

gc_data_unpivoted, merged = clean_and_reshape_data(gc_data, geojson)

# >> DISPLAY WIDGETS <<

# FILTER TO SELECTED INDICATOR & YEAR
year = st.sidebar.slider(
    'Select year', min_value=2015, max_value=2022, value=2020
)  # data starts at 2000, but selection is from 2010 because of 10 yr GAGR
indicators_list = list(
    merged.loc[:, 'Indicator'].sort_values(ascending=True).unique())
indicator = st.sidebar.selectbox('Select indicator', indicators_list)
merged_filtered_view = merged.loc[(merged.loc[:, 'Indicator'] == indicator)
                                  & (gc_data_unpivoted.loc[:, 'Year'] == year)]
# FILTER TO SELECTED MEASUREMENT
measurements_list = list(merged_filtered_view.loc[:,
                                                  'Measurement'].sort_values(
Ejemplo n.º 2
0
def main():
    st.sidebar.header("Settings")

    # GET DATA
    data = load_data()
    data_unpivoted = clean_and_reshape_data(data)

    # >> DISPLAY WIDGETS <<

    # FILTER TO SELECTED LOCATION & YEAR
    year = st.sidebar.slider('Select year',
                             min_value=2015,
                             max_value=2022,
                             value=2020)
    locations_list = list(
        data_unpivoted.loc[:, 'Location'].sort_values(ascending=True).unique())
    location = st.sidebar.selectbox(
        'Select location',
        locations_list,
        index=locations_list.index('United Kingdom'))

    data_view = data_unpivoted.loc[(data_unpivoted.loc[:,
                                                       'Location'] == location)
                                   & (data_unpivoted.loc[:, 'Year'] == year)]

    if st.checkbox('Show data', False):
        '''
        ### Data

        _The sample data used in this application is property of Oxford Economics and provided for personal use and_
        _educational purposes only. A 5yr rolling mean transformation has been applied to the original data series values_
        _and so is still representative of actual level values. Please do not redistribute this data without the express_
        _permission of the owner, Oxford Economics._
        '''
        # TABLE
        if st.checkbox('Show DataFrame', True):
            data_view

        if st.checkbox('Show Table'):
            st.table(data_view)
    '''
    ### Chart
    '''
    # chart data is calculated in two steps
    # step 1 (using data_unpivoted, filtered by location)
    chart_data = data_unpivoted[(data_unpivoted['Location'] == location)]

    indicators_list = list(
        chart_data.loc[:, 'Indicator'].sort_values(ascending=True).unique())
    # this selection box is put into the reserved widget slot created above
    indicator = st.sidebar.selectbox('Select indicator', indicators_list)

    # step 2 (using chart_data, filtered by location's indicators)
    chart_data = chart_data[(chart_data['Indicator'] == indicator)]

    fig = alt.Chart(chart_data, title=f'{location} | {indicator}').mark_bar().encode(
    alt.X('Year:O', axis=alt.Axis(domain=False, tickSize=0)),
    alt.Y('Value', axis=alt.Axis(domain=False, tickSize=0, title='Value')),
        color='Value', tooltip=['Id','Year','Value']) \
        .properties(width=600).interactive()
    st.altair_chart(fig)

    # ABOUT
    st.sidebar.header('About')
    st.sidebar.info('Using Streamlit to build a Web App.\n\n' + \
        '(c) 2020. Oxford Economics Ltd. All rights reserved.')
    st.sidebar.markdown('---')

    # Display Readme.md
    if st.sidebar.checkbox('Readme', False):
        st.markdown('---')
        '''
        ### Readme
        '''
        with open('./README.md', 'r', encoding='utf-8') as f:
            readme = f.read()
            st.markdown(readme)

    # TESTS
    if st.sidebar.checkbox('Run Tests', False):
        st.markdown('---')
        st.title('Test Suite')
        '''
        ### Data Load Test
        '''
        suite = unittest.TestLoader().loadTestsFromModule(TestFixtures)
        result = unittest.TextTestRunner(verbosity=2).run(suite)
        if result.wasSuccessful():
            st.info(f'Test PASSED :-)')
            st.balloons()
        else:
            st.error(f'Test FAILED :-(')

    # Style
    st.sidebar.markdown('---')
    if st.sidebar.checkbox('Configure Style'):
        BlockContainerStyler().block_container_styler()