Unexpected behavior of time_input with key argument

I am not sure if this is a bug but the following code behaves very strange to me:

import streamlit as st

time = st.time_input('input the time', key='time')
st.text(time)

When I load the page for the first time, the default value of the time_input widget will be current system time, say for example 10:00, and I am able to change the value with no problem.

However after 1 minute (i.e. when system clock shows 10:01), when I try to change to the widget value again, no matter what value I selected, the value will turn into 10:01

Demo:
Screen-Recording

It seems like time_input widget is forced to reset to current time every minute. And during each minute, it can be freely changed.

The only workaround I can find is to set both value and key arguments

import datetime as dt
import streamlit as st

if 'time' not in st.session_state:
    st.session_state['time'] = dt.datetime.now().time()
time = st.time_input('input the time', value=st.session_state['time'], key='time')
st.text(time)

However this will raise the warning The widget with key "time" was created with a default value but also had its value set via the Session State API.

1 Like

Hi @KenExplore,

Thanks for posting!

I wasn’t able to reproduce this behavior. Does it only happen when you select the time input right before the minute changes? i.e. if you select a time input and then wait 60 seconds, does it still revert?

Caroline :balloon:

Hi Caroline, thanks for replying!

It happens at every time right after the minute changes.

Wait 60 seconds and select any time input, it will revert. And wait for another 60 seconds (minute changes), it will revert again.

By the way, I’m using streamline 1.12.2, MacOS Monterey, test on both safari and chrome.