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