When running the following code locally (streamlit 1.44.0), there is a situation where clicking the button doesn’t do anything: if I edit the text input, then click on the button while the text input is still focused, the code under the button’s condition doesn’t run.
import time
import streamlit as st
layout = st.empty()
layout.write("Please wait...")
time.sleep(1)
layout.empty() # This line is necessary to trigger the bug
input = st.text_input("Enter your name") # Adding an on_change delay fixes the bug...
st.write(f"Your name is {input}.")
if "input" not in st.session_state:
st.session_state.input = ""
# Bug: clicking this button when the focus is in the text_input will not trigger the button
if st.button("Update", key="button_clicked"):
st.session_state.input = input
if st.session_state.input:
st.write(f"Hello {st.session_state.input}!")
This behavior does not happen if layout.empty()
isn’t there.
My actual application has a time consuming process (modelled by the time.sleep(1)
) tracked by a progress bar, that is then hidden with layout.empty()
.
One workaround I found is to use input = st.text_input("Enter your name", on_change=lambda: time.sleep(0.2))
, but that looks very hacky to me and not reliable.
Open to suggestions!