U
    ARd|&                     @   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)Radio)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 )
RadioSerde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/radio.py	serialize7   s    zRadioSerde.serialize )ui_value	widget_idr%   c                 C   s<   |d k	r|n| j }t| jdkr8| j| d k	r8| j| S d S r&   )r#   r'   r"   )r(   r-   r.   idxr)   r)   r*   deserialize<   s    zRadioSerde.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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 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
e d
ddZeddddZdS )
RadioMixinradior   NFZvisible)disabled
horizontallabel_visibility)labelr"   r#   format_funckeyhelp	on_changeargskwargsr:   r;   r<   r%   c
                C   s*   t  }| j|||||||||	|
|||dS )a  Display a radio button widget.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this radio group 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 radio 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 radio 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 radio.
        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 radio.
        on_change : callable
            An optional callback invoked when this radio'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 radio button if set to
            True. The default is False. This argument can only be supplied by
            keyword.
        horizontal : bool
            An optional boolean, which orients the radio group horizontally.
            The default is false (vertical buttons). 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
        >>>
        >>> genre = st.radio(
        ...     "What\'s your favorite movie genre",
        ...     ('Comedy', 'Drama', 'Documentary'))
        >>>
        >>> if genre == 'Comedy':
        ...     st.write('You selected comedy.')
        ... else:
        ...     st.write("You didn\'t select comedy.")

        .. output::
           https://doc-radio.streamlitapp.com/
           height: 260px

        )r=   r"   r#   r>   r?   r@   rA   rB   rC   r:   r;   ctxr<   )r   _radio)r(   r=   r"   r#   r>   r?   r@   rA   rB   rC   r:   r;   r<   rD   r)   r)   r*   r9   K   s     lzRadioMixin.radio)r=   r"   r#   r>   r?   r@   rA   rB   rC   r:   r;   r<   rD   r%   c
                   sD  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r0||j|_d	|_| jd| |jS )
Nr   )default_valuer?   z Radio Value has invalid type: %sz3Radio index must be between 0 and length of optionsc                    s   g | ]}t  |qS r)   )r7   ).0optionr>   r)   r*   
<listcomp>   s     z%RadioMixin._radio.<locals>.<listcomp>r9   )Zuser_keyZon_change_handlerrB   rC   Zdeserializer
serializerrD   T) r   r   dgr   r   r   
isinstancer5   r   typer1   r'   
RadioProtor=   defaultr"   r   Zform_idr;   r   r@   r!   r   r0   r+   r:   r   r<   valuevalue_changed	set_valueZ_enqueue)r(   r=   r"   r#   r>   r?   r@   rA   rB   rC   r:   r;   r<   rD   optZradio_protoZserdeZwidget_stater)   rI   r*   rE      sT    

&

zRadioMixin._radior    )r%   c                 C   s
   t d| S )zGet our DeltaGenerator.r    )r
   )r(   r)   r)   r*   rL     s    zRadioMixin.dg)r1   r2   r3   r   r7   r   r   r5   r   r   r   r   r   r   r   boolr   r9   r   rE   propertyrL   r)   r)   r)   r*   r8   J   sr    F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.Radio_pb2r   rO   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    $$	