Python Streamlit 侧边栏在输入时重置

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

python streamlit sidebar resetting on entry of input

问题

你的代码中存在一个问题,导致在输入 input1 后重置到 step 1。问题出现在以下部分:

if reset:
    placeholder = st.empty()

在你点击 "Reset" 按钮后,它会创建一个空白的 placeholder,但没有其他逻辑来保持用户在第二步。你需要在这里添加一些代码,以确保用户在第二步时不会重置到第一步。具体的解决方案可能会依赖于你的应用的需求,但你可以考虑使用状态变量或条件来控制用户界面的状态,以确保用户能够顺利完成第二步操作。

如果你需要更具体的帮助,可以提供更多关于你的应用和预期行为的信息。

英文:

I am creating a streamlit app where on the sidebar I have first a file uploader and a button. Once the file is uploaded and then the button is pressed, it shows more input option and two more buttons for user to finally sumbit. When I am at the second step, after user enters the input, it resets the sidebard to go back to step 1 and earlier hidden options go back to hidden.

Here is my code:

    file = st.sidebar.file_uploader('Upload',type=["pdf","txt"])
    #this is step1
    if file is not None:

        # Save uploaded file to 'F:/tmp' folder.
        save_folder = 'F:/tmp'
        save_path = Path(save_folder, file.name)
        with open(save_path, mode='wb') as w:
            w.write(uploaded_file.getvalue())

        with st.sidebar:
            if save_path.exists():
                st.write("File uploaded")

    co1, co2= st.sidebar.columns([1, 1])
    
    submit = co1.button("Submit", use_container_width=True)

    if submit:
        input1, input2, input3 = compo() # local function which sets streamlit sidebar inputs
        st.sidebar.markdown("---")
        c1,c2 = st.sidebar.columns([1,1])
        reset = c1.button("Reset", use_container_width=True)
        fin_submit = c2.button("Final Submit", use_container_width=True)
        
        #When I enter input1 and then go click to enter input2, it resets to step 1

        if reset:
            placeholder = st.empty()
            

        if fin_submit:

            if file is not None:
                
                if (input1 == "Hello") or (input2 == "There"):
                    placeholder.empty()
                    # Call processng function and display result
                else:
                    placeholder.empty()
                    st.error("Sorry!")

            else:
                if (input1 == "You") or (input2 == "there"):

                    
                    placeholder.empty()
                    # Call processng function and display result
                else:
                    placeholder.empty()
                    st.error("Sorry!")

Here is the code for compo():

def compo():

    
    c1,c2 = st.sidebar.columns(2)
    input1 = c1.selectbox(label = "Input 1",options = ['Hello',"You"])
    input2 = c2.selectbox(label = "Input 2", options = ["There","there"])
    input3 = st.sidebar.slider("Number",min_value=1, max_value=10, value=1, step=1)
    st.sidebar. markdown("---")
    return input1, input2, input3

I don't know why as I enter a value in input1, it resets to step1

答案1

得分: 1

按钮不会保留状态。它们在页面加载时因点击而返回True,然后立即变为False。如果你将某些内容嵌套在按钮内,它将在用户执行操作后消失(因为页面重新加载,按钮变为false)。

参见这个博客帖子中的第一个观点。

这里有另一个类似问题的答案

要嵌套按钮,你可以通过回调函数来跟踪一个布尔标志,将其设置为True:

import streamlit as st

if 'submitted' not in st.session_state:
    st.session_state.submitted = False

def submit():
    st.session_state.submitted = True

st.button('提交', on_click=submit)

if st.session_state.submitted:
    st.write('提交按钮已被点击。')
    st.button('什么也不做')

由于'什么也不做'按钮是基于会话状态中的'submitted'值而不是直接基于'提交'按钮的输出来条件控制的,因此当被点击时它会保留在屏幕上。

英文:

Buttons do not retain state. They return True on the page load resulting from their click and then immediately go back to False. If you nest something inside a button, it will go away as soon as the user does something (because the page reloads and the button becomes false).

See the first point in this blog post.

Here's another answer for a similar question.

To nest buttons, you can track a boolean flag that gets set to True by a callback function:

import streamlit as st

if 'submitted' not in st.session_state:
    st.session_state.submitted = False

def submit():
    st.session_state.submitted = True

st.button('Submit', on_click=submit)

if st.session_state.submitted:
    st.write('The submit button was clicked.')
    st.button('Do nothing')

Since the 'Do Nothing' button is conditioned on the 'submitted' value in session state instead of directly on the output of the 'Submit' button, it will stay on the screen when clicked.

huangapple
  • 本文由 发表于 2023年5月22日 13:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303248.html
匿名

发表评论

匿名网友

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

确定