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.

Hello everyone,

I am also struggling with the time_input under MacOS (Ventura 13.0.1) and Streamlit v1.15.2.

It always returns the current time, not the selected, whatever combination of value / key I choose.

trip_time_out = st.time_input(label = 'Time out', key = 'time_out')
trip_time_in = st.time_input(label = 'Time in', value = datetime.now().time(), key = 'time_in')

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.