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