I am building a simple chat bot using streamlit. A small problem is that I am collecting user input using text_input. Sometimes, the user clicks away to do something else mid-sentence, and this triggers the “on_change” event, which in my case sends the message to the chat bot and gets a response. The alternative of using a submit button for each sentence in a chat bot seems too tedious, so I am wondering if there are any good workarounds (e.g. a way to track enter keypress, or another method) to the fact that text_input triggers their callback on loss of focus.
Steps to reproduce
input=st.text_input("Type your response here")
st.write(f"User input is:{input}")
If writing in this textbox and click away, their input will still be written below.
Thanks, I’m totally new to javascript and writing components, but I took what you wrote as inspiration and looked up how to apply this to text_inputs as well. Here is a version that I can apply to all text_inputs, or even a single text_input so that enter works, but not focusout. Super helpful thank you!
import streamlit as st
import streamlit.components.v1 as components
toggle_all=st.checkbox("""Turn on "focus out" component for all text inputs""")
toggle_first=st.checkbox("""Turn on "focus out" component for text input 1 only.""")
x=st.text_input("1 Type something here")
y=st.text_input("2 Type something here as well")
st.write(f"Result of input box 1: {x}")
st.write(f"Result of input box 2: {y}")
if toggle_all:
#This looks for any input box and applies the code to it to stop default behavior when focus is lost
components.html(
"""
<script>
const doc = window.parent.document;
const inputs = doc.querySelectorAll('input');
inputs.forEach(input => {
input.addEventListener('focusout', function(event) {
event.stopPropagation();
event.preventDefault();
console.log("lost focus")
});
});
</script>""",
height=0,
width=0,
)
if toggle_first:
#This will apply only to an input with a specific label (aria-label)
components.html(
"""
<script>
const doc = window.parent.document;
const input = doc.querySelector('input[aria-label="1 Type something here"]');
input.addEventListener('focusout', function() {
event.stopPropagation();
event.preventDefault();
console.log('Lost focus');
});
</script>
""",
height=0,
width=0,
)
```
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.