o
    Yh^P                     @  s  d dl mZ 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 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mZmZmZ d d	lmZ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+ d dl,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4 erd dlmZ d dlmZ d dl5m6Z6 d dl1m7Z7m8Z8m9Z9 edZ:G dd de
e: Z;d&ddZ<d'd!d"Z=G d#d$ d$Z>d%S )(    )annotations)Sequence)dedent)TYPE_CHECKINGAnyCallableGenericLiteralTypeVarcastoverload)OptionSequenceconvert_anything_to_list)current_form_id)LayoutConfigWidthWithoutContentvalidate_width)(convert_to_sequence_and_check_comparablecreate_mappingsget_default_indicesmaybe_coerce_enum_sequence)check_widget_policiesmaybe_raise_label_warnings)KeyLabelVisibilitycompute_and_register_element_id get_label_visibility_proto_valuesave_for_app_testingto_key)&StreamlitSelectionCountExceedsMaxError)MultiSelect)gather_metrics)ScriptRunContextget_script_run_ctx)register_widget)is_iterable)r   )DeltaGenerator)
WidgetArgsWidgetCallbackWidgetKwargsTc                   @  sR   e Zd ZU ded< ded< ded< ded< d	d
dddZdddZdddZd	S )MultiSelectSerdeSequence[T]options	list[str]formatted_optionsdict[str, int] formatted_option_to_option_indexz	list[int]default_options_indicesN)r2   list[int] | NonereturnNonec                C  s    || _ || _|| _|pg | _dS )a  Initialize the MultiSelectSerde.

        We do not store an option_to_formatted_option mapping because the generic
        options might not be hashable, which would raise a RuntimeError. So we do
        two lookups: option -> index -> formatted_option[index].


        Parameters
        ----------
        options : Sequence[T]
            The sequence of selectable options.
        formatted_options : list[str]
            The string representations of each option. The formatted_options correspond
            to the options sequence by index.
        formatted_option_to_option_index : dict[str, int]
            A mapping from formatted option strings to their corresponding indices in
            the options sequence.
        default_option_index : int or None, optional
            The index of the default option to use when no selection is made.
            If None, no default option is selected.
        N)r-   r/   r1   r2   )selfr-   r/   r1   r2    r7   g/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/elements/widgets/multiselect.py__init__V   s   zMultiSelectSerde.__init__valuelist[T | str] | list[T]c              	   C  s\   t |}g }|D ]#}z| j|}|| j|  W q ty+   |td| Y qw |S )Nstr)r   r-   indexappendr/   
ValueErrorr   )r6   r:   converted_valuevaluesvoption_indexr7   r7   r8   	serializey   s   zMultiSelectSerde.serializeui_valuelist[str] | Nonec              	     sj   |d u r fdd j D S g }|D ] }z j|}| j|  W q ty2   || Y qw |S )Nc                   s   g | ]} j | qS r7   )r-   ).0ir6   r7   r8   
<listcomp>   s    z0MultiSelectSerde.deserialize.<locals>.<listcomp>)r2   r/   r=   r>   r-   r?   )r6   rE   rA   rB   rC   r7   rI   r8   deserialize   s   zMultiSelectSerde.deserialize)
r-   r,   r/   r.   r1   r0   r2   r3   r4   r5   )r:   r;   r4   r.   )rE   rF   r4   r;   )__name__
__module____qualname____annotations__r9   rD   rK   r7   r7   r7   r8   r+   P   s   
 
#r+   defaultSequence[Any] | Any | Noner4   intc                 C  s&   | d u rdS t | sdS ttd| S )Nr      zSequence[Any])r%   lenr   )rP   r7   r7   r8   _get_default_count   s
   rU   
selectionsmax_selections
int | Noner5   c                 C  s,   |d u rd S t | }||krt||dd S )N)current_selections_countmax_selections_count)rU   r   )rV   rW   default_countr7   r7   r8   _check_max_selections   s   r\   c                	   @  s  e Zd Zededddddfdddddddd8d%d&Zededddddfddddd'ddd9d*d&Zededddddfdddddddd:d,d&Zed-dedddddfdddddddd;d/d&Zdedddddfdddddddd0d<d3d4Zed=d6d7Z	dS )>MultiSelectMixinNFvisiblestretch)rW   placeholderdisabledlabel_visibilityaccept_new_optionswidthlabelr<   r-   OptionSequence[T]rP   
Any | Noneformat_funcCallable[[Any], str]key
Key | Nonehelp
str | None	on_changeWidgetCallback | NoneargsWidgetArgs | NonekwargsWidgetKwargs | NonerW   rX   r`   ra   boolrb   r   rc   Literal[False]rd   r   r4   list[T]c
                C     d S Nr7   r6   re   r-   rP   rh   rj   rl   rn   rp   rr   rW   r`   ra   rb   rc   rd   r7   r7   r8   multiselect      zMultiSelectMixin.multiselectTLiteral[True]list[T | str]c
                C  rw   rx   r7   ry   r7   r7   r8   rz      r{   list[T] | list[T | str]c
                C  rw   rx   r7   ry   r7   r7   r8   rz      r{   rz   Literal[False, True] | boolc
                C  s~   |dkrd}t  }| jdi d|d|d|d|d|d|d	|d
|d|	d|
d|d|d|d|d|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 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

        options : Iterable
            Labels for the select options in an ``Iterable``. This can be a
            ``list``, ``set``, or anything supported by ``st.dataframe``. If
            ``options`` is dataframe-like, the first column will be used. Each
            label will be cast to ``str`` internally by default.

        default : Iterable of V, V, or None
            List of default values. Can also be a single value.

        format_func : function
            Function to modify the display of the 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 command.

        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 widget's value changes.

        args : list or tuple
            An optional list or tuple of args to pass to the callback.

        kwargs : dict
            An optional dict of kwargs to pass to the callback.

        max_selections : int
            The max selections that can be selected at a time.

        placeholder : str or  None
            A string to display when no options are selected.
            If this is ``None`` (default), the widget displays placeholder text
            based on the widget's configuration:

            - "Choose options" is displayed when options are available and
              ``accept_new_options=False``.
            - "Choose or add options" is displayed when options are available
              and ``accept_new_options=True``.
            - "Add options" is displayed when no options are available and
              ``accept_new_options=True``.
            - "No options to select" is displayed when no options are available
              and ``accept_new_options=False``. The widget is also disabled in
              this case.

        disabled : bool
            An optional boolean that disables the multiselect widget 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 aligned with other widgets.
            If this is ``"collapsed"``, Streamlit displays no label or spacer.

        accept_new_options : bool
            Whether the user can add selections that aren't included in ``options``.
            If this is ``False`` (default), the user can only select from the
            items in ``options``. If this is ``True``, the user can enter new
            items that don't exist in ``options``.

            When a user enters and selects a new item, it is included in the
            widget's returned list as a string. The new item is not added to
            the widget's drop-down menu. Streamlit will use a case-insensitive
            match from ``options`` before adding a new item, and a new item
            can't be added if a case-insensitive match is already selected. The
            ``max_selections`` argument is still enforced.

        width : "stretch" or int
            The width of the multiselect widget. This can be one of the
            following:

            - ``"stretch"`` (default): The width of the widget matches the
              width of the parent container.
            - An integer specifying the width in pixels: The widget has a
              fixed width. If the specified width is greater than the width of
              the parent container, the width of the widget matches the width
              of the parent container.

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

        Examples
        --------
        **Example 1: Use a basic multiselect widget**

        You can declare one or more initial selections with the ``default``
        parameter.

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

        .. output::
           https://doc-multiselect.streamlit.app/
           height: 350px

        **Example 2: Let users to add new options**

        To allow users to enter and select new options that aren't included in
        the ``options`` list, use the ``accept_new_options`` parameter. To
        prevent users from adding an unbounded number of new options, use the
        ``max_selections`` parameter.

        >>> import streamlit as st
        >>>
        >>> options = st.multiselect(
        ...     "What are your favorite cat names?",
        ...     ["Jellybeans", "Fish Biscuit", "Madam President"],
        ...     max_selections=5,
        ...     accept_new_options=True,
        ... )
        >>>
        >>> st.write("You selected:", options)

        .. output::
           https://doc-multiselect-accept-new-options.streamlit.app/
           height: 350px

          re   r-   rP   rh   rj   rl   rn   rp   rr   rW   r`   ra   rb   rc   rd   ctxNr7   )r#   _multiselect)r6   re   r-   rP   rh   rj   rl   rn   rp   rr   rW   r`   ra   rb   rc   rd   r   r7   r7   r8   rz      sJ    ?
	
)rW   r`   ra   rb   rc   rd   r   r   ScriptRunContext | Nonec
                C  s  t |}d}t| j|||d t|| t|}t||\}}t||}|dkr*d}t| j}t||| j|||||
|||d}t	 }||_
||jd d < ||_||_||_|
pXd|_|p]d|_t||j_||jd d < |d urut||_||_t||||d}t|j
|||	|j|j|dd	}t|j|
 t|||}|jr||j|jd d < d
|_ t!| t"|d}|rt#||| | jj$|||d |jS )Nrz   )default_valuer   r   )
user_keydgre   r-   rP   rl   rW   r`   rc   rd   r   )r/   r1   r2   string_array_value)on_change_handlerrp   rr   deserializer
serializerr   
value_typeT)rd   )layout_config)%r   r   r   r   r   r   r   r   r   MultiSelectProtoidrP   form_idra   re   rW   r`   r   rb   r:   r-   r   rl   rc   r+   r$   rK   rD   r\   r   value_changed
raw_values	set_valuer   r   r   _enqueue)r6   re   r-   rP   rh   rj   rl   rn   rp   rr   rW   r`   ra   rb   rc   rd   r   widget_nameindexable_optionsr/   r1   default_valuesr   
element_idprotoserdewidget_stater   r7   r7   r8   r     s   






zMultiSelectMixin._multiselectr&   c                 C  s
   t d| S )zGet our DeltaGenerator.r&   )r   rI   r7   r7   r8   r   0  s   
zMultiSelectMixin.dg) re   r<   r-   rf   rP   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rW   rX   r`   rm   ra   rt   rb   r   rc   ru   rd   r   r4   rv   ) re   r<   r-   rf   rP   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rW   rX   r`   rm   ra   rt   rb   r   rc   r|   rd   r   r4   r}   ) re   r<   r-   rf   rP   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rW   rX   r`   rm   ra   rt   rb   r   rc   rt   rd   r   r4   r~   ) re   r<   r-   rf   rP   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rW   rX   r`   rm   ra   rt   rb   r   rc   r   rd   r   r4   r~   )"re   r<   r-   rf   rP   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rW   rX   r`   rm   ra   rt   rb   r   rc   rt   rd   r   r   r   r4   r~   )r4   r&   )
rL   rM   rN   r   r<   rz   r!   r   propertyr   r7   r7   r7   r8   r]      s     Yrr]   N)rP   rQ   r4   rR   )rV   rQ   rW   rX   r4   r5   )?
__future__r   collections.abcr   textwrapr   typingr   r   r   r   r	   r
   r   r   streamlit.dataframe_utilr   r   !streamlit.elements.lib.form_utilsr   #streamlit.elements.lib.layout_utilsr   r   r   -streamlit.elements.lib.options_selector_utilsr   r   r   r   streamlit.elements.lib.policiesr   r   streamlit.elements.lib.utilsr   r   r   r   r   r   streamlit.errorsr   streamlit.proto.MultiSelect_pb2r    r   streamlit.runtime.metrics_utilr!   streamlit.runtime.scriptrunnerr"   r#   streamlit.runtime.stater$   streamlit.type_utilr%   streamlit.delta_generatorr&   r'   r(   r)   r*   r+   rU   r\   r]   r7   r7   r7   r8   <module>   s4   ( 
D
