Skip to content

Commit

Permalink
move display_costs function to layout elements module
Browse files Browse the repository at this point in the history
  • Loading branch information
joAschauer committed Nov 23, 2023
1 parent 2d13085 commit 96bc380
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 49 deletions.
55 changes: 55 additions & 0 deletions app/layout_elements.py
Original file line number Diff line number Diff line change
@@ -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
51 changes: 2 additions & 49 deletions app/tab_compare_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 96bc380

Please sign in to comment.