diff --git a/app/ptxboa_functions.py b/app/ptxboa_functions.py index 418eb613..b921b807 100644 --- a/app/ptxboa_functions.py +++ b/app/ptxboa_functions.py @@ -607,3 +607,18 @@ def display_and_edit_input_data( ) return df + + +def move_to_tab(tab_name): + """ + Move to a certain tab within a callback. + + Increment the session state variable "tab_key" by 1. + + Parameters + ---------- + tab_name : str + """ + old_tab_key_nb = int(st.session_state["tab_key"].replace("tab_key_", "")) + st.session_state["tab_key"] = f"tab_key_{old_tab_key_nb + 1}" + st.session_state[st.session_state["tab_key"]] = tab_name diff --git a/app/tab_dashboard.py b/app/tab_dashboard.py index a4b42313..dc8569ca 100644 --- a/app/tab_dashboard.py +++ b/app/tab_dashboard.py @@ -10,37 +10,16 @@ create_box_plot, plot_costs_on_map, ) -from app.ptxboa_functions import remove_subregions +from app.ptxboa_functions import move_to_tab, remove_subregions from ptxboa.api import PtxboaAPI -def _create_infobox(context_data: dict): - data = context_data["infobox"] - st.markdown(f"**Key information on {st.session_state['country']}:**") - demand = data.at[st.session_state["country"], "Projected H2 demand [2030]"] - info1 = data.at[st.session_state["country"], "key_info_1"] - info2 = data.at[st.session_state["country"], "key_info_2"] - info3 = data.at[st.session_state["country"], "key_info_3"] - info4 = data.at[st.session_state["country"], "key_info_4"] - st.markdown(f"* Projected H2 demand in 2030: {demand}") - - def write_info(info): - if isinstance(info, str): - st.markdown(f"* {info}") - - write_info(info1) - write_info(info2) - write_info(info3) - write_info(info4) - - def content_dashboard( api: PtxboaAPI, costs_per_region: pd.DataFrame, costs_per_scenario: pd.DataFrame, costs_per_res_gen: pd.DataFrame, costs_per_chain: pd.DataFrame, - context_data: dict, ): with st.expander("What is this?"): st.markdown( @@ -84,7 +63,11 @@ def content_dashboard( doublefig.update_layout(title_text="Cost distribution and details:") st.plotly_chart(doublefig, use_container_width=True) - _create_infobox(context_data) + st.button( + "More Info on Supply Region and Demand Country", + on_click=move_to_tab, + args=("Country fact sheets",), + ) display_costs( remove_subregions(api, costs_per_region, st.session_state["country"]), diff --git a/ptxboa_streamlit.py b/ptxboa_streamlit.py index 883078fc..342ea150 100644 --- a/ptxboa_streamlit.py +++ b/ptxboa_streamlit.py @@ -5,6 +5,7 @@ import pandas as pd import streamlit as st +import streamlit_antd_components as sac import app.ptxboa_functions as pf from app.context_data import load_context_data @@ -68,28 +69,35 @@ else: placeholder = st.empty() -( - t_dashboard, - t_market_scanning, - t_input_data, - t_deep_dive_countries, - t_country_fact_sheets, - t_certification_schemes, - t_sustainability, - t_literature, - t_disclaimer, -) = st.tabs( - [ - "Dashboard", - "Market scanning", - "Input data", - "Deep-dive countries", - "Country fact sheets", - "Certification schemes", - "Sustainability", - "Literature", - "Disclaimer", - ] +tabs = ( + "Dashboard", + "Market scanning", + "Input data", + "Deep-dive countries", + "Country fact sheets", + "Certification schemes", + "Sustainability", + "Literature", + "Disclaimer", +) + +# the "tab_key" is used to identify the sac.tabs element. Whenever a tab is switched +# programatically (e.g. via app.ptxboa.functions.move_to_tab), the "tab_key" entry is +# incremented by 1. This allows us to set the programatically set tab as the default +# `index` in `sac.tabs()`. +if "tab_key" not in st.session_state: + st.session_state["tab_key"] = "tab_key_0" + +# initializing "tab at first round +if st.session_state["tab_key"] not in st.session_state: + st.session_state[st.session_state["tab_key"]] = "Dashboard" + +sac.tabs( + [sac.TabsItem(label=i) for i in tabs], + index=tabs.index(st.session_state[st.session_state["tab_key"]]), + format_func="title", + align="center", + key=st.session_state["tab_key"], ) # create sidebar: @@ -130,36 +138,35 @@ cd = load_context_data() # dashboard: -with t_dashboard: +if st.session_state[st.session_state["tab_key"]] == "Dashboard": content_dashboard( api, costs_per_region=costs_per_region, costs_per_scenario=costs_per_scenario, costs_per_res_gen=costs_per_res_gen, costs_per_chain=costs_per_chain, - context_data=cd, ) -with t_market_scanning: +if st.session_state[st.session_state["tab_key"]] == "Market scanning": content_market_scanning(api, costs_per_region) -with t_input_data: +if st.session_state[st.session_state["tab_key"]] == "Input data": content_input_data(api) -with t_deep_dive_countries: +if st.session_state[st.session_state["tab_key"]] == "Deep-dive countries": content_deep_dive_countries(api, costs_per_region) -with t_country_fact_sheets: +if st.session_state[st.session_state["tab_key"]] == "Country fact sheets": content_country_fact_sheets(cd) -with t_certification_schemes: +if st.session_state[st.session_state["tab_key"]] == "Certification schemes": content_certification_schemes(cd) -with t_sustainability: +if st.session_state[st.session_state["tab_key"]] == "Sustainability": content_sustainability(cd) -with t_literature: +if st.session_state[st.session_state["tab_key"]] == "Literature": content_literature(cd) -with t_disclaimer: +if st.session_state[st.session_state["tab_key"]] == "Disclaimer": content_disclaimer() diff --git a/requirements.txt b/requirements.txt index dd92c153..3b2e0837 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ streamlit>=1.28 openpyxl~=3.1 plotly~=5.16 faker~=18.9 +streamlit-antd-components