I am building a streamlit app. I want to add a button where every time a user clicks it, a new form will appear without erasing the old form.
For example, the user will click the button 3 times; therefore, 3 forms will appear and he can fill each one of them separately.
The button contain conditions and different forms can appear depending on some things he selected earlier in the app but that doesn't matter yet, i need the general concept to make this work.
def AddLayer(l):
if l == "one":
with st.form("formm"):
st.subheader("example")
test = st.number_input("Just an example")
submitted = st.form_submit_button("Save")
if submitted:
st.write("saved!")
elif l == "two":
...
choices = [" ", "one", "two", "three"]
layer = st.selectbox("Choose Layer type: ", choices)
if st.button("Add"):
AddLayer(layer)
The code explains the concept but it doesn't work, it just erases the old form to output a new one, but I want the one generated before to stay.
Thank you.
Working with classes is a good solution, i tryed this : (in this example everytime i choose "one" and clic add, a new form will pop up without deleting the old ones) Upvote if you see it a useful solution
if "layers" not in st.session_state:
st.session_state.layers = []
class Layer:
def show(self, n):
name = "L" + str(n)
with st.form(name):
st.subheader("layer")
test = st.number_input("name")
submitted = st.form_submit_button("Save")
if submitted:
st.write("saved!")
choices = [" ", "one", "two", "three"]
layer = st.selectbox("Choose Layer type: ", choices)
if st.button("Add"):
if layer == "one":
st.session_state["i"]+= 1
st.session_state.layers.append(Layer())
st.session_state.layers
for i in range(len(st.session_state.layers)):
st.session_state.layers[i].show(i)
If you have better solution or ideas please let me know.