o
    e-                     @  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	dJdddddddddddddddddddKd#d$ZdLd'd(ZdMd*d+Zd,d- Zee	j
dddddddddddfdNd1d2ZdOd6d7Zd8d9 Zd:d; Zd<d= Zd>d? ZdPdCdDZddEdQdFdGZdHdI ZdS )R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
filterablelabelinfoevery
show_label	containerscale	min_widthinteractivevisibleelem_idelem_classeschoices>list[str | int | float | tuple[str, str | int | float]] | None=str | int | float | list[str | int | float] | Callable | Noner   Literal['value', 'index']r   bool | Noner   boolr   
int | Noner   r   
str | Noner   r   float | Noner   r   r    r!   intr"   r#   r$   r%   list[str] | str | Nonec                K  s   |r	dd |D ng | _ ddg}||vrtd| d| || _|| _|r.t|tr.|g}|s9|dur9td |sD|rDd	}td
 || _|| _	d| _
|| _|  	 tj| f||	|
|||||||||d| dS )a_
  
        Parameters:
            choices: A list of string options to choose from. An option can also be a tuple of the form (name, value), where name is the displayed name of the dropdown choice and value is the value to be passed to the function, or returned by the function.
            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.
            max_choices: maximum number of choices that can be selected. If None, no limit is enforced.
            filterable: If True, user will be able to type into the dropdown and filter the choices by typing. Can only be set to False if `allow_custom_value` is False.
            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  &   g | ]}t |tr|nt||fqS  
isinstancetuplestr.0cr2   r2   JD:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/dropdown.py
<listcomp>W      & 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.TzrThe `filterable` parameter cannot be set to False when `allow_custom_value` is True. Setting `filterable` to True.F)r   r   r   r   r   r    r!   r"   r#   r$   r%   r   )r&   
ValueErrorr   r   r4   r6   warningswarnr   r   Zinterpret_by_tokensr   r
   __init__)selfr&   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   kwargsZvalid_typesr2   r2   r:   rA   *   s\   .
zDropdown.__init__returndict[str, dict | bool]c                 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   rI   F)r   Zserialized_infor   r&   )rB   r   r2   r2   r:   api_info   s   

zDropdown.api_infodict[str, Any]c                 C  sf   | j r| jr| jd gng | jr| jd gdS g dS | jr$| jd nd | jr/| jd dS d dS )Nr   )rawZ
serializedrJ   rB   r2   r2   r:   example_inputs   s   zDropdown.example_inputsc                 C  s,   | j | j| j| j| j| j| jdt| S )N)r&   r   r   r   r   r   r   )	r&   r   r   r   r   r   r   r
   
get_configrN   r2   r2   r:   rP      s   zDropdown.get_config(Any | Literal[_Keywords.NO_VALUE] | None(str | list[str | tuple[str, str]] | Noneplaceholderc                 C  s:   |d u rd ndd |D }||||||||| |	|
|ddS )Nc                 S  r1   r2   r3   r7   r2   r2   r:   r;      r<   z#Dropdown.update.<locals>.<listcomp>update)r&   r   r   r   r   r    r!   r#   r   r"   rS   r   Z__type__r2   )r   r&   r   r   r   r   r   r    r!   r"   rS   r#   r2   r2   r:   rT      s$   zDropdown.updatex2str | int | float | list[str | int | float] | NoneEstr | int | float | list[str | int | float] | list[int | None] | Nonec                   s   | j dkr|S | j dkr8dd | jD  |du rdS | jr-t|ts$J  fdd|D S | v r6 |S dS td| j  d)	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=   c                 S     g | ]\}}|qS r2   r2   r8   _r   r2   r2   r:   r;          z'Dropdown.preprocess.<locals>.<listcomp>Nc                   s"   g | ]}| v r  |nd qS N)r=   )r8   choiceZchoice_valuesr2   r:   r;      s    zUnknown type: z'. Please choose from: 'value', 'index'.)r   r&   r   r4   listr=   r>   )rB   rU   r2   r^   r:   
preprocess   s   
	

zDropdown.preprocessc                 C  s4   | j s|dd | jD v rd S td| d d S )Nc                 S  rX   r2   r2   rY   r2   r2   r:   r;      r[   z4Dropdown._warn_if_invalid_choice.<locals>.<listcomp>zqThe value passed into gr.Dropdown() is not in the list of choices. Please update the list of choices to include: z  or set allow_custom_value=True.)r   r&   r?   r@   rB   yr2   r2   r:   _warn_if_invalid_choice   s
   
z Dropdown._warn_if_invalid_choicec                   s6   |d u rd S  j r fdd|D  |S  | |S )Nc                   s   g | ]}  |qS r2   )rc   )r8   Z_yrN   r2   r:   r;      s    z(Dropdown.postprocess.<locals>.<listcomp>)r   rc   ra   r2   rN   r:   postprocess   s   
zDropdown.postprocessc                 C  s   | S )z
        Calculates interpretation score of each choice by comparing the output against each of the outputs when alternative choices are selected.
        r2   rN   r2   r2   r:   set_interpret_parameters   s   z!Dropdown.set_interpret_parametersc                 C  s   t | j}|| |i fS r\   )r_   r&   remove)rB   rU   r&   r2   r2   r:   get_interpretation_neighbors  s   

z%Dropdown.get_interpretation_neighborsscoreslist[float | None]r_   c                 K  s   | | j|d |S )zs
        Returns:
            Each value represents the interpretation score corresponding to each choice.
        N)insertr&   r=   )rB   rU   Z	neighborsrh   rC   r2   r2   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   )rB   r   rC   r2   r2   r:   style  s   zDropdown.stylec                   s   t  fdd| jD d S )Nc                 3  s$    | ]}|d   kr|d V  qdS )   r   Nr2   r7   
input_datar2   r:   	<genexpr>  s   " z&Dropdown.as_example.<locals>.<genexpr>)nextr&   )rB   ro   r2   rn   r:   
as_example  s   zDropdown.as_exampler\   )$r&   r'   r   r(   r   r)   r   r*   r   r+   r   r,   r   r+   r   r-   r   r-   r   r.   r   r*   r   r+   r    r,   r!   r/   r"   r*   r#   r+   r$   r-   r%   r0   )rD   rE   )rD   rL   )r   rQ   r&   rR   r   r-   r   r-   r   r*   r   r*   r   r*   r    r,   r!   r,   r"   r*   rS   r-   r#   r*   )rU   rV   rD   rW   )rh   ri   rD   r_   )r   r*   )__name__
__module____qualname____doc__rA   rK   rO   rP   staticmethodr   ZNO_VALUErT   r`   rc   rd   re   rg   rk   rl   rr   r2   r2   r2   r:   r      s^    


^

#	

	r   )rv   
__future__r   r?   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   r2   r2   r2   r:   <module>   s(    

