a
    d&                     @  s   d Z ddlmZ ddlZddl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 dd	lmZmZmZmZmZ e
d
 e	 G dd deeeeeee	ZdS )zgr.Dropdown() component.    )annotationsN)AnyCallableLiteral)documentset_documentation_group)SimpleSerializable)FormComponentIOComponent	_Keywords)warn_style_method_deprecation)
ChangeableEventListenerMethod	Focusable	Inputable
Selectable	componentc                   @  s  e Zd ZdZd4ddddddddddddddddddd	d
dddddddddddddddddZddddZddddZdd Zee	j
ddddddddddfdddddddddddd d!d"Zd#d$d%d&d'Zd(d) Zd*d+ Zd,d-d.d/d0Zdd1dd1d2d3ZdS )5Dropdowna  
    Creates a dropdown of choices from which entries can be selected.
    Preprocessing: passes the value of the selected dropdown entry as a {str} or its index as an {int} into the function, depending on `type`.
    Postprocessing: expects a {str} corresponding to the value of the dropdown entry to be selected.
    Examples-format: a {str} representing the drop down value to select.
    Demos: sentence_builder, titanic_survival
    NvalueFT   )r   typemultiselectallow_custom_valuemax_choiceslabelinfoevery
show_label	containerscale	min_widthinteractivevisibleelem_idelem_classeszlist[str] | Nonez!str | list[str] | Callable | NonezLiteral['value', 'index']zbool | Noneboolz
int | Nonez
str | Nonezfloat | Noneintzlist[str] | str | None)choicesr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   c                K  s   |rdd |D ng | _ ddg}||vr<td| d| || _|| _|r\t|tr\|g}|sr|durrtd || _|| _	|r|rtd	d
| _
|  tj| f|||	|
||||||||d| dS )a	  
        Parameters:
            choices: list of options to select from.
            value: default value(s) selected in dropdown. If None, no value is selected by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
            type: Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
            multiselect: if True, multiple choices can be selected.
            allow_custom_value: If True, allows user to enter a custom value that is not in the list of choices. Only applies if `multiselect` is False.
            max_choices: maximum number of choices that can be selected. If None, no limit is enforced.
            label: component name in interface.
            info: additional component description.
            every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
            show_label: if True, will display label.
            container: If True, will place the component in a container - providing some extra padding around the border.
            scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
            min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
            interactive: if True, choices in this dropdown will be selectable; if False, selection will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
            visible: If False, component will be hidden.
            elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
            elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
        c                 S  s   g | ]}t |qS  )str).0choicer(   r(   c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/components/dropdown.py
<listcomp>T       z%Dropdown.__init__.<locals>.<listcomp>r   indexz$Invalid value for parameter `type`: z. Please choose from one of: NzCThe `max_choices` parameter is ignored when `multiselect` is False.z;Custom values are not supported when `multiselect` is True.F)r   r   r   r   r   r   r    r!   r"   r#   r$   r   )r'   
ValueErrorr   r   
isinstancer)   warningswarnr   r   Zinterpret_by_tokensr
   __init__)selfr'   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   kwargsZvalid_typesr(   r(   r,   r4   *   sP    *zDropdown.__init__zdict[str, dict | bool])returnc                 C  s<   | j r dddid| j d}ndd| j d}|dd	S )
Narrayr   stringzList of options from: )r   itemsdescriptionzOption from: )r   r;   F)r   Zserialized_infor   r'   )r5   r   r(   r(   r,   api_info   s    
zDropdown.api_infozdict[str, Any]c                 C  sb   | j r4| jr| jd gng | jr,| jd gng dS | jrD| jd nd | jrV| jd nd dS d S )Nr   )rawZ
serializedr<   r5   r(   r(   r,   example_inputs   s    zDropdown.example_inputsc                 C  s(   | j | j| j| j| j| jdt| S )N)r'   r   r   r   r   r   )r'   r   r   r   r   r   r
   
get_configr?   r(   r(   r,   rA      s    zDropdown.get_configz(Any | Literal[_Keywords.NO_VALUE] | Nonezstr | list[str] | Noner   r'   r   r   r   r   r   r    r!   placeholderr"   c                 C  s   ||||||||
| ||	ddS )Nupdate)r'   r   r   r   r   r   r    r"   r   r!   rC   Z__type__r(   rB   r(   r(   r,   rD      s    zDropdown.updatezstr | list[str]z(str | int | list[str] | list[int] | None)xr7   c                   sx    j dkr|S  j dkrb|du r$dS  jr< fdd|D S t|trt| jv r\ j|S dS ntd j  ddS )z
        Parameters:
            x: selected choice(s)
        Returns:
            selected choice(s) as string or index within choice list or list of string or indices
        r   r/   Nc                   s   g | ]} j |qS r(   )r'   r/   )r*   cr?   r(   r,   r-      r.   z'Dropdown.preprocess.<locals>.<listcomp>zUnknown type: z'. Please choose from: 'value', 'index'.)r   r   r1   r)   r'   r/   r0   )r5   rE   r(   r?   r,   
preprocess   s    	


zDropdown.preprocessc                 C  s   | S )z
        Calculates interpretation score of each choice by comparing the output against each of the outputs when alternative choices are selected.
        r(   r?   r(   r(   r,   set_interpret_parameters   s    z!Dropdown.set_interpret_parametersc                 C  s   t | j}|| |i fS )N)listr'   remove)r5   rE   r'   r(   r(   r,   get_interpretation_neighbors   s    

z%Dropdown.get_interpretation_neighborszlist[float | None]rI   )scoresr7   c                 K  s   | | j|d |S )zs
        Returns:
            Each value represents the interpretation score corresponding to each choice.
        N)insertr'   r/   )r5   rE   Z	neighborsrL   r6   r(   r(   r,   get_interpretation_scores   s    z"Dropdown.get_interpretation_scores)r   c                K  s   t   |dur|| _| S )zc
        This method is deprecated. Please set these arguments in the constructor instead.
        N)r   r   )r5   r   r6   r(   r(   r,   style   s    zDropdown.style)N)__name__
__module____qualname____doc__r4   r=   r@   rA   staticmethodr   ZNO_VALUErD   rG   rH   rK   rN   rO   r(   r(   r(   r,   r      sR   

 2V&
r   )rS   
__future__r   r2   typingr   r   r   Zgradio_client.documentationr   r   Zgradio_client.serializingr   Zgradio.components.baser	   r
   r   Zgradio.deprecationr   Zgradio.eventsr   r   r   r   r   r   r(   r(   r(   r,   <module>   s&   
