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.