o
    沪g:                     @  s^  U d dl mZ d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZmZm Z m!Z! d dl"m#Z# d dl$m%Z& d dl'm(Z) d dl*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 erd dl7m8Z8 ee	ee9ed f Z:de;d< eeee9ed df Z<de;d< ee<ee< f Z=de;d< ee>d e>e e>eef f Z?de;d< eee?df Z@de;d< dZAde;d< eBd ZCde;d!< d?d%d&ZDd@d(d)ZEdAd+d,ZFdBd0d1ZGdCd3d4ZHed5d6G d7d8 d8ZIeG d9d: d:ZJeG d;d< d<ZKG d=d> d>ZLdS )D    )annotationsN)Sequence)	dataclass)datedatetimetime	timedelta)dedent)TYPE_CHECKINGAnyFinalLiteralUnioncastoverload)	TypeAlias)current_form_id)check_widget_policiesmaybe_raise_label_warnings)KeyLabelVisibilitycompute_and_register_element_id get_label_visibility_proto_valueto_key)StreamlitAPIException)	DateInput)	TimeInput)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsget_session_stateregister_widget)adjust_years)DeltaGeneratornowr   	TimeValuetodayNullableScalarDateValue	DateValue DateWidgetRangeReturnDateWidgetReturn   r   DEFAULT_STEP_MINUTESzB^(YYYY[/.\-]MM[/.\-]DD|DD[/.\-]MM[/.\-]YYYY|MM[/.\-]DD[/.\-]YYYY)$ALLOWED_DATE_FORMATSvaluereturnr   c                 C  s   | dkrt   jdddS t| tr=zt| W S  ty<   zt |  jdddW  Y S  ty9   Y nw Y nw t| t rK|  jdddS t| trR| S td)Nr'   r   )secondmicrosecondzEThe type of value should be one of datetime, time, ISO string or None)	r   r'   r   replace
isinstancestrfromisoformat
ValueErrorr   r2   r,   r,   c/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/elements/widgets/time_widgets.py_convert_timelike_to_timeV   s0   


r=   r   c                 C  s   t | tr	|  S t | tr| S | dv rt  S t | trFzt| W S  tyE   z
t|  W  Y S  ty?   Y nw Y tdw td)N>   r)   zHDate value should either be an date/datetime or an ISO string or "today")r7   r   r   r'   r8   r9   r:   r   r;   r,   r,   r<   _convert_datelike_to_datet   s.   


r>   tuple[list[date] | None, bool]c                 C  sd   | d u rdS t | trt | tsd}| }nd}tt| g}t|dvr'tddd |D }||fS )N)NFTF>   r         z_DateInput value should either be an date/datetime or a list/tuple of 0 - 2 date/datetime valuesc                 S  s   g | ]}t |qS r,   )r>   .0vr,   r,   r<   
<listcomp>   s    z%_parse_date_value.<locals>.<listcomp>)r7   r   r8   r   r*   lenr   )r2   is_rangevalue_tupleparsed_datesr,   r,   r<   _parse_date_value   s   rJ   	min_valuerI   Sequence[date] | Nonec                 C  X   t | tttfrt| }|S | d u r(|rt|d dd}|S tt dd}|S td)Nr   iyearsz6DateInput min should either be a date/datetime or Noner7   r   r   r8   r>   r%   r)   r   )rK   rI   parsed_min_dater,   r,   r<   _parse_min_date      
rR   	max_valuec                 C  rM   )N
   rN   z6DateInput max should either be a date/datetime or NonerP   )rT   rI   parsed_max_dater,   r,   r<   _parse_max_date   rS   rX   T)frozenc                   @  sF   e Zd ZU ded< ded< ded< ded< edddZdddZdS )_DateInputValuesrL   r2   boolrG   r   maxminr+   rK   r*   rT   r3   c           	      C  sl   t |d\}}t||d}t||d}|dkr.ttt |d }||k r'|g}||kr.|g}| ||||dS )Nr;   )rK   rI   )rT   rI   r)   r   )r2   rG   r]   r\   )rJ   rR   rX   r   listr   )	clsr2   rK   rT   parsed_valuerG   
parsed_min
parsed_maxrD   r,   r,   r<   from_raw_values   s*   z _DateInputValues.from_raw_valuesNonec                 C  s~   | j | jkrtd| j  d| j d| jr;| jd }| jd }|| j k s*|| jkr=td| j d| j  d| j d	d S d S )
NzThe `min_value`, set to z3, shouldn't be larger than the `max_value`, set to .r   rU   zThe default `value` of z% must lie between the `min_value` of z and the `max_value` of z, inclusively.)r]   r\   r   r2   )selfstart_value	end_valuer,   r,   r<   __post_init__   s&   



z_DateInputValues.__post_init__N)r2   r+   rK   r*   rT   r*   r3   rZ   )r3   rd   )__name__
__module____qualname____annotations__classmethodrc   ri   r,   r,   r,   r<   rZ      s   
 rZ   c                   @  s,   e Zd ZU ded< ddd	d
ZdddZdS )TimeInputSerdetime | Noner2    ui_value
str | None	widget_idr   r3   c                 C  s   |d urt |d S | jS N%H:%M)r   strptimer   r2   )rf   rr   rt   r,   r,   r<   deserialize
  s
   zTimeInputSerde.deserializerD   datetime | time | Nonec                 C  s*   |d u rd S t |tr| }t|dS ru   )r7   r   r   strftime)rf   rD   r,   r,   r<   	serialize  s
   
zTimeInputSerde.serializeNrq   )rr   rs   rt   r   r3   rp   )rD   ry   r3   rs   rj   rk   rl   rm   rx   r{   r,   r,   r,   r<   ro     s   
 ro   c                   @  s.   e Zd ZU ded< 	ddd
dZdddZdS )DateInputSerderZ   r2   rq   rr   r   rt   r8   r3   r.   c                 C  sf   |d urt dd |D }n| jj}|d u st|dkr$| jjr"dS d S | jjs,|d S ttt |S )Nc                 s  s     | ]}t |d  V  qdS )%Y/%m/%dN)r   rw   r   rB   r,   r,   r<   	<genexpr>$  s    
z-DateInputSerde.deserialize.<locals>.<genexpr>r   r,   )tupler2   rF   rG   r   r.   )rf   rr   rt   return_valuer,   r,   r<   rx     s   
zDateInputSerde.deserializerD   	list[str]c                 C  s2   |d u rg S t |trt|n|g}dd |D S )Nc                 S     g | ]}t |d qS r   r   rz   rB   r,   r,   r<   rE   6  s    z,DateInputSerde.serialize.<locals>.<listcomp>)r7   r   r^   )rf   rD   to_serializer,   r,   r<   r{   1  s   zDateInputSerde.serializeNr|   )rr   r   rt   r8   r3   r.   )rD   r.   r3   r   r}   r,   r,   r,   r<   r~     s
   
 r~   c                   @  s  e Zd Ze						dDddeedddEddZe						dFddeedddGd!dZed"						dDddeedddHd$dZ						dDddeeddd%dId(d)Ze	*							dJd+ddd,dKd3d4Z	e							dLd+ddd,dMd6d4Z	e							dLd+ddd,dNd9d4Z	ed:	*							dJd+ddd,dOd=d4Z		*							dJd+dddd>dPd?d@Z
edQdBdCZdS )RTimeWidgetsMixinr'   NFvisible)minutes)disabledlabel_visibilitysteplabelr8   r2   r(   key
Key | Nonehelprs   	on_changeWidgetCallback | NoneargsWidgetArgs | NonekwargsWidgetKwargs | Noner   r[   r   r   r   int | timedeltar3   r   c                C     d S Nr,   rf   r   r2   r   r   r   r   r   r   r   r   r,   r,   r<   
time_input:     zTimeWidgetsMixin.time_inputrd   rp   c                C  r   r   r,   r   r,   r,   r<   r   K  r   r   TimeValue | Nonec                C  s&   t  }| j|||||||||	|
|dS )aq  Display a time input widget.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this time input is for.
            The label can optionally contain GitHub-flavored Markdown of the
            following types: Bold, Italics, Strikethroughs, Inline Code, Links,
            and Images. Images display like icons, with a max height equal to
            the font height.

            Unsupported Markdown elements are unwrapped so only their children
            (text contents) render. Display unsupported elements as literal
            characters by backslash-escaping them. E.g.,
            ``"1\. Not an ordered list"``.

            See the ``body`` parameter of |st.markdown|_ for additional,
            supported Markdown directives.

            For accessibility reasons, you should never set an empty label, but
            you can hide it with ``label_visibility`` if needed. In the future,
            we may disallow empty labels by raising an exception.

            .. |st.markdown| replace:: ``st.markdown``
            .. _st.markdown: https://docs.streamlit.io/develop/api-reference/text/st.markdown

        value : "now", datetime.time, datetime.datetime, str, or None
            The value of this widget when it first renders. This can be one of
            the following:

            - ``"now"`` (default): The widget initializes with the current time.
            - A ``datetime.time`` or ``datetime.datetime`` object: The widget
              initializes with the given time, ignoring any date if included.
            - An ISO-formatted time ("hh:mm", "hh:mm:ss", or "hh:mm:ss.sss") or
              datetime ("YYYY-MM-DD hh:mm:ss") string: The widget initializes
              with the given time, ignoring any date if included.
            - ``None``: The widget initializes with no time and returns
              ``None`` until the user selects a 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. No two widgets may have the same key.

        help : str or None
            A tooltip that gets displayed next to the widget label. Streamlit
            only displays the tooltip when ``label_visibility="visible"``. If
            this is ``None`` (default), no tooltip is displayed.

            The tooltip can optionally contain GitHub-flavored Markdown,
            including the Markdown directives described in the ``body``
            parameter of ``st.markdown``.

        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.

        disabled : bool
            An optional boolean that disables the time input if set to
            ``True``. The default is ``False``.

        label_visibility : "visible", "hidden", or "collapsed"
            The visibility of the label. The default is ``"visible"``. If this
            is ``"hidden"``, Streamlit displays an empty spacer instead of the
            label, which can help keep the widget alligned with other widgets.
            If this is ``"collapsed"``, Streamlit displays no label or spacer.

        step : int or timedelta
            The stepping interval in seconds. Defaults to 900, i.e. 15 minutes.
            You can also pass a datetime.timedelta object.

        Returns
        -------
        datetime.time or None
            The current value of the time input widget or ``None`` if no time has been
            selected.

        Example
        -------
        >>> import datetime
        >>> import streamlit as st
        >>>
        >>> t = st.time_input("Set an alarm for", datetime.time(8, 45))
        >>> st.write("Alarm is set for", t)

        .. output::
           https://doc-time-input.streamlit.app/
           height: 260px

        To initialize an empty time input, use ``None`` as the value:

        >>> import datetime
        >>> import streamlit as st
        >>>
        >>> t = st.time_input("Set an alarm for", value=None)
        >>> st.write("Alarm is set for", t)

        .. output::
           https://doc-time-input-empty.streamlit.app/
           height: 260px

        )r   r2   r   r   r   r   r   r   r   r   ctx)r   _time_input)rf   r   r2   r   r   r   r   r   r   r   r   r   r,   r,   r<   r   \  s   z)r   r   r   r   r   ScriptRunContext | Nonec             
   C  s  t |}t| j|||dkr|nd d t||	 |d u rd }nt|}td|t| j|t|tt	fr4|n|||
d}~t
 j}|d urO||v rO|| d u rOd }t }||_||_|d urct	|d|_t| j|_t|
ttfsztdt|
 dt|
tr|
j}
|
dk s|
td	d
jkrtd|
 d|
|_||_t|	|j_|d urt||_t|}t|j||||j |j!|dd}|j"r|!|j }d ur||_d|_#| j$d| |jS )Nr'   default_valuer   )user_keyform_idr   r2   r   r   rv   z,`step` can only be `int` or `timedelta` but z is provided.<      )hourszG`step` must be between 60 seconds and 23 hours but is currently set to z	 seconds.string_valueon_change_handlerr   r   deserializer
serializerr   
value_typeT)%r   r   dgr   r=   r   r   r7   r   r   r#   filtered_stateTimeInputProtoidr   rz   defaultr   intr   r   typesecondsr   r   r   r   r2   r	   r   ro   r$   rx   r{   value_changed	set_value_enqueue)rf   r   r2   r   r   r   r   r   r   r   r   r   parsed_time
element_idsession_statetime_input_protoserdewidget_stateserialized_valuer,   r,   r<   r     s   
	


zTimeWidgetsMixin._time_inputr)   z
YYYY/MM/DD)formatr   r   (date | datetime | str | Literal['today']rK   r*   rT   r   r   c
                C  r   r   r,   rf   r   r2   rK   rT   r   r   r   r   r   r   r   r   r,   r,   r<   
date_input@     zTimeWidgetsMixin.date_inputdate | Nonec
                C  r   r   r,   r   r,   r,   r<   r   R  r   xtuple[NullableScalarDateValue] | tuple[NullableScalarDateValue, NullableScalarDateValue] | list[NullableScalarDateValue]r-   c
                C  r   r   r,   r   r,   r,   r<   r   d  s   r   r+   r.   c
                C  s*   t  }| j|||||||||	|||
|dS )a  Display a date input widget.

        The first day of the week is determined from the user's locale in their
        browser.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this date input is for.
            The label can optionally contain GitHub-flavored Markdown of the
            following types: Bold, Italics, Strikethroughs, Inline Code, Links,
            and Images. Images display like icons, with a max height equal to
            the font height.

            Unsupported Markdown elements are unwrapped so only their children
            (text contents) render. Display unsupported elements as literal
            characters by backslash-escaping them. E.g.,
            ``"1\. Not an ordered list"``.

            See the ``body`` parameter of |st.markdown|_ for additional,
            supported Markdown directives.

            For accessibility reasons, you should never set an empty label, but
            you can hide it with ``label_visibility`` if needed. In the future,
            we may disallow empty labels by raising an exception.

            .. |st.markdown| replace:: ``st.markdown``
            .. _st.markdown: https://docs.streamlit.io/develop/api-reference/text/st.markdown

        value : "today", datetime.date, datetime.datetime, str, list/tuple of these, or None
            The value of this widget when it first renders. This can be one of
            the following:

            - ``"today"`` (default): The widget initializes with the current date.
            - A ``datetime.date`` or ``datetime.datetime`` object: The widget
              initializes with the given date, ignoring any time if included.
            - An ISO-formatted date ("YYYY-MM-DD") or datetime
              ("YYYY-MM-DD hh:mm:ss") string: The widget initializes with the
              given date, ignoring any time if included.
            - A list or tuple with up to two of the above: The widget will
              initialize with the given date interval and return a tuple of the
              selected interval. You can pass an empty list to initialize the
              widget with an empty interval or a list with one value to
              initialize only the beginning date of the iterval.
            - ``None``: The widget initializes with no date and returns
              ``None`` until the user selects a date.

        min_value : "today", datetime.date, datetime.datetime, str, or None
            The minimum selectable date. This can be any of the date types
            accepted by ``value``, except list or tuple.

            If this is ``None`` (default), the minimum selectable date is ten
            years before the initial value. If the initial value is an
            interval, the minimum selectable date is ten years before the start
            date of the interval. If no initial value is set, the minimum
            selectable date is ten years before today.

        max_value : "today", datetime.date, datetime.datetime, str, or None
            The maximum selectable date. This can be any of the date types
            accepted by ``value``, except list or tuple.

            If this is ``None`` (default), the maximum selectable date is ten
            years after the initial value. If the initial value is an interval,
            the maximum selectable date is ten years after the end date of the
            interval. If no initial value is set, the maximum selectable date
            is ten years after today.

        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. No two widgets may have the same key.

        help : str or None
            A tooltip that gets displayed next to the widget label. Streamlit
            only displays the tooltip when ``label_visibility="visible"``. If
            this is ``None`` (default), no tooltip is displayed.

            The tooltip can optionally contain GitHub-flavored Markdown,
            including the Markdown directives described in the ``body``
            parameter of ``st.markdown``.

        on_change : callable
            An optional callback invoked when this date_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.

        format : str
            A format string controlling how the interface should display dates.
            Supports "YYYY/MM/DD" (default), "DD/MM/YYYY", or "MM/DD/YYYY".
            You may also use a period (.) or hyphen (-) as separators.

        disabled : bool
            An optional boolean that disables the date input if set to
            ``True``. The default is ``False``.

        label_visibility : "visible", "hidden", or "collapsed"
            The visibility of the label. The default is ``"visible"``. If this
            is ``"hidden"``, Streamlit displays an empty spacer instead of the
            label, which can help keep the widget alligned with other widgets.
            If this is ``"collapsed"``, Streamlit displays no label or spacer.

        Returns
        -------
        datetime.date or a tuple with 0-2 dates or None
            The current value of the date input widget or ``None`` if no date has been
            selected.

        Examples
        --------
        >>> import datetime
        >>> import streamlit as st
        >>>
        >>> d = st.date_input("When's your birthday", datetime.date(2019, 7, 6))
        >>> st.write("Your birthday is:", d)

        .. output::
           https://doc-date-input.streamlit.app/
           height: 380px

        >>> import datetime
        >>> import streamlit as st
        >>>
        >>> today = datetime.datetime.now()
        >>> next_year = today.year + 1
        >>> jan_1 = datetime.date(next_year, 1, 1)
        >>> dec_31 = datetime.date(next_year, 12, 31)
        >>>
        >>> d = st.date_input(
        ...     "Select your vacation for next year",
        ...     (jan_1, datetime.date(next_year, 1, 7)),
        ...     jan_1,
        ...     dec_31,
        ...     format="MM.DD.YYYY",
        ... )
        >>> d

        .. output::
           https://doc-date-input1.streamlit.app/
           height: 380px

        To initialize an empty date input, use ``None`` as the value:

        >>> import datetime
        >>> import streamlit as st
        >>>
        >>> d = st.date_input("When's your birthday", value=None)
        >>> st.write("Your birthday is:", d)

        .. output::
           https://doc-date-input-empty.streamlit.app/
           height: 380px

        )r   r2   rK   rT   r   r   r   r   r   r   r   r   r   )r   _date_input)rf   r   r2   rK   rT   r   r   r   r   r   r   r   r   r   r,   r,   r<   r   x  s"    /)r   r   r   r   c
                  s
  t |}t| j|||dkr|nd d t|| ddd  |} |}|dkr,d }nt|tr; fd	d
|D }n |}td|t| j||||||
d	}tt	
|
s^td|
 dtj|||d}|dkrt j}|d ur||v r|| }tj|||d}~~~t }||_|j|_||_t||j_|
|_||_|jd u rg |jd d < ndd
 |jD |jd d < t|jd|_t|jd|_t| j|_|d urt||_ t!|}t"|j|||	|j#|j$|dd}|j%r|$|j|jd d < d|_&| j'd| |jS )Nr)   r   rD   r*   r3   rs   c                 S  sN   | dkrd S t | tr| S t | trt|  dS t | tr%t| dS d S )Nr)   r   )r7   r8   r   r   rz   )rD   r,   r,   r<   parse_date_deterministic_for_idR  s   


zETimeWidgetsMixin._date_input.<locals>.parse_date_deterministic_for_idc                   s   g | ]	} t t|qS r,   )r   r*   rB   r   r,   r<   rE   g  s    z0TimeWidgetsMixin._date_input.<locals>.<listcomp>r   )r   r   r   r2   rK   rT   r   r   zThe provided format (`z`) is not valid. DateInput format should be one of `YYYY/MM/DD`, `DD/MM/YYYY`, or `MM/DD/YYYY` and can also use a period (.) or hyphen (-) as separators.)r2   rK   rT   c                 S  r   r   r   rB   r,   r,   r<   rE     s    r   string_array_valuer   T)rD   r*   r3   rs   )(r   r   r   r   r7   r   r   r   r[   r1   matchr   rZ   rc   r#   r   DateInputProtor   rG   r   r   r   r2   r   r   r   r   rz   r]   r\   r   r	   r   r~   r$   rx   r{   r   r   r   )rf   r   r2   rK   rT   r   r   r   r   r   r   r   r   r   rQ   rW   parsedr   parsed_valuesr   state_valuedate_input_protor   r   r,   r   r<   r   7  s   






zTimeWidgetsMixin._date_inputr&   c                 C  s
   t d| S )zGet our DeltaGenerator.r&   )r   )rf   r,   r,   r<   r     s   
zTimeWidgetsMixin.dg)r'   NNNNN)r   r8   r2   r(   r   r   r   rs   r   r   r   r   r   r   r   r[   r   r   r   r   r3   r   )NNNNNN)r   r8   r2   rd   r   r   r   rs   r   r   r   r   r   r   r   r[   r   r   r   r   r3   rp   )r   r8   r2   r   r   r   r   rs   r   r   r   r   r   r   r   r[   r   r   r   r   r3   rp   )r   r8   r2   r   r   r   r   rs   r   r   r   r   r   r   r   r[   r   r   r   r   r   r   r3   rp   )r)   NNNNNNN)r   r8   r2   r   rK   r*   rT   r*   r   r   r   rs   r   r   r   r   r   r   r   r8   r   r[   r   r   r3   r   )NNNNNNN)r   r8   r2   rd   rK   r*   rT   r*   r   r   r   rs   r   r   r   r   r   r   r   r8   r   r[   r   r   r3   r   )r   r8   r2   r   rK   r*   rT   r*   r   r   r   rs   r   r   r   r   r   r   r   r8   r   r[   r   r   r3   r-   )r   r8   r2   r+   rK   r*   rT   r*   r   r   r   rs   r   r   r   r   r   r   r   r8   r   r[   r   r   r3   r.   )r   r8   r2   r+   rK   r*   rT   r*   r   r   r   rs   r   r   r   r   r   r   r   r8   r   r[   r   r   r   r   r3   r.   )r3   r&   )rj   rk   rl   r   r   r0   r   r   r   r   r   propertyr   r,   r,   r,   r<   r   9  s    


 
[ B r   )r2   r(   r3   r   )r2   r*   r3   r   )r2   r+   r3   r?   )rK   r*   rI   rL   r3   r   )rT   r*   rI   rL   r3   r   )M
__future__r   recollections.abcr   dataclassesr   r   r   r   r   textwrapr	   typingr
   r   r   r   r   r   r   typing_extensionsr   !streamlit.elements.lib.form_utilsr   streamlit.elements.lib.policiesr   r   streamlit.elements.lib.utilsr   r   r   r   r   streamlit.errorsr   streamlit.proto.DateInput_pb2r   r   streamlit.proto.TimeInput_pb2r   r   streamlit.runtime.metrics_utilr   streamlit.runtime.scriptrunnerr   r   streamlit.runtime.stater    r!   r"   r#   r$   streamlit.time_utilr%   streamlit.delta_generatorr&   r8   r(   rm   r*   r+   r   r-   r.   r0   compiler1   r=   r>   rJ   rR   rX   rZ   ro   r~   r   r,   r,   r,   r<   <module>   s\   $






8