How to clear the value from a text_area after a button is pushed

If you’re creating a debugging post, please include the following info:

  1. Are you running your app locally or is it deployed? Local
  2. If your app is deployed:
    a. Is it deployed on Community Cloud or another hosting platform? Local
    b. Share the link to the public deployed app. n/a
  3. Share the link to your app’s public GitHub repository (including a requirements file).
  4. Share the full text of the error message (not a screenshot). No errors
  5. Share the Streamlit and Python versions. Streamlit: 1.32.2; Python 3.12.2

Here is a code chunk showing what I am trying to do:

import streamlit as st

st.title("Testing")

# Initialize promptinputcheckbox
if "promptinputcheckbox" not in st.session_state:
    st.session_state["promptinputcheckbox"] = False

# Initialize additionalpromptinfo
if "additionalpromptinfo" not in st.session_state:
    st.session_state["additionalpromptinfo"] = None

# Set up the sidebar
with st.sidebar:
    print(f"BEGIN sidebar")
    promptinputcheckbox = st.checkbox("Do you want to include additional prompt input?")
    st.session_state["promptinputcheckbox"] = promptinputcheckbox
    additionalpromptinfo = st.text_area(
        "Additional Prompt Information", "", disabled=not st.session_state["promptinputcheckbox"])
    st.session_state["additionalpromptinfo"] = additionalpromptinfo

    startbutton = st.button("Start Process", type="primary")
    if startbutton:
        print(f"startbutton: {startbutton}")
        st.session_state['currentlyprocessing'] = True

    stopbutton = st.button("Stop Process")
    if stopbutton:
        print(f"stopbutton: {stopbutton}")
        st.session_state['currentlyprocessing'] = False
        st.rerun()
    print(f"END sidebar")

After the stopbutton is pressed I would like to clear the text_area of all input data. How do I do that?

try this:

if "msg" not in st.session_state:
    st.session_state["msg"] = ''

if stopbutton:
        print(f"stopbutton: {stopbutton}")
        st.session_state['currentlyprocessing'] = False
        st.session_state["msg"] = ''


additionalpromptinfo = st.text_area(
        "Additional Prompt Information", st.session_state["msg"], 
        disabled=not st.session_state["promptinputcheckbox"])

Here is one approach, use the on_click parameter of the button to call a function. There are some comments in the stop_process_cb()

import streamlit as st

st.title("Testing")

# Initialize promptinputcheckbox
if "promptinputcheckbox" not in st.session_state:
    st.session_state["promptinputcheckbox"] = False

# Initialize additionalpromptinfo
if "additionalpromptinfo" not in st.session_state:
    st.session_state["additionalpromptinfo"] = None


def stop_process_cb():
    """This callback function is called first after the Stop Process button is clicked.

    After a user clicked the button, streamlit then goes to the top of
    the script to do its routine that is, rerun the code from top to bottom.
    But hold on, any statements of this function has to be executed first.

    The advantage is that we can define/update other states in advance.

    Note this one is wrong:
    if st.button("Stop Process"):
        st.session_state.ta = ''

    We will get an exception:
        StreamlitAPIException: st.session_state.ta cannot be modified after the widget with key ta is instantiated.
    """
    # Clears the text area via the "ta" key of the text_area widget.
    st.session_state.ta = ''

    # Update other object state.
    st.session_state['currentlyprocessing'] = False

    # Done, streamlit goes to the top of the script reading:
    # import streamlit as st
    # and so on, going down.


# Set up the sidebar
with st.sidebar:
    print(f"BEGIN sidebar")
    promptinputcheckbox = st.checkbox("Do you want to include additional prompt input?")
    st.session_state["promptinputcheckbox"] = promptinputcheckbox

    # additionalpromptinfo = st.text_area(
    #     "Additional Prompt Information", "", disabled=not st.session_state["promptinputcheckbox"])
    
    additionalpromptinfo = st.text_area(
        "Additional Prompt Information",
        "",
        disabled=not st.session_state["promptinputcheckbox"],
        key='ta'  # <============================================ define a key
    )
    
    st.session_state["additionalpromptinfo"] = additionalpromptinfo

    startbutton = st.button("Start Process", type="primary")
    if startbutton:
        print(f"startbutton: {startbutton}")
        st.session_state['currentlyprocessing'] = True

    # stopbutton = st.button("Stop Process")
    st.button("Stop Process", on_click=stop_process_cb)
        
    # if stopbutton:
    #     print(f"stopbutton: {stopbutton}")
    #     st.session_state['currentlyprocessing'] = False
    #     st.rerun()
    print(f"END sidebar")

2 Likes

@Siming_Yan and @ferdy I want to thank you both for the prompt response. I went with @ferdy approach.

You are both awesome for helping me out.