diff --git a/app/layout_elements.py b/app/layout_elements.py new file mode 100644 index 00000000..3f6ea01a --- /dev/null +++ b/app/layout_elements.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +"""Layout elements that get reused in several tabs.""" +import pandas as pd +import streamlit as st + +from app.plot_functions import create_bar_chart_costs +from app.ptxboa_functions import config_number_columns + + +def display_costs(df_costs: pd.DataFrame, key: str, titlestring: str): + """Display costs as table and bar chart.""" + st.subheader(titlestring) + c1, c2 = st.columns([1, 5]) + with c1: + # filter data: + df_res = df_costs.copy() + + # select filter: + show_which_data = st.radio( + "Select elements to display:", + ["All", "Manual select"], + index=0, + key=f"show_which_data_{key}", + ) + + # apply filter: + if show_which_data == "Manual select": + ind_select = st.multiselect( + "Select regions:", + df_res.index.values, + default=df_res.index.values, + key=f"select_data_{key}", + ) + df_res = df_res.loc[ind_select] + + # sort: + sort_ascending = st.toggle( + "Sort by total costs?", value=True, key=f"sort_data_{key}" + ) + if sort_ascending: + df_res = df_res.sort_values(["Total"], ascending=True) + with c2: + # create graph: + fig = create_bar_chart_costs( + df_res, + current_selection=st.session_state[key], + ) + st.plotly_chart(fig, use_container_width=True) + + with st.expander("**Data**"): + column_config = config_number_columns( + df_res, format=f"%.1f {st.session_state['output_unit']}" + ) + st.dataframe(df_res, use_container_width=True, column_config=column_config) + return None diff --git a/app/tab_compare_costs.py b/app/tab_compare_costs.py index 1360051f..7a0e96a8 100644 --- a/app/tab_compare_costs.py +++ b/app/tab_compare_costs.py @@ -3,58 +3,11 @@ import pandas as pd import streamlit as st -from app.plot_functions import create_bar_chart_costs -from app.ptxboa_functions import config_number_columns, remove_subregions +from app.layout_elements import display_costs +from app.ptxboa_functions import remove_subregions from ptxboa.api import PtxboaAPI -def display_costs(df_costs: pd.DataFrame, key: str, titlestring: str): - """Display costs as table and bar chart.""" - st.subheader(titlestring) - c1, c2 = st.columns([1, 5]) - with c1: - # filter data: - df_res = df_costs.copy() - - # select filter: - show_which_data = st.radio( - "Select elements to display:", - ["All", "Manual select"], - index=0, - key=f"show_which_data_{key}", - ) - - # apply filter: - if show_which_data == "Manual select": - ind_select = st.multiselect( - "Select regions:", - df_res.index.values, - default=df_res.index.values, - key=f"select_data_{key}", - ) - df_res = df_res.loc[ind_select] - - # sort: - sort_ascending = st.toggle( - "Sort by total costs?", value=True, key=f"sort_data_{key}" - ) - if sort_ascending: - df_res = df_res.sort_values(["Total"], ascending=True) - with c2: - # create graph: - fig = create_bar_chart_costs( - df_res, - current_selection=st.session_state[key], - ) - st.plotly_chart(fig, use_container_width=True) - - with st.expander("**Data**"): - column_config = config_number_columns( - df_res, format=f"%.1f {st.session_state['output_unit']}" - ) - st.dataframe(df_res, use_container_width=True, column_config=column_config) - - def content_compare_costs( api: PtxboaAPI, costs_per_region: pd.DataFrame,