I’m trying to create an app where I have to stock some text input in a list if it’s not aleady in this list.
I get the input text with a form and I have a callback function which check if the text is aleady is the list or not. If it’s not in the list, It add the text in it. But if the text is in the list I want to display an error message and it’s here that I have an issue : when I’m displaying the message, the form is no more accessible so we can’t update the input text…
To reproduce the issue :
Enter a text
Click the submit button twice
And a minimal example app to reproduce the issue.
import streamlit as st
st.title("Example")
def do_something():
if st.session_state["new_text"] in st.session_state["text"]:
st.error("This text already exist, please enter another text")
st.stop()
st.session_state["text"].append(st.session_state["new_text"])
if "text" not in st.session_state:
st.session_state["text"] = []
with st.form(key="form"):
st.text_input(label="Text", key="new_text")
st.form_submit_button(label="Submit", on_click=do_something)
st.write(st.session_state["text"])
I don’t know how to display an error message and let the form accessible so we can update the input text.
I’m using :
Streamlit 1.22.0
Python 3.10.10
So if someone have a idea, I would be glad to hear it !
Yes, I know but what I ask is : is there a properly way to check if an item exist in a list and if the item is already in the list display an error, do not add the item in and let the form accessible to be able to modify this item ?
def do_something():
if st.session_state["new_text"] in st.session_state["text"]:
st.error("This text already exist, please enter another text")
else:
st.session_state["text"].append(st.session_state["new_text"])
I thought about this solution but in my real project I have a lot of params to check, it will be a mess if I’ll do that…
So I found another solution, I don’t know if it’s the best but it do the work for the moment : instead of calling the function which add the text to my list, I call a function which check every error possible and stock the errors found. Then, after the form, I check if there are errors, if yes I display all the errors and if not I add the text in the list.
import streamlit as st
st.title("Example")
def check_error():
if st.session_state["new_text"] in st.session_state["text"]:
st.session_state["error"][
"already_in_list"
] = "This text already exist, please enter another text"
else:
st.session_state["error"]["already_in_list"] = None
if "text" not in st.session_state:
st.session_state["text"] = []
if "error" not in st.session_state:
st.session_state["error"] = {}
with st.form(key="form"):
st.text_input(label="Text", key="new_text")
st.form_submit_button(label="Submit", on_click=check_error)
if len(st.session_state["error"]) == 0:
st.stop()
if any(x != None for x in st.session_state["error"].values()):
for e in st.session_state["error"]:
st.error(e)
st.stop()
st.session_state["text"].append(st.session_state["new_text"])
st.write(st.session_state["text"])
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
Hello there 👋🏻
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.