Multipage Streamit - persistent state across pages
Thu Jan 19 2023 20:12:13 GMT+0000 (Coordinated Universal Time)
Saved by @tofufu #python #streamlit
import streamlit as st #app.py import streamlit as st import multipage_streamlit as mt import page_1, page_2, page_3_args st.write("Main page") num_pages = 3 # pages = ["Page 1", "Page 2"] pages = [] for i in range(1, num_pages + 1): pages.append("Page 3_" + str(i)) pages.insert(0, "Page 1") pages.insert(1, "Page 2") # Nav radios page = st.radio(label="Welcome, please select a calculator:", options=pages, horizontal=True) # st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True) # st.markdown("---") app = mt.MultiPage() # app.add("Page a1", page_1.app) # no parentheses or else it will run! # app.add("Page b2", page_2.app) # ^ditto for i in range(1, num_pages+1): app.add("Page 3." + str(i), page_3_args.app) # app.add("Page 1", page_1.app) # app.add("Page 2", page_2.app) # app.run_selectbox() # Nav between case pages if page == pages[0]: page_1.app() elif page == pages[1]: page_2.app() elif page == pages[2]: page_3_args.app(1) elif page == pages[3]: page_3_args.app(2) elif page == pages[4]: page_3_args.app(3) # Session states st.write(st.session_state) # st.write(st.session_state["_state_"]) # st.write(st.session_state["_state_"]["page_1:x"]) # st.write(st.session_state["_state_"].keys()) # st.write(st.session_state["_state_"].values()) # page_1.py import streamlit as st from multipage_streamlit import State # import page_1, page_2 def app(): # state = State("sharing") # state = State(__name__) st.header("Page 1") # # if "page_1" not in st.session_state: # # st.session_state["page_1"] = 9 # # p1_text = st.number_input(label="Sharing state", value=st.session_state["page_1"], key="page_1") # p1_text = st.number_input(label="Sharing state", key=state("x", 9)) # st.write(st.session_state[__name__ + ":x"]) # st.number_input(label="Testing state", value=st.session_state[__name__ + ":x"]) # # st.number_input(label="Testing state", key=st.session_state["_state_"][__name__ + ":x"]) # st.write("\_state_") # # st.number_input(label="Testing state", value=st.session_state[__name__ + ":x"], key=state("x", 3)) st.number_input(label="Testing shared state across pages page 1", key="testing") # # st.write(st.session_state["page_1"]) # # st.write(st.session_state["page_2"]) # state.save() return # page_2.py import streamlit as st from multipage_streamlit import State def app(): # state = State("sharing") # state = State(__name__) st.header("Page 2") # # p1_text = st.number_input(label="Sharing state", value=st.session_state["page_1"], key="page_1") # p2_text = st.number_input(label="Indiv state", key=state("x", 1)) # # p1_text = st.number_input(label="Sharing state", key=st.session_state[["_state_"]["page_1:x"]]) # # st.write(__name__ + ":x") # # st.write(st.session_state[__name__ + ":x"]) # # st.write(st.session_state) # st.write(state("x", 1)) # # st.write(st.session_state["page_1"]) st.number_input(label="Testing shared state across pages page 2", key="testing") # # if "page_2" not in st.session_state: # # st.session_state["page_2"] = 12 # # p2_text = st.number_input(label="Own state", value=st.session_state["page_2"], key="page_2") # # st.write(st.session_state["page_2"]) # state.save() return import streamlit as st from multipage_streamlit import State def app(case_x): state = State(__name__) st.header("Page 3") p3_text = st.number_input(label="Indiv state", key=state(str(case_x), 20 + case_x)) p3_selbox = st.selectbox(label="Indiv state", options=[21, 22, 23], key=state(str(case_x) + "_select", 20 + case_x)) state.save() return
Comments