How to disable text input after user finish input?

how to disable text input after user finish input?
now use disabled=True will make input unavailable before user input something.
Thank you.

Anyone have solution for this requirement?
Thank you.

Sorry, your question is not clear to me.

Am I understanding correctly:
a) Your have a text input widget
b) once the user enters something in the input then you want the text input widget to disappear?

My suggestion is this

  1. Make an empty widget with st.empty
  2. In the empty widget you just made, create an st.form
  3. In the form you just made, include a st.text_input widget where the user can enter text.
  4. In the form you made, you have to include a button. When the button become โ€œsubmittedโ€ turn your form back into a nice message using something like st.write

So the basic idea is you go from

mywidget = st.empty โ†’ (mywidget = st.form + st.text.input) โ†’ mywidget = st.write

Does that make sense?

1 Like

Hereโ€™s an alternative to using forms and st.empty:

Solution

import streamlit as st

if "disabled" not in st.session_state:
    st.session_state["disabled"] = False

def disable():
    st.session_state["disabled"] = True

st.text_input(
    "Enter some text", 
    disabled=st.session_state.disabled, 
    on_change=disable
)

Output

text-input-disable

Best, :balloon:
Snehan

4 Likes

Thank you very much.

Thank you! But is there a way of, except for re-loading the site, to enable the text again?

You can manually set st.session_state["disabled"] = False somewhere in your code, or define an enable function if you want to set it as a callback to some other action.

def enable():
    st.session_state["disabled"] = False
1 Like

Thank you a lot for your very good - and fast response! :smiley:

1 Like

Hi @mathcatsand
I set st.session_state.disabled to False in the last line of my script. However, the text widget is still disbaled. Can you give some hints?

If youโ€™ve set it to False at the end of your script, then anything prior would not see that new value. Whatever value your widget function sees in its disabled parameter (at the moment the widget function is executed) is all that it will know. The value in session state would need to be False before passing that value to your widget on your last rerun.

Thanks for your reply. I solved my problem by calling experimental_rerun in the end, such that the input area can be re-enabled automatically.

1 Like