Managing error in callback function with form

Hello everyone !

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 :

  1. Enter a text
  2. 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. :confused:

I’m using :

  • Streamlit 1.22.0
  • Python 3.10.10

So if someone have a idea, I would be glad to hear it ! :smiley:

Thanks for your help ! :heart:

Calling st.stop() after displaying the error prevents the rest of the code from running.

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 ?

That is what else is for.

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"])

Thanks for your reply :slight_smile:

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