Hi there. I am new to Streamlit and I am trying to make an experimental webpage that will remember the name of the user. This will be done through an input form and a submit button. The name value will be stored with st.session_state. But then I want it to disable submit button, so users won’t be allowed to make a second input or a duplicate. But I am stuck. Any ideas?
That’s my code so far
Code snippet:
with st.form("myform"):
name=st.text_input("Enter your name below:")
submit_button=st.form_submit_button("Submit")
if submit_button:
st.info("You have entered: "+name)
if "my_input" not in st.session_state:
st.session_state["my_input"]=name
else:
st.session_state["my_input"]=name
Code works fine, but it allows duplicates and second inputs.
Expected behavior:
I want the submit button to be disabled after the very first input. If there is a way for the text to “freeze”, that would be awesome too!
on_click(callable): An optional callback invoked when this button is clicked.
disabled(bool): An optional boolean, which disables the button if set to True. The default is False. This argument can only be supplied by keyword.
We can make use of both of them to achieve the behavior you want. In this example:
We first initialize a session state variable called disabled to False and pass that to the form submit button’s disabled parameter.
Next, we define a function disable() that sets st.session_state.disabled to True.
Pass the disable function to the on_click parameter of the form submit button, so that when the button is clicked, the disable() function is executed, and that in-turn disables the button
Code
import streamlit as st
# Disable the submit button after it is clicked
def disable():
st.session_state.disabled = True
# Initialize disabled for form_submit_button to False
if "disabled" not in st.session_state:
st.session_state.disabled = False
with st.form("myform"):
# Assign a key to the widget so it's automatically in session state
name = st.text_input("Enter your name below:", key="name")
submit_button = st.form_submit_button(
"Submit", on_click=disable, disabled=st.session_state.disabled
)
if submit_button:
st.info("You have entered: " + st.session_state.name)
Thank you very much. That was very helpful. I finally used st.session_state the way you recommended but also at some other point in my code, because I needed the name input to be stored for further use in the page and in a local excel file. I had no clear understanding of that, but it works now. I then eliminated the initial blank inputs with a simple if statement. Here’s the code.
def disable():
st.session_state.disabled=True
if "disabled" not in st.session_state:
st.session_state.disabled=False
with st.form("myform"):
name=st.text_input("You only have one chance:",
key="name",placeholder="Enter your name here")
submit_button=st.form_submit_button("Submit",on_click=disable,
disabled=st.session_state.disabled)
if submit_button:
st.info("Username submited: "+st.session_state.name)
if "my_input" not in st.session_state:
st.session_state["my_input"]=name
if name!="":
vis=[name]
df=pd.DataFrame(vis)
with pd.ExcelWriter(visitors,mode="a",engine="openpyxl",
if_sheet_exists="overlay") as writer:
df.to_excel(writer,index=False,header=False,
startrow=writer.sheets["Sheet1"].max_row)
But this does not work for st_submit_buttons. The here presented way is ok, if you shall have only one chance. But it does not work, if the st_submit_button shall activated / deactivated by callback / key.
Is there any other possibility to control st_submit_button from anywhere of the code?
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.