Streamlit按钮,每次点击生成新表单。

huangapple go评论90阅读模式
英文:

Streamlit button to generate a new form at every click

问题

我正在构建一个Streamlit应用程序。我想添加一个按钮,每当用户点击它时,都会出现一个新的表单,而不会删除旧的表单。

例如,用户会点击按钮3次;因此,会出现3个表单,他可以分别填写每个表单。

按钮包含条件,根据用户之前在应用程序中选择的某些内容,可能会出现不同的表单,但现在这并不重要,我需要一般的概念来使这个工作。

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)

这段代码解释了概念,但它不起作用,它只会擦除旧的表单以输出一个新的表单,但我希望之前生成的表单保持不变。

谢谢。

英文:

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.

答案1

得分: 1

使用类是一个不错的解决方案,我尝试了这样做:
(在这个示例中,每当我选择"one"并单击"Add"时,会弹出一个新的表单,而不会删除旧的表单)
如果您认为这是一个有用的解决方案,请点赞。

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)

如果您有更好的解决方案或想法,请告诉我。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 03:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定