Hey All.
The solution to do multi-step forms in Streamlit works really well, thank you Profile - blackary - Streamlit for setting this up. But additional question:
How can we access values that are set through the different forms steps?
For instance in one of the steps we do a call to OpenAI to generate something based on a previous value that has been set in a previous step.
We also want to use that OpenAI Response in a later step.
How would we implement this? Sorry if it’s obvious but I tried a couple of things and it looks like the form is scoping the variables…is this correct?
Because I would expect the below to set the session_state[“name”] and be able to access it later on. But- it does not work like this?
The code I have so far below:
import streamlit as st
if "name" not in st.session_state:
st.session_state["name"] = ""
def goto_level_3():
st.session_state["stage"] = "level_3"
def goto_level_2():
st.session_state["stage"] = "level_2"
def goto_level_1():
st.session_state["stage"] = "level_1"
if "stage" not in st.session_state:
st.session_state["stage"] = "level_3"
if st.session_state["stage"] == "level_3":
with st.form("level_3_form"):
st.caption("Level 3")
name = st.text_input("name", st.session_state['name'] )
st.session_state['name'] = name
submitted = st.form_submit_button(
"Generate level 2", on_click=goto_level_2
)
if st.session_state["stage"] == "level_2":
with st.form("level_2_form"):
st.caption("Level 2")
st.write(st.session_state['name'])
b = st.text_input("lastname")
submitted = st.form_submit_button(
"Generate level 1", on_click=goto_level_1
)
if st.session_state["stage"] == "level_1":
with st.form("level_1_form"):
st.caption("Level 1")
submitted = st.form_submit_button(
"Back to 3", on_click=goto_level_3
)
Thank you so much