St.stop and text_input

Hello! I have some doubts about the use of text_input and st.stop. I’ve created this method that works correctly when I use it alone, but as a part of a more complex code doesn’t work at all.

def misunderstanding(problem, example):
    correct = st.text_input(f"Introduce your {problem}: ", placeholder=f"{example}")
    if not correct:
        st.stop()
    st.write("the name is saved") #the code doesn't execute this
    st.session_state[problema] = correct

When I use it in the complex code, it gives me: st.session_state[problem]=“”, because when the text is written in the input it doesn’t continue with the part outside the if, anyone knows how can i fix it? Is it possible to continue the execution on this point instead of execute the all the code again after de stop? I think that is the problem.

Thank you so much.

1 Like

Thanks for posting, @saraibaallo!

With Streamlit, whenever the script reruns, all the variables and their states are reset unless they’re stored in st.session_state.

You may modify your code to check if st.session_state already contains the problem key. If not, it sets it to an empty string.

def misunderstanding(problem, example):
    if problem not in st.session_state:
        st.session_state[problem] = ''
        
    st.session_state[problem] = st.text_input(f"Introduce your {problem}: ", placeholder=f"{example}", value=st.session_state[problem])

    if st.session_state[problem] == '':
        st.stop()

    st.write("the name is saved")

Let me know how it goes.

Thanks,
Charly

Thanks @Charly_Wargnier for your answer! I think my problem is related to code rerun after the input. When the user types into the input box, the box dissapears but the variable still being “”, it doesn’t happen when I use it alone, but it is the result I get use it in a more complex code (I imagine that because the variable is not obtained, so it is still in the stop, but I doesn’t know why in this case the variable is not obtained as the one that the user writes) . I’m trying to use state variables to be in this method until I get a different solution for the input, but the problem continues.

Hi! I’ve solved my problem, I answer only in case there is somebody in the same situation.
The problem is not about the stop and the input text, the problem is because I used the input structure inside of a try structure. I don’t know why but this is a problem for Streamlit, using it outside everything works perfectly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.