I want to capture the ‘ENTER’ keypress event in text_input. I could not find any example or documentation for this. Below is something I was trying to achieve using Streamlit:
How to specify event on ‘ENTER’ key?
text = st.text_input(“User pressed enter”)
How to detect ‘ENTER’ key press?
if user_presses_enter_key:
print(“Execute this code on ENTER keypress event”
Currently, I have added a separate button which the user has to click every time for the input, but I want this to happen when the user presses the ‘ENTER’ key.
Streamlit does not work with event bindings. In fact, each time you interact with your app (press Enter on your text_input for example), the whole script reruns.
So, what is your use-case exactly?
If you want to execute some code when your text_input contains something, it’s as simple as this:
import streamlit as st
value = st.text_input("Some input")
if value:
st.write("There is some value. Processing...")
# Some code
My use-case is a simple search app, where the user should be able to use ‘Enter’ key press to input the query and display the results other than clicking a button as well. I have done this with a hack which works fine for now for both the cases, but wanted to do a few things on other keypress events as well. Thanks for the clarification on event bindings.
Hack:
prev_qry = ""
user_query = st.text_input(label="Enter query")
if st.button('Search') or (prev_qry != user_query):
prev_qry = user_query
# Display search results for user_query
My other queries were with st.dataframe. I guess I will open a different topic for that. Thank you.
I don’t see your solution as a hack per se. Your if statement describes exactly what you want to do, and one of Streamlit goals is to allow such logic flows.
I wanted to use other key press events (backspace, shift…) to trigger some functionality, but I can do that in an alternative way. Thanks for the help. I am currently trying the dataframe issue which I have raised here Adjust dataframe column width. Would really appreciate your help on that.
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.