U
    ARd`                     @   s  U d dl mZ d dlmZmZ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mZmZ d dlmZmZ d dlmZ d dlmZmZmZ d dlmZ d d	l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l0m1Z1m2Z2m3Z3m4Z4 er d dl5m6Z6 ede7e8eeeZ9ee7e8ef Z:ee;d< ee7e8eeef Z<ee;d< ee9ee9 f Z=ee;d< ee=e7 e=e8 e=e e=e e=e f Z>ee;d< ee9ee9 ee9e9f f Z?ee;d< ee?e7 e?e8 e?e e?e e?e f Z@ee;d< dZAee;d< deA ZBee;d< edddejCdZDee;d< eedd d!ZEeed"d#d$ZFee7d%d&d'ZGee7d(d)d*ZHe7ee ed+d,d-ZIeG d.d/ d/ZJG d0d1 d1ZKd2S )3    )	dataclass)datedatetimetime	timedeltatimezonetzinfo)dedent)	TYPE_CHECKINGAnyListOptionalSequenceTupleTypeVarUnioncast)Final	TypeAlias)current_form_id)check_callback_rulescheck_session_state_rules get_label_visibility_proto_value)StreamlitAPIException)JSNumberJSNumberBoundsException)Slider)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsget_session_stateregister_widget)KeyLabelVisibilitymaybe_raise_label_warningsto_key)DeltaGeneratorSliderScalarTStepSliderScalarSliderValueGenericSliderValueSliderReturnGenericSliderReturni@B SECONDS_TO_MICROSiQ DAYS_TO_MICROSi     r   	UTC_EPOCH)time_returnc                 C   s   t tddd| S )Ni  r3   )r   combiner   )r6    r9   =/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/slider.py_time_to_datetime[   s    r;   )date_r7   c                 C   s   t | t S N)r   r8   r   )r<   r9   r9   r:   _date_to_datetimeb   s    r>   )deltar7   c                 C   s   | j | jt  | jt  S r=   )microsecondssecondsr1   daysr2   )r?   r9   r9   r:   _delta_to_microsf   s    rC   )dtr7   c                 C   s   | j tjd}t|t S )Nr4   )replacer   utcrC   r5   )rD   utc_dtr9   r9   r:   _datetime_to_microsn   s    rH   )microsorig_tzr7   c                 C   s   t t| d }|j|dS )zERestore times/datetimes to original timezone (dates are always naive))r@   r4   )r5   r   rE   )rI   rJ   rG   r9   r9   r:   _micros_to_datetimex   s    rK   c                   @   sd   e Zd ZU ee ed< eed< eed< ee	 ed< deee  e
dddZeee d	d
dZdS )SliderSerdevalue	data_typesingle_valuerJ    )ui_value	widget_idc                    s   |d k	r|}n j } jtjkr.dd |D } jtjkrL fdd|D } jtjkrj fdd|D } jtjkr fdd|D } jr|d S t|S )Nc                 S   s   g | ]}t |qS r9   )int.0vr9   r9   r:   
<listcomp>   s     z+SliderSerde.deserialize.<locals>.<listcomp>c                    s   g | ]}t t| jqS r9   )rK   rS   rJ   rT   selfr9   r:   rW      s     c                    s    g | ]}t t| j qS r9   )rK   rS   rJ   r   rT   rX   r9   r:   rW      s     c                    s*   g | ]"}t t| j j jd qS )r4   )rK   rS   rJ   r   rE   rT   rX   r9   r:   rW      s   r   )	rM   rN   SliderProtoINTDATETIMEDATETIMErO   tuple)rY   rQ   rR   valr9   rX   r:   deserialize   s    
zSliderSerde.deserialize)rV   r7   c                 C   sr   t |ttf}|rt|n|g}| jtjkr:dd |D }| jtjkrTdd |D }| jtjkrndd |D }|S )Nc                 S   s   g | ]}t t|qS r9   )rH   r>   rT   r9   r9   r:   rW      s     z)SliderSerde.serialize.<locals>.<listcomp>c                 S   s   g | ]}t t|qS r9   )rH   r;   rT   r9   r9   r:   rW      s     c                 S   s   g | ]}t |qS r9   )rH   rT   r9   r9   r:   rW      s     )
isinstancelistr_   rN   rZ   r]   r^   r\   )rY   rV   range_valuerM   r9   r9   r:   	serialize   s    zSliderSerde.serializeN)rP   )__name__
__module____qualname__r   float__annotations__rS   boolr   r   strra   r   re   r9   r9   r9   r:   rL      s   
rL   c                   @   s   e Zd Zedddddeee ee ee ee ee ee	 ee ee
 ee ee eeedddZddddd	eee ee ee	 ee ee
 ee ee eeee ed
ddZeddddZdS )SliderMixinsliderNFZvisible)disabledlabel_visibility)label	min_value	max_valuerM   stepformatkeyhelp	on_changeargskwargsro   rp   r7   c                C   s,   t  }| j|||||||||	|
||||dS )a  Display a slider widget.

        This supports int, float, date, time, and datetime types.

        This also allows you to render a range slider by passing a two-element
        tuple or list as the `value`.

        The difference between `st.slider` and `st.select_slider` is that
        `slider` only accepts numerical or date/time data and takes a range as
        input, while `select_slider` accepts any datatype and takes an iterable
        set of options.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this slider is for.
            The label can optionally contain Markdown and supports the following
            elements: Bold, Italics, Strikethroughs, Inline Code, Emojis, and Links.

            This also supports:

            * Emoji shortcodes, such as ``:+1:``  and ``:sunglasses:``.
              For a list of all supported codes,
              see https://share.streamlit.io/streamlit/emoji-shortcodes.

            * LaTeX expressions, by wrapping them in "$" or "$$" (the "$$"
              must be on their own lines). Supported LaTeX functions are listed
              at https://katex.org/docs/supported.html.

            * Colored text, using the syntax ``:color[text to be colored]``,
              where ``color`` needs to be replaced with any of the following
              supported colors: blue, green, orange, red, violet.

            Unsupported 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``.

            For accessibility reasons, you should never set an empty label (label="")
            but hide it with label_visibility if needed. In the future, we may disallow
            empty labels by raising an exception.
        min_value : a supported type or None
            The minimum permitted value.
            Defaults to 0 if the value is an int, 0.0 if a float,
            value - timedelta(days=14) if a date/datetime, time.min if a time
        max_value : a supported type or None
            The maximum permitted value.
            Defaults to 100 if the value is an int, 1.0 if a float,
            value + timedelta(days=14) if a date/datetime, time.max if a time
        value : a supported type or a tuple/list of supported types or None
            The value of the slider when it first renders. If a tuple/list
            of two values is passed here, then a range slider with those lower
            and upper bounds is rendered. For example, if set to `(1, 10)` the
            slider will have a selectable range between 1 and 10.
            Defaults to min_value.
        step : int/float/timedelta or None
            The stepping interval.
            Defaults to 1 if the value is an int, 0.01 if a float,
            timedelta(days=1) if a date/datetime, timedelta(minutes=15) if a time
            (or if max_value - min_value < 1 day)
        format : str or None
            A printf-style format string controlling how the interface should
            display numbers. This does not impact the return value.
            Formatter for int/float supports: %d %e %f %g %i
            Formatter for date/time/datetime uses Moment.js notation:
            https://momentjs.com/docs/#/displaying/format/
        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 slider.
        on_change : callable
            An optional callback invoked when this slider'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, which disables the slider if set to True. The
            default is False. This argument can only be supplied by keyword.
        label_visibility : "visible" or "hidden" or "collapsed"
            The visibility of the label. If "hidden", the label doesn't show but there
            is still empty space for it above the widget (equivalent to label="").
            If "collapsed", both the label and the space are removed. Default is
            "visible". This argument can only be supplied by keyword.


        Returns
        -------
        int/float/date/time/datetime or tuple of int/float/date/time/datetime
            The current value of the slider widget. The return type will match
            the data type of the value parameter.

        Examples
        --------
        >>> import streamlit as st
        >>>
        >>> age = st.slider('How old are you?', 0, 130, 25)
        >>> st.write("I'm ", age, 'years old')

        And here's an example of a range slider:

        >>> import streamlit as st
        >>>
        >>> values = st.slider(
        ...     'Select a range of values',
        ...     0.0, 100.0, (25.0, 75.0))
        >>> st.write('Values:', values)

        This is a range time slider:

        >>> import streamlit as st
        >>> from datetime import time
        >>>
        >>> appointment = st.slider(
        ...     "Schedule your appointment:",
        ...     value=(time(11, 30), time(12, 45)))
        >>> st.write("You're scheduled for:", appointment)

        Finally, a datetime slider:

        >>> import streamlit as st
        >>> from datetime import datetime
        >>>
        >>> start_time = st.slider(
        ...     "When do you start?",
        ...     value=datetime(2020, 1, 1, 9, 30),
        ...     format="MM/DD/YY - hh:mm")
        >>> st.write("Start time:", start_time)

        .. output::
           https://doc-slider.streamlitapp.com/
           height: 300px

        )rq   rr   rs   rM   rt   ru   rv   rw   rx   ry   rz   ro   rp   ctx)r   _slider)rY   rq   rr   rs   rM   rt   ru   rv   rw   rx   ry   rz   ro   rp   r{   r9   r9   r:   rn      s$      zSliderMixin.slider)ro   rp   r{   )rq   rt   ru   rv   rw   rx   ry   rz   ro   rp   r{   r7   c          '      C   sL  t |}t| j|	 t||d t|| |d krdt j}|d k	rT||krT|| }n|d k	r`|nd}ttj	t
tjttjttjttji}tjtjtjf}t|t| }t|ttfot|dk}|s|std|r|g}dd }||stdttt| t|dkrtj	}n|t|d  }tj}tj}|tjkrltjj|d jd}tjj|d jd}|tjtjfkr|d td	d
 }|d td	d
 }tj	dddddtjdddddtj||tdd
ddtj||tdd
ddtj||tddddi}|d kr|| d }|d kr4|| d }|d krtt || d }|tjtjfkr|| tdd
k rtdd}|d krtt!|| d }|dkrtd|||g}t"tdd |}t"tdd |}||ko t|to t|t|k}|s:|s:|s:tdt|j#t|j#t|j#d  |tj	koH|}|tjkoX|}||kof|}|s|s|std!t|j#t|j#t|j#d" t||}t||}t|dkrt|d |}t|d |}nPt|d#kr(|\} }!| |!kr|!|  } }!| |!f}t| |}t|!|}n||g}zJ|rRt$%|d$ t$%|d% n&|rrt$&|d$ t$&|d% n|rxW n0 t'k
r }" ztt!|"W 5 d }"~"X Y nX d }#|tjkrttt(|}t(|}t(|}|tjkrttt)|}t)|}t)|}||krZ|tjtjfkr*|d jnd }#ttt*|}t*|}t*|}t+tt|}t }$tj,j-|$_||$_.||$_/||$j0d d < ||$_||$_tt
||$_1||$_2g |$j3d d < t4| j|$_5|d k	rt6||$_7t8||||#}%t9d&|$||	|
||%j:|%j;|d'	}&||$_<t=||$j>_?|&j@r2|%;|&j?|$j?d d < d(|$_A| jBd&|$ ttC|&j?S ))N)default_valuerv   r   )r   r3      zcSlider value should either be an int/float/datetime or a list/tuple of 0 to 2 ints/floats/datetimesc                 S   s   t ttt| dk S )Nr~   )lensetmaptype)itemsr9   r9   r:   all_same_type  s    z*SliderMixin._slider.<locals>.all_same_typezASlider tuple/list components must be of the same type.
But were: r4      )rB   d   r3   z%d)rr   rs   rt   ru   g        g      ?g{Gz?z%0.2fz
YYYY-MM-DD   )minuteszHH:mmrr   rs   rt   ru   z1Slider components cannot be passed a `step` of 0.c                 S   s
   t | tS r=   )rb   rS   ar9   r9   r:   <lambda>      z%SliderMixin._slider.<locals>.<lambda>c                 S   s
   t | tS r=   )rb   ri   r   r9   r9   r:   r     r   zSlider value arguments must be of matching types.
`min_value` has %(min_type)s type.
`max_value` has %(max_type)s type.
`step` has %(step)s type.)min_typemax_typert   zBoth value and arguments must be of the same type.
`value` has %(value_type)s type.
`min_value` has %(min_type)s type.
`max_value` has %(max_type)s type.)Z
value_typer   r   r~   z`min_value`z`max_value`rn   )Zuser_keyZon_change_handlerry   rz   Zdeserializer
serializerr{   T)Dr(   r   dgr   r'   r#   Zfiltered_staterS   rZ   r[   ri   FLOATr   r\   r   r]   r   r^   rb   r_   keysrc   r   r   r   r   minmaxrE   r   r   r   r+   rl   allrf   r   Zvalidate_int_boundsZvalidate_float_boundsr   r;   r>   rH   rC   TypeZSLIDERrq   ru   defaultrt   rN   optionsr   Zform_idr	   rw   rL   r$   ra   re   ro   r   rp   rM   value_changed	set_valueZ_enqueuer0   )'rY   rq   rr   rs   rM   rt   ru   rv   rw   rx   ry   rz   ro   rp   r{   Zsession_stateSUPPORTED_TYPESZTIMELIKE_TYPESrO   rd   r   rN   Zdatetime_minZdatetime_maxZDEFAULTSZslider_argsZint_argsZ
float_argsZtimelike_argsZall_intsZ
all_floatsZall_timelikesstartenderJ   Zslider_protoZserdeZwidget_stater9   r9   r:   r|   \  s   

     
!
















zSliderMixin._sliderr)   )r7   c                 C   s
   t d| S )zGet our DeltaGenerator.r)   )r   rX   r9   r9   r:   r   x  s    zSliderMixin.dg)
NNNNNNNNNN)
NNNNNNNNNN)rf   rg   rh   r   rl   r   r,   r.   r+   r%   r!   r    r"   rk   r&   r   rn   r   r0   r|   propertyr   r9   r9   r9   r:   rm      s~              4            rm   N)LZdataclassesr   r   r   r   r   r   r   textwrapr	   typingr
   r   r   r   r   r   r   r   r   Ztyping_extensionsr   r   Zstreamlit.elements.formr   Zstreamlit.elements.utilsr   r   r   Zstreamlit.errorsr   Zstreamlit.js_numberr   r   Zstreamlit.proto.Slider_pb2r   rZ   Zstreamlit.runtime.metrics_utilr   Zstreamlit.runtime.scriptrunnerr   r   Zstreamlit.runtime.stater    r!   r"   r#   r$   Zstreamlit.type_utilr%   r&   r'   r(   Zstreamlit.delta_generatorr)   rS   ri   r*   r+   rj   r,   r-   r.   r/   r0   r1   r2   rF   r5   r;   r>   rC   rH   rK   rL   rm   r9   r9   r9   r:   <module>   sr     ,

)