Skip to content

Commit

Permalink
Merge pull request #159 from agoenergy/antd_tabs
Browse files Browse the repository at this point in the history
remove infobox and add option switch between tabs programatically
  • Loading branch information
markushal authored Dec 7, 2023
2 parents faf7c07 + afdc7df commit 51694e2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
15 changes: 15 additions & 0 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 6 additions & 23 deletions app/tab_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"]),
Expand Down
71 changes: 39 additions & 32 deletions ptxboa_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,28 +77,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:
Expand Down Expand Up @@ -138,36 +146,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()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ streamlit>=1.28
openpyxl~=3.1
plotly~=5.16
faker~=18.9
streamlit-antd-components

0 comments on commit 51694e2

Please sign in to comment.