I am currently working on a chatbot and runnning a streamlit locally on my macos system.
I want to clear the input_text once submit is pressed by the user and found this code on the discussion. here
after implemennting on my work:
my code:
def submit():
st.session_state.something = st.session_state.widget
st.session_state.widget = ''
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "How may I help you today?"}
]
if "current_response" not in st.session_state:
st.session_state.current_response = ""
for message in st.session_state.messages:
chat_message(message["content"], is_user=message["role"] == "user")
if user_prompt := st.text_input("Your message here",on_change=submit):
st.session_state.messages.append(
{"role": "user", "content": user_prompt}
)
chat_message(user_prompt, is_user=True)
response = model(user_prompt, max_length, temp)
st.session_state.messages.append(
{"role": "assistant", "content": response}
)
chat_message(response)
It looks like st.session_state.widget isn’t being initialized until the submit() function is being called (which doesn’t happen until someone enters something in the st.text_input widget).
Initializing st.session_state.widget outside of the submit() function should help you avoid this error. E.g.:
if "widget" not in st.session_state:
st.session_state.widget = "default value" # add what you want the default value to be
Hi @Caroline thank you for your help
so i tried this:
if "widget" not in st.session_state:
st.session_state.widget = ''
def submit():
# Check if Enter key was pressed
if st.session_state.widget.endswith('\n'):
st.session_state.widget = '' # Reset text input after submission
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "How may I help you today?"}
]
but it doesnt do the task, and once i click enter it still shows the text and is not cleared. is it becuase I am using keys instead of submit button?
no @Caroline ,
the full streamlit snippet is as follows:
styl = f"""
<style>
.stTextInput {{
position: fixed;
bottom: 3rem;
}}
</style>
"""
st.markdown(styl, unsafe_allow_html=True)
# Initialize session state variables
if "widget" not in st.session_state:
st.session_state.widget = ''
def submit():
# Check if Enter key was pressed
if st.session_state.widget.endswith('\n'):
st.session_state.widget = '' # Reset text input after submission
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "How may I help you today?"}
]
if "current_response" not in st.session_state:
st.session_state.current_response = ""
for message in st.session_state.messages:
chat_message(message["content"], is_user=message["role"] == "user")
# Take questions from the chat input to pass to the LLM
if user_prompt := st.text_input("Your message here", on_change=submit):
st.session_state.messages.append(
{"role": "user", "content": user_prompt}
)
chat_message(user_prompt, is_user=True)
response = model(user_prompt, max_length, temp)
st.session_state.messages.append(
{"role": "assistant", "content": response}
)
chat_message(response)
Sorry for the delayed response. Can you share the link to the GitHub repo for your app so I can run the full app? That snippet unfortunately isn’t runnable
I’m sorry just to make myself understand,
does that mean
it resets the value of st.session_state.widget not on the streamlit but on the variable only?
@Goyo then is there anyway where i can change the text. (basically reset the textbox) AIM
The user types their question in the textbox and I want the textbox cleared when user presses enter key through keyboard.
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.