def _time_input( self, label: str, value=None, key: Optional[Key] = None, help: Optional[str] = None, on_change: Optional[WidgetCallback] = None, args: Optional[WidgetArgs] = None, kwargs: Optional[WidgetKwargs] = None, *, # keyword-only arguments: disabled: bool = False, ctx: Optional[ScriptRunContext] = None, ) -> time: key = to_key(key) check_callback_rules(self.dg, on_change) check_session_state_rules(default_value=value, key=key) # Set value default. if value is None: value = datetime.now().time().replace(second=0, microsecond=0) # Ensure that the value is either datetime/time if not isinstance(value, datetime) and not isinstance(value, time): raise StreamlitAPIException( "The type of the value should be either datetime or time.") # Convert datetime to time if isinstance(value, datetime): value = value.time().replace(second=0, microsecond=0) time_input_proto = TimeInputProto() time_input_proto.label = label time_input_proto.default = time.strftime(value, "%H:%M") time_input_proto.form_id = current_form_id(self.dg) time_input_proto.disabled = disabled if help is not None: time_input_proto.help = dedent(help) def deserialize_time_input(ui_value, widget_id=""): return (datetime.strptime(ui_value, "%H:%M").time() if ui_value is not None else value) def serialize_time_input(v): if isinstance(v, datetime): v = v.time() return time.strftime(v, "%H:%M") current_value, set_frontend_value = register_widget( "time_input", time_input_proto, user_key=key, on_change_handler=on_change, args=args, kwargs=kwargs, deserializer=deserialize_time_input, serializer=serialize_time_input, ctx=ctx, ) if set_frontend_value: time_input_proto.value = serialize_time_input(current_value) time_input_proto.set_value = True self.dg._enqueue("time_input", time_input_proto) return cast(time, current_value)
def time_input( self, label: str, value=None, key: Optional[Key] = None, help: Optional[str] = None, on_change: Optional[WidgetCallback] = None, args: Optional[WidgetArgs] = None, kwargs: Optional[WidgetKwargs] = None, ) -> time: """Display a time input widget. Parameters ---------- label : str A short label explaining to the user what this time input is for. value : datetime.time/datetime.datetime The value of this widget when it first renders. This will be cast to str internally. Defaults to the current time. key : str or int An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key. help : str An optional tooltip that gets displayed next to the input. on_change : callable An optional callback invoked when this time_input's value changes. args : tuple An optional tuple of args to pass to the callback. kwargs : dict An optional dict of kwargs to pass to the callback. Returns ------- datetime.time The current value of the time input widget. Example ------- >>> t = st.time_input('Set an alarm for', datetime.time(8, 45)) >>> st.write('Alarm is set for', t) """ key = to_key(key) check_callback_rules(self.dg, on_change) check_session_state_rules(default_value=value, key=key) # Set value default. if value is None: value = datetime.now().time().replace(second=0, microsecond=0) # Ensure that the value is either datetime/time if not isinstance(value, datetime) and not isinstance(value, time): raise StreamlitAPIException( "The type of the value should be either datetime or time.") # Convert datetime to time if isinstance(value, datetime): value = value.time().replace(second=0, microsecond=0) time_input_proto = TimeInputProto() time_input_proto.label = label time_input_proto.default = time.strftime(value, "%H:%M") time_input_proto.form_id = current_form_id(self.dg) if help is not None: time_input_proto.help = dedent(help) def deserialize_time_input(ui_value, widget_id=""): return (datetime.strptime(ui_value, "%H:%M").time() if ui_value is not None else value) def serialize_time_input(v): if isinstance(v, datetime): v = v.time() return time.strftime(v, "%H:%M") current_value, set_frontend_value = register_widget( "time_input", time_input_proto, user_key=key, on_change_handler=on_change, args=args, kwargs=kwargs, deserializer=deserialize_time_input, serializer=serialize_time_input, ) if set_frontend_value: time_input_proto.value = serialize_time_input(current_value) time_input_proto.set_value = True self.dg._enqueue("time_input", time_input_proto) return cast(time, current_value)