Skip to content

Commit

Permalink
Merge pull request agoenergy#195 from agoenergy/fix_index_names
Browse files Browse the repository at this point in the history
Fix index names
  • Loading branch information
markushal authored Dec 13, 2023
2 parents cf6e1dc + 521bfb5 commit 4ffaa1c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/layout_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import streamlit as st

from app.plot_functions import create_bar_chart_costs
from app.ptxboa_functions import config_number_columns
from app.ptxboa_functions import change_index_names, config_number_columns


def display_costs(
Expand Down Expand Up @@ -71,6 +71,9 @@ def display_costs(
if sort_ascending:
df_res = df_res.sort_values(["Total"], ascending=True)

# fix index names
change_index_names(df_res)

# create graph:
fig = create_bar_chart_costs(
df_res,
Expand Down
29 changes: 29 additions & 0 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ def display_and_edit_input_data(
missing_index_value = None
column_config = get_column_config()

df = change_index_names(df)

# if editing is enabled, store modifications in session_state:
if st.session_state["edit_input_data"]:
if f"{key}_number" not in st.session_state:
Expand Down Expand Up @@ -746,3 +748,30 @@ def get_column_config() -> dict:
),
}
return column_config


def change_index_names(df: pd.DataFrame, mapping: dict | None = None) -> pd.DataFrame:
"""
Change the index name of cost results or input data dataframes.
Only call this just befor you display any data, not before any transformation
or pivot actions.
https://stackoverflow.com/a/19851521
If mapping is None, default mappings for input_data and cost_results data is
used.
"""
if mapping is None:
mapping = {
"process_code": "Process",
"source_region_code": "Source Region",
"region": "Source Region",
"scenario": "Scenario",
"res_gen": "RE Source",
"chain": "Chain",
"flow_code": "Carrier/Material",
}
new_idx_names = [mapping.get(i, i) for i in df.index.names]
df.index.names = new_idx_names
return df
1 change: 1 addition & 0 deletions app/tab_deep_dive_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ def content_deep_dive_countries(
st.markdown("**Regional Distribution**")
with c_1_1:
fig = px.box(df)
fig.update_layout(xaxis_title=None)
st.plotly_chart(fig, use_container_width=True)
1 change: 1 addition & 0 deletions app/tab_input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def content_input_data(api: PtxboaAPI) -> None:
with c_1_1:
# create plot:
fig = px.box(df)
fig.update_layout(xaxis_title=None)
st.plotly_chart(fig, use_container_width=True)

with st.container(border=True):
Expand Down

0 comments on commit 4ffaa1c

Please sign in to comment.