U
    ARdp$                     @   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 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( d dl)m*Z* erd dl+m,Z, eG dd dee% Z-G dd dZ.dS )    )	dataclass)dedent)TYPE_CHECKINGAnyCallableGenericOptionalSequencecast)current_form_id)check_callback_rulescheck_session_state_rules get_label_visibility_proto_value)StreamlitAPIException)	Selectbox)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsregister_widget)KeyLabelVisibilityOptionSequenceTensure_indexablemaybe_raise_label_warningsto_key)index_)DeltaGeneratorc                   @   sN   e Zd ZU ee ed< eed< eedddZde	e e
e	e ddd	Zd
S )SelectboxSerdeoptionsindex)vreturnc                 C   s   t | jdkrdS t| j|S Nr   )lenr"   r   )selfr$    r)   @/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/selectbox.py	serialize7   s    zSelectboxSerde.serialize )ui_value	widget_idr%   c                 C   s.   |d k	r|n| j }t| jdkr*| j| S d S r&   )r#   r'   r"   )r(   r-   r.   idxr)   r)   r*   deserialize<   s    zSelectboxSerde.deserializeN)r,   )__name__
__module____qualname__r	   r   __annotations__intobjectr+   r   strr0   r)   r)   r)   r*   r!   2   s   
 r!   c                   @   s   e Zd Zeddedddddfdddeee eee	ge	f 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
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eddddZdS )SelectboxMixin	selectboxr   NFZvisible)disabledlabel_visibility)labelr"   r#   format_funckeyhelp	on_changeargskwargsr:   r;   r%   c
                C   s(   t  }| j|||||||||	|
||dS )aB  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 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, 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.
        index : int
            The index of the preselected option on first render.
        format_func : function
            Function to modify the display of the labels. It receives the option
            as an argument and its output will be cast to str.
        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 selectbox.
        on_change : callable
            An optional callback invoked when this selectbox'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 selectbox 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
        -------
        any
            The selected option

        Example
        -------
        >>> 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.streamlitapp.com/
           height: 320px

        )r<   r"   r#   r=   r>   r?   r@   rA   rB   r:   r;   ctx)r   
_selectbox)r(   r<   r"   r#   r=   r>   r?   r@   rA   rB   r:   r;   rC   r)   r)   r*   r9   G   s    `zSelectboxMixin.selectbox)r:   r;   rC   )r<   r"   r#   r=   r>   r?   r@   rA   rB   r:   r;   rC   r%   c
                   s>  t |}t| j| t|dkr"d n||d t|| t|}t|tsZtdt	|j
 t|dkrd|  kr~t|k sn tdt }||_||_ fdd|D |jd d < t| j|_|d k	rt||_t||}td|||||	|j|j|d	}|
|_t||j_|jr*||j|_d	|_| jd| |jS )
Nr   )default_valuer>   z$Selectbox Value has invalid type: %sz7Selectbox index must be between 0 and length of optionsc                    s   g | ]}t  |qS r)   )r7   ).0optionr=   r)   r*   
<listcomp>   s     z-SelectboxMixin._selectbox.<locals>.<listcomp>r9   )Zuser_keyZon_change_handlerrA   rB   Zdeserializer
serializerrC   T)r   r   dgr   r   r   
isinstancer5   r   typer1   r'   SelectboxProtor<   defaultr"   r   Zform_idr   r?   r!   r   r0   r+   r:   r   r;   valuevalue_changed	set_valueZ_enqueue)r(   r<   r"   r#   r=   r>   r?   r@   rA   rB   r:   r;   rC   optZselectbox_protoZserdeZwidget_stater)   rH   r*   rD      sR    

&

zSelectboxMixin._selectboxr    )r%   c                 C   s
   t d| S )zGet our DeltaGenerator.r    )r
   )r(   r)   r)   r*   rK      s    zSelectboxMixin.dg)r1   r2   r3   r   r7   r   r   r5   r   r   r   r   r   r   r   boolr   r9   r   rD   propertyrK   r)   r)   r)   r*   r8   F   sj   sEr8   N)/Zdataclassesr   textwrapr   typingr   r   r   r   r   r	   r
   Zstreamlit.elements.formr   Zstreamlit.elements.utilsr   r   r   Zstreamlit.errorsr   Zstreamlit.proto.Selectbox_pb2r   rN   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   Zstreamlit.utilr   Zstreamlit.delta_generatorr    r!   r8   r)   r)   r)   r*   <module>   s    $$	