o
    YhkV                     @  sf  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 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 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.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 erd dl8m9Z9 d dl:m;Z; e
dZ<G dd dee< Z=G dd dZ>dS )    )annotations)dedent)TYPE_CHECKINGAnyCallableGenericLiteralTypeVarcastoverload)Never)OptionSequenceconvert_anything_to_list)current_form_id)LayoutConfigWidthWithoutContentvalidate_width)create_mappingsindex_maybe_coerce_enum)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)StreamlitAPIException)	Selectbox)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsget_session_stateregister_widget)check_python_comparable)Sequence)DeltaGenerator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 )SelectboxSerdeSequence[T]options	list[str]formatted_optionsdict[str, int] formatted_option_to_option_index
int | Nonedefault_option_indexN)r4   returnNonec                C  s   || _ || _|| _|| _dS )a  Initialize the SelectboxSerde.

        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.   r0   r2   r4   )selfr.   r0   r2   r4    r8   e/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/elements/widgets/selectbox.py__init__S   s   
zSelectboxSerde.__init__vT | str | None
str | Nonec                 C  sT   |d u rd S t | jdkrdS zt| j|}| j| W S  ty)   td| Y S w )Nr    str)lenr.   r   r0   
ValueErrorr
   )r7   r;   option_indexr8   r8   r9   	serializev   s   zSelectboxSerde.serializeui_valuec                 C  sR   |d u r| j d urt| jdkr| j| j  S d S | j|}|d ur'| j| S |S )Nr   )r4   r@   r.   r2   get)r7   rD   rB   r8   r8   r9   deserialize   s   
zSelectboxSerde.deserialize)
r.   r-   r0   r/   r2   r1   r4   r3   r5   r6   )r;   r<   r5   r=   )rD   r=   r5   r<   )__name__
__module____qualname____annotations__r:   rC   rF   r8   r8   r8   r9   r,   M   s   
 
#r,   c                   @  sx  e Zd Zededddddfddddddd>d$d%Zededddddfddddddd?d(d%Zededddddfdddd)ddd@d,d%ZeedddddfdddddddAd.d%Zeedddddfdddd)dddBd0d%ZededddddfdddddddCd2d%Zed3dedddddfdddddddCd4d%Zdedddddfddddddd5dDd9d:ZedEd<d=Z	dS )FSelectboxMixinr   NFvisiblestretch)placeholderdisabledlabel_visibilityaccept_new_optionswidthlabelr?   r.   Sequence[Never]indexintformat_funcCallable[[Any], str]key
Key | Nonehelpr=   	on_changeWidgetCallback | NoneargsWidgetArgs | NonekwargsWidgetKwargs | NonerN   rO   boolrP   r   rQ   Literal[False]rR   r   r5   r6   c
                C     d S Nr8   r7   rS   r.   rU   rW   rY   r[   r\   r^   r`   rN   rO   rP   rQ   rR   r8   r8   r9   	selectbox      zSelectboxMixin.selectboxOptionSequence[T]r+   c
                C  rd   re   r8   rf   r8   r8   r9   rg      rh   TLiteral[True]T | strc
                C  rd   re   r8   rf   r8   r8   r9   rg      rh   T | Nonec
                C  rd   re   r8   rf   r8   r8   r9   rg      rh   r<   c
                C  rd   re   r8   rf   r8   r8   r9   rg      rh   r3   c
                C  rd   re   r8   rf   r8   r8   r9   rg      rh   rg   c
                C  s.   t  }| j|||||||||	|
|||||dS )a  Display a select widget.

        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.

        index : int or None
            The index of the preselected option on first render. If ``None``,
            will initialize empty and return ``None`` until the user selects an option.
            Defaults to 0 (the first option).

        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 selectbox'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.

        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 an option" is displayed when options are available and
              ``accept_new_options=False``.
            - "Choose or add an option" is displayed when options are available
              and ``accept_new_options=True``.
            - "Add an option" 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 selectbox 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 a selection that isn'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 a new
            item that doesn't exist in ``options``.

            When a user enters a new item, it is returned by the widget 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.

        width : "stretch" or int
            The width of the selectbox 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
        -------
        any
            The selected option or ``None`` if no option is selected.

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

        If no index is provided, the first option is selected by default.

        >>> import streamlit as st
        >>>
        >>> option = st.selectbox(
        ...     "How would you like to be contacted?",
        ...     ("Email", "Home phone", "Mobile phone"),
        ... )
        >>>
        >>> st.write("You selected:", option)

        .. output::
           https://doc-selectbox.streamlit.app/
           height: 320px

        **Example 2: Use a selectbox widget with no initial selection**

        To initialize an empty selectbox, use ``None`` as the index value.

        >>> import streamlit as st
        >>>
        >>> option = st.selectbox(
        ...     "How would you like to be contacted?",
        ...     ("Email", "Home phone", "Mobile phone"),
        ...     index=None,
        ...     placeholder="Select contact method...",
        ... )
        >>>
        >>> st.write("You selected:", option)

        .. output::
           https://doc-selectbox-empty.streamlit.app/
           height: 320px

        **Example 3: Let users add a new option**

        To allow users to add a new option that isn't included in the
        ``options`` list, use the ``accept_new_options=True`` parameter. You
        can also customize the placeholder text.

        >>> import streamlit as st
        >>>
        >>> option = st.selectbox(
        ...     "Default email",
        ...     ["foo@example.com", "bar@example.com", "baz@example.com"],
        ...     index=None,
        ...     placeholder="Select a saved email or enter a new one",
        ...     accept_new_options=True,
        ... )
        >>>
        >>> st.write("You selected:", option)

        .. output::
           https://doc-selectbox-accept-new-options.streamlit.app/
           height: 320px

        )rS   r.   rU   rW   rY   r[   r\   r^   r`   rN   rO   rP   rQ   rR   ctx)r"   
_selectbox)r7   rS   r.   rU   rW   rY   r[   r\   r^   r`   rN   rO   rP   rQ   rR   rm   r8   r8   r9   rg     s&    G)rN   rO   rP   rQ   rR   rm   Callable[[Any], Any]rm   ScriptRunContext | Nonec
                C  s
  t |}t| j|||dkrd n|d t|| t|}t| t|ts3|d ur3tdt	|j
 |d urQt|dkrQd|  krLt|k sQtd td|
dkrWd}
t||\}}td|| j|||||
||d
}t j}|d ur||v r|| d u rd }t }||_||_|d ur||_||jd d < t| j|_|
pd|_||_t||j_||_|d urt||_t||||d	}t |j|||	|j!|j"|d
d}t#|||}|j$r|"|j}|d ur||_%d|_&t'| t(|d}|rt)||| | jj*d||d |jS )Nr   )default_valuez"Selectbox Value has invalid type: zWSelectbox index must be greater than or equal to 0 and less than the length of options.r>    rg   )	user_keydgrS   r.   rU   r[   rN   rQ   rR   )r0   r2   r4   string_value)on_change_handlerr^   r`   deserializer
serializerrm   
value_typeT)rR   )layout_config)+r   r   rt   r   r   r(   
isinstancerV   r   typerG   r@   r   r   r&   filtered_stateSelectboxProtoidrS   defaultr.   r   form_idrN   rO   r   rP   valuerQ   r   r[   r,   r'   rF   rC   r   value_changed	raw_value	set_valuer   r   r   _enqueue)r7   rS   r.   rU   rW   rY   r[   r\   r^   r`   rN   rO   rP   rQ   rR   rm   optr0   r2   
element_idsession_stateselectbox_protoserdewidget_stateserialized_valuerz   r8   r8   r9   rn     s   
*	



zSelectboxMixin._selectboxr*   c                 C  s
   t d| S )zGet our DeltaGenerator.r*   )r
   )r7   r8   r8   r9   rt   _  s   
zSelectboxMixin.dg)rS   r?   r.   rT   rU   rV   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rc   rR   r   r5   r6   )rS   r?   r.   ri   rU   rV   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rc   rR   r   r5   r+   )rS   r?   r.   ri   rU   rV   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rj   rR   r   r5   rk   )rS   r?   r.   ri   rU   r6   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rc   rR   r   r5   rl   )rS   r?   r.   ri   rU   r6   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rj   rR   r   r5   r<   )rS   r?   r.   ri   rU   r3   rW   rX   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rb   rR   r   r5   r<   ) rS   r?   r.   ri   rU   r3   rW   ro   rY   rZ   r[   r=   r\   r]   r^   r_   r`   ra   rN   r=   rO   rb   rP   r   rQ   rb   rR   r   rm   rp   r5   r<   )r5   r*   )
rG   rH   rI   r   r?   rg   r    rn   propertyrt   r8   r8   r8   r9   rK      s     ]xrK   N)?
__future__r   textwrapr   typingr   r   r   r   r   r	   r
   r   typing_extensionsr   streamlit.dataframe_utilr   r   !streamlit.elements.lib.form_utilsr   #streamlit.elements.lib.layout_utilsr   r   r   -streamlit.elements.lib.options_selector_utilsr   r   r   streamlit.elements.lib.policiesr   r   streamlit.elements.lib.utilsr   r   r   r   r   r   streamlit.errorsr   streamlit.proto.Selectbox_pb2r   r~   streamlit.runtime.metrics_utilr    streamlit.runtime.scriptrunnerr!   r"   streamlit.runtime.stater#   r$   r%   r&   r'   streamlit.type_utilr(   collections.abcr)   streamlit.delta_generatorr*   r+   r,   rK   r8   r8   r8   r9   <module>   s,   ( H