I’m trying to keep the selection in my st.selectbox
accross pages.
When returning to the first (main) page, the selection must be set as before.
Bellow is a simplifyied code: I still have a jump between to choices when I change the selection.
Does anybody have an idea? I’m using Streamlit 1.37.
<code>import streamlit as st
if __name__ == "__main__":
TITLE = "Demo Streamlit App"
st.set_page_config(layout="wide")
st.sidebar.markdown("Home")
st.title(TITLE)
# initialize session state
ss = st.session_state
ss.setdefault("execution", {})
if "first_execution" not in ss.execution:
ss.execution["first_execution"] = True
if "edition_mode" not in st.session_state:
st.session_state.edition_mode = "New script" # default value
form1, _ = st.columns([1, 1])
with form1:
choices = [
"New script",
"Import script",
"Load a script",
"Delete a script",
]
# Edition mode
selected_mode = st.selectbox(
label="Edition mode : ",
options=choices,
index=choices.index(st.session_state.edition_mode),
)
st.session_state.edition_mode = selected_mode
st.write("---")
st.write("---")
### Switch to the right app
if ss.execution.get("switch_page"):
st.switch_page("pages/01_Editor.py")
</code>
<code>import streamlit as st
if __name__ == "__main__":
TITLE = "Demo Streamlit App"
st.set_page_config(layout="wide")
st.sidebar.markdown("Home")
st.title(TITLE)
# initialize session state
ss = st.session_state
ss.setdefault("execution", {})
if "first_execution" not in ss.execution:
ss.execution["first_execution"] = True
if "edition_mode" not in st.session_state:
st.session_state.edition_mode = "New script" # default value
form1, _ = st.columns([1, 1])
with form1:
choices = [
"New script",
"Import script",
"Load a script",
"Delete a script",
]
# Edition mode
selected_mode = st.selectbox(
label="Edition mode : ",
options=choices,
index=choices.index(st.session_state.edition_mode),
)
st.session_state.edition_mode = selected_mode
st.write("---")
st.write("---")
### Switch to the right app
if ss.execution.get("switch_page"):
st.switch_page("pages/01_Editor.py")
</code>
import streamlit as st
if __name__ == "__main__":
TITLE = "Demo Streamlit App"
st.set_page_config(layout="wide")
st.sidebar.markdown("Home")
st.title(TITLE)
# initialize session state
ss = st.session_state
ss.setdefault("execution", {})
if "first_execution" not in ss.execution:
ss.execution["first_execution"] = True
if "edition_mode" not in st.session_state:
st.session_state.edition_mode = "New script" # default value
form1, _ = st.columns([1, 1])
with form1:
choices = [
"New script",
"Import script",
"Load a script",
"Delete a script",
]
# Edition mode
selected_mode = st.selectbox(
label="Edition mode : ",
options=choices,
index=choices.index(st.session_state.edition_mode),
)
st.session_state.edition_mode = selected_mode
st.write("---")
st.write("---")
### Switch to the right app
if ss.execution.get("switch_page"):
st.switch_page("pages/01_Editor.py")
Thanks !