Multipage streamlit
Wed Jan 11 2023 16:30:17 GMT+0000 (Coordinated Universal Time)
Saved by
@tofufu
#python
#streamlit
import streamlit as st
import multipage_streamlit as mt
# Check preserved session state if you need to replace value with user input or material has changed with the multipage ses state library
def old_persistent_ses_state(old_extracted_val_ses, current_extracted_val_ses, extracted_input):
""" Determines if preserved inputs can have its value replaced or not, via user input or material change
Args:
old_extracted_val_ses (string): session state of old input value to determine if value gets replaced or not
current_extracted_val_ses (string): session state of current input value
extracted_input (int): input value for current input
"""
if old_extracted_val_ses in ses and ses[old_extracted_val_ses]!=extracted_input and current_extracted_val_ses in ses: ses[current_extracted_val_ses] = extracted_input
return
# app.py
app = mt.MultiPage()
app.add("Page 1", page_1.app) # no parentheses or else it will run even when not selected!
app.add("Page 2", page_2.app)
# page_1.py
def app():
select = st.selectbox(label="Input 1 Page 1", options=[1, 2, 3], key=state("input_1_page_1")) # don't need to init session state, will default to first item/0 for num_input
value = st.number_input(label="Input 2 Page 1", key=state("input_2_page_1", select))
# Save old session state to prevent overwriting
content_copyCOPY
Modified for horizontal "tabbed" navigation
https://github.com/crxi/multipage_streamlit
Comments