Skip to content

Commit

Permalink
Merge pull request #77 from agoenergy/70-improve-number-format-in-dat…
Browse files Browse the repository at this point in the history
…a-tables

improve number format in data tables (#70)
  • Loading branch information
markushal authored Nov 13, 2023
2 parents cd62050 + 08c6ab7 commit 7bbcfaa
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,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 @@ -616,7 +617,11 @@ def content_costs_by_region(
create_bar_chart_costs(df_res)

st.write("**Data:**")
st.dataframe(df_res, 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)


def content_deep_dive_countries(
Expand Down Expand Up @@ -684,6 +689,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 @@ -702,6 +708,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 @@ -764,6 +771,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 @@ -773,10 +782,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 @@ -788,7 +800,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 @@ -809,7 +823,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 @@ -820,6 +834,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 @@ -829,19 +844,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 @@ -1246,3 +1273,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 7bbcfaa

Please sign in to comment.