Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into 86-implement-compa…
Browse files Browse the repository at this point in the history
…rison-sheets
  • Loading branch information
markushal committed Nov 13, 2023
2 parents d727fc4 + 7bbcfaa commit 410825d
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ def content_market_scanning(

# show data in tabular form:
st.markdown("**Data:**")
st.dataframe(df_plot, use_container_width=True)
column_config = config_number_columns(df_plot, format="%.1f")
st.dataframe(df_plot, use_container_width=True, column_config=column_config)


def remove_subregions(api: PtxboaAPI, df: pd.DataFrame, settings: dict):
Expand Down Expand Up @@ -678,7 +679,10 @@ def display_costs(df_costs: pd.DataFrame, titlestring: str, settings: dict):
create_bar_chart_costs(df_res, settings)

with st.expander("**Data**"):
st.dataframe(df_res.style.format(precision=1), use_container_width=True)
column_config = config_number_columns(
df_res, format=f"%.1f {settings['output_unit']}"
)
st.dataframe(df_res, use_container_width=True, column_config=column_config)

res_costs_without_subregions = remove_subregions(api, res_costs, settings)
display_costs(res_costs_without_subregions, "Costs by region:", settings)
Expand Down Expand Up @@ -764,6 +768,7 @@ def content_deep_dive_countries(
"Wind-PV-Hybrid",
]
x = "process_code"
column_config = {"format": "%.0f h/a", "min_value": 0, "max_value": 8760}

if data_selection == "total costs":
df = res_costs.copy()
Expand All @@ -782,6 +787,7 @@ def content_deep_dive_countries(
source_region_code=region_list,
parameter_code=parameter_code,
process_code=process_code,
column_config=column_config,
key_suffix="_ddc",
)
with c1:
Expand Down Expand Up @@ -844,6 +850,8 @@ def content_input_data(api: PtxboaAPI, settings: dict) -> None:
"Wind-PV-Hybrid",
]
x = "process_code"
column_config = {"format": "%.0f USD/kW", "min_value": 0}

if data_selection == "full load hours":
parameter_code = ["full load hours"]
process_code = [
Expand All @@ -853,10 +861,13 @@ def content_input_data(api: PtxboaAPI, settings: dict) -> None:
"Wind-PV-Hybrid",
]
x = "process_code"
column_config = {"format": "%.0f h/a", "min_value": 0, "max_value": 8760}

if data_selection == "interest rate":
parameter_code = ["interest rate"]
process_code = [""]
x = "parameter_code"
column_config = {"format": "%.3f", "min_value": 0, "max_value": 1}

c1, c2 = st.columns(2, gap="medium")
with c2:
Expand All @@ -868,7 +879,9 @@ def content_input_data(api: PtxboaAPI, settings: dict) -> None:
source_region_code=region_list_without_subregions,
parameter_code=parameter_code,
process_code=process_code,
column_config=column_config,
)

with c1:
# create plot:
st.markdown("**Figure:**")
Expand All @@ -889,7 +902,7 @@ def display_user_changes():
"""Display input data changes made by user."""
if "user_changes_df" in st.session_state.keys():
st.write("**Input data has been modified:**")
st.write(st.session_state["user_changes_df"])
st.dataframe(st.session_state["user_changes_df"].style.format(precision=3))


def display_and_edit_data_table(
Expand All @@ -900,6 +913,7 @@ def display_and_edit_data_table(
index: str = "source_region_code",
columns: str = "process_code",
values: str = "value",
column_config: dict = None,
key_suffix: str = "",
) -> pd.DataFrame:
"""Display selected input data as 2D table, which can also be edited."""
Expand All @@ -909,19 +923,31 @@ def display_and_edit_data_table(
df = input_data.loc[ind1 & ind2 & ind3]
df_tab = df.pivot_table(index=index, columns=columns, values=values, aggfunc="sum")

# if editing is enabled, store modifications in session_state:
if st.session_state["edit_input_data"]:
disabled = [index]
key = f"edit_input_data_{parameter_code}{key_suffix}"
else:
disabled = True
key = None

# configure columns for display:
if column_config is None:
column_config_all = None
else:
column_config_all = config_number_columns(df_tab, **column_config)

# display data:
st.data_editor(
df_tab,
use_container_width=True,
key=key,
num_rows="fixed",
disabled=disabled,
column_config=column_config_all,
)
if st.session_state["edit_input_data"]:
st.markdown("You can edit data directly in the table!")

# store changes in session_state:
if st.session_state["edit_input_data"]:
Expand Down Expand Up @@ -1326,3 +1352,14 @@ def content_disclaimer():
)
st.image("static/disclaimer.png")
st.image("static/disclaimer_2.png")


def config_number_columns(df: pd.DataFrame, **kwargs) -> {}:
"""Create number column config info for st.dataframe() or st.data_editor."""
column_config_all = {}
for c in df.columns:
column_config_all[c] = st.column_config.NumberColumn(
**kwargs,
)

return column_config_all

0 comments on commit 410825d

Please sign in to comment.