U
    ARd0                     @   s  d dl 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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 d dlmZ d d	lmZmZ d d
lm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- erd dl.m/Z/ eee de
e	e0  dddZ1eee eee ef e	e0 dddZ1ee eee edf e
e	e0  dddZ1eee edf e0dddZ2e0e0dddZ3eG dd dee( Z4G dd dZ5dS )    )	dataclass)dedent)
TYPE_CHECKINGAnyCallableGenericListOptionalSequenceUnioncastoverload)current_form_id)check_callback_rulescheck_session_state_rules get_label_visibility_proto_value)StreamlitAPIException)MultiSelect)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsregister_widget)	KeyLabelVisibilityOptionSequenceTensure_indexableis_iterableis_typemaybe_raise_label_warningsto_key)DeltaGeneratorN)optdefault_valuesreturnc                 C   s   d S N r%   r&   r)   r)   B/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/multiselect.py_check_and_convert_to_indices>   s    r,   c                 C   s   d S r(   r)   r*   r)   r)   r+   r,   E   s    c                    s   |dkrd krdS t |tsbt|ds2t|drFtttt |}n|rR| krZ|g}nt|}|D ]}| krftdqf fdd|D S )zIPerform validation checks and return indices based on the default values.Nznumpy.ndarrayzpandas.core.series.Seriesz5Every Multiselect default value must exist in optionsc                    s   g | ]}  |qS r)   )index).0valuer%   r)   r+   
<listcomp>f   s     z1_check_and_convert_to_indices.<locals>.<listcomp>)
isinstancelistr!   r   r
   r   r   )r%   r&   r/   r)   r0   r+   r,   L   s"    
 )defaultr'   c                 C   s*   | d krdS t | sdS tttt | S )Nr      )r    lenr   r
   r   )r4   r)   r)   r+   _get_default_counti   s
    r7   )current_selectionsmax_selectionsc                 C   sD   | dkrdnd}|dkrdnd}d|  d| d| d| d| dS )	Nr5   optionoptionsz
Multiselect has  z) selected but `max_selections`
is set to z. This happened because you either gave too many options to `default`
or you manipulated the widget's state through `st.session_state`. Note that
the latter can happen before the line indicated in the traceback.
Please select at most z.
r)   )r8   r9   Zcurr_selections_nounZmax_selections_nounr)   r)   r+   _get_over_max_options_messageq   s    r=   c                   @   s^   e Zd ZU ee ed< ee ed< ee ee dddZde	ee  e
ee ddd	Zd
S )MultiSelectSerder;   default_value)r/   r'   c                 C   s   t | j|S r(   )r,   r;   )selfr/   r)   r)   r+   	serialize   s    zMultiSelectSerde.serialize )ui_value	widget_idr'   c                    s$   |d k	r|n j } fdd|D S )Nc                    s   g | ]} j | qS r)   )r;   )r.   ir@   r)   r+   r1      s     z0MultiSelectSerde.deserialize.<locals>.<listcomp>)r?   )r@   rC   rD   Zcurrent_valuer)   rF   r+   deserialize   s    zMultiSelectSerde.deserializeN)rB   )__name__
__module____qualname__r
   r   __annotations__r   intrA   r	   strrG   r)   r)   r)   r+   r>   }   s   
 
r>   c                   @   s  e Zd Zeddedddddfddddeee ee e	egef ee
 ee ee ee ee eeee ee dddZdedddddfddddd	eee eee edf e	egef 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 )MultiSelectMixinmultiselectNFZvisible)disabledlabel_visibilityr9   )labelr;   r4   format_funckeyhelp	on_changeargskwargsrP   rQ   r9   r'   c
                C   s*   t  }| j|||||||||	|
|||dS )a  Display a multiselect widget.
        The multiselect widget starts as empty.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this select widget 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.
        options : Sequence[V], numpy.ndarray, pandas.Series, pandas.DataFrame, or pandas.Index
            Labels for the select options. This will be cast to str internally
            by default. For pandas.DataFrame, the first column is selected.
        default: [V], V, or None
            List of default values. Can also be a single value.
        format_func : function
            Function to modify the display of selectbox options. It receives
            the raw option as an argument and should output the label to be
            shown for that option. This has no impact on the return value of
            the multiselect.
        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 multiselect.
        on_change : callable
            An optional callback invoked when this multiselect'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 multiselect widget 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.
        max_selections : int
            The max selections that can be selected at a time.
            This argument can only be supplied by keyword.

        Returns
        -------
        list
            A list with the selected options

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> options = st.multiselect(
        ...     'What are your favorite colors',
        ...     ['Green', 'Yellow', 'Red', 'Blue'],
        ...     ['Yellow', 'Red'])
        >>>
        >>> st.write('You selected:', options)

        .. output::
           https://doc-multiselect.streamlitapp.com/
           height: 420px

        )rR   r;   r4   rS   rT   rU   rV   rW   rX   rP   rQ   ctxr9   )r   _multiselect)r@   rR   r;   r4   rS   rT   rU   rV   rW   rX   rP   rQ   r9   rY   r)   r)   r+   rO      s     izMultiSelectMixin.multiselect)rP   rQ   rY   r9   )rR   r;   r4   rS   rT   rU   rV   rW   rX   rP   rQ   rY   r9   r'   c
                   s@  t |}t| j| t||d t|}t|| t||}t }||_|d krTg n|}||j	d d <  fdd|D |j
d d < t| j|_|pd|_|d k	rt||_t||}td|||||	|j|j|d	}t|j}|r||krtt|||
|_t||j_|jr,||j|jd d < d|_| jd| |jS )N)r?   rT   c                    s   g | ]}t  |qS r)   )rM   )r.   r:   rS   r)   r+   r1   (  s     z1MultiSelectMixin._multiselect.<locals>.<listcomp>r   rO   )Zuser_keyZon_change_handlerrW   rX   Zdeserializer
serializerrY   T)r#   r   dgr   r   r"   r,   MultiSelectProtorR   r4   r;   r   Zform_idr9   r   rU   r>   r   rG   rA   r7   r/   r   r=   rP   r   rQ   value_changed	set_valueZ_enqueue)r@   rR   r;   r4   rS   rT   rU   rV   rW   rX   rP   rQ   rY   r9   r%   indicesZmultiselect_protor?   ZserdeZwidget_stateZdefault_countr)   r[   r+   rZ     sR    





zMultiSelectMixin._multiselectr$   )r'   c                 C   s
   t d| S )zGet our DeltaGenerator.r$   )r   rF   r)   r)   r+   r]   N  s    zMultiSelectMixin.dg)rH   rI   rJ   r   rM   r   r   r	   r   r   r   r   r   r   boolr   rL   r   rO   r   r
   r   rZ   propertyr]   r)   r)   r)   r+   rN      sr   }CrN   )6Zdataclassesr   textwrapr   typingr   r   r   r   r   r	   r
   r   r   r   Zstreamlit.elements.formr   Zstreamlit.elements.utilsr   r   r   Zstreamlit.errorsr   Zstreamlit.proto.MultiSelect_pb2r   r^   Zstreamlit.runtime.metrics_utilr   Zstreamlit.runtime.scriptrunnerr   r   Zstreamlit.runtime.stater   r   r   r   Zstreamlit.type_utilr   r   r   r   r   r    r!   r"   r#   Zstreamlit.delta_generatorr$   rL   r,   r7   r=   r>   rN   r)   r)   r)   r+   <module>   s>   0, 
  
