Streamit dynamic radio tabs based on selection

PHOTO EMBED

Fri Jan 13 2023 17:53:12 GMT+0000 (Coordinated Universal Time)

Saved by @tofufu #python #streamlit

# Num cases + in session state
num_cases = st.selectbox('Select the number of cases for comparison, maximum of 12', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], key="num_cases")


# Nav options: dynamically append/remove # of cases menu bar has
case_pages = {}
for x in range(1, st.session_state["num_cases"] + 1): case_pages["Case " + str(x)] = x

# "Slice" dictionary according to input for # of cases + case titles
# One-liner for iterating through dictionary for num_cases times to show both the case and the summary page names in the navigation "tabs"
case_pages_sliced = dict(itertools.islice(case_pages.items(), 0, st.session_state["num_cases"]))

# Add "Summary" option at end of options list
case_pages_sliced["Summary"] = 0
content_copyCOPY

We put it in a separate dictionary because we need to check if the user has selected a case or the summary. The case_n function takes in the case number as the argument while the summary function has no arguments. Plus, this will help make sure the app navigates to the right page.