o
    e"                     @  s   d Z ddlmZ ddlZddlZddl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
 ddlmZmZmZ dd	lmZmZ dd
lmZmZ ed e G dd deeeeZdS )zgr.UploadButton() component.    )annotationsN)AnyCallableLiteral)utils)documentset_documentation_group)FileSerializable)	ComponentIOComponent	_Keywords)warn_deprecationwarn_style_method_deprecation)	Clickable
Uploadable	componentc                   @  s   e Zd ZdZ		d4ddddddddddddd5d!d"Zd#d$ Zeejdddddddfd6d(d)Z	d7d.d/Z
ddd0d8d2d3ZdS )9UploadButtona!  
    Used to create an upload button, when clicked allows a user to upload files that satisfy the specified file type or generic files (if file_type not set).
    Preprocessing: passes the uploaded file as a {file-object} or {List[file-object]} depending on `file_count` (or a {bytes}/{List{bytes}} depending on `type`)
    Postprocessing: expects function to return a {str} path to a file, or {List[str]} consisting of paths to files.
    Examples-format: a {str} path to a local file that populates the component.
    Demos: upload_button
    Upload a FileNZ	secondaryTfilesingle)variantvisiblesizescale	min_widthinteractiveelem_idelem_classestype
file_count
file_typeslabelstrvalue!str | list[str] | Callable | Noner   'Literal['primary', 'secondary', 'stop']r   boolr   Literal['sm', 'lg'] | Noner   
int | Noner   r   r   
str | Noner   list[str] | str | Noner   Literal['file', 'bytes']r   *Literal['single', 'multiple', 'directory']r    list[str] | Nonec                K  s   || _ || _|dkr|durtd |dur%t|ts%td|jj || _	|| _
|| _|| _tj| f|||	|
||||d| dS )a  
        Parameters:
            label: Text to display on the button. Defaults to "Upload a File".
            value: File or list of files to upload by default.
            variant: 'primary' for main call-to-action, 'secondary' for a more subdued style, 'stop' for a stop button.
            visible: If False, component will be hidden.
            size: Size of the button. Can be "sm" or "lg".
            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 False, the UploadButton will be in a disabled state.
            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.
            type: Type of value to be returned by component. "file" returns a temporary file object with the same base name as the uploaded file, whose full path can be retrieved by file_obj.name, "binary" returns an bytes object.
            file_count: if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
            file_types: List of type of files to be uploaded. "file" allows any file to be uploaded, "image" allows only image files to be uploaded, "audio" allows only audio files to be uploaded, "video" allows only video files to be uploaded, "text" allows only text files to be uploaded.
        	directoryNzGThe `file_types` parameter is ignored when `file_count` is 'directory'.z.Parameter file_types must be a list. Received )r!   r   r   r   r#   r   r   r   )r   r   warningswarn
isinstancelist
ValueError	__class____name__r   r    r!   r   r   __init__)selfr!   r#   r   r   r   r   r   r   r   r   r   r   r    kwargs r9   OD:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/upload_button.pyr6      s8   "

zUploadButton.__init__c              
   C  s4   | j | j| 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   r   r   r   r
   
get_configr7   r9   r9   r:   r;   \   s   
zUploadButton.get_config4str | list[str] | Literal[_Keywords.NO_VALUE] | None.Literal['primary', 'secondary', 'stop'] | Nonebool | Nonec              
   C  s   ||||| |||dd	S )Nupdate)	r   r   r   r   r#   r   r   r!   Z__type__r9   )r#   r!   r   r   r   r   r   r   r9   r9   r:   r@   j   s   zUploadButton.updatexlist[dict[str, Any]] | Nonereturn\bytes | tempfile._TemporaryFileWrapper | list[bytes | tempfile._TemporaryFileWrapper] | Nonec                   sf   |du rdS d
fdd j dkr!t|tr |d S  |S t|tr/ fdd	|D S  |S )z
        Parameters:
            x: List of JSON objects with filename as 'name' property and base64 data as 'data' property
        Returns:
            File objects in requested format
        NrC   &bytes | tempfile._TemporaryFileWrapperc                   s   | d | d |  dd}}} jdkrH|r |}nt|\}} j||d}tt|} j	
| tjd jd}||_||_|S  jdkro|rht|d	}| W  d    S 1 scw   Y  t|d
 S tdt j d )Nnamedatais_fileFr   )	file_name)deletedirbytesrbr   zUnknown type: z&. Please choose from: 'file', 'bytes'.)getr   Zmake_temp_copy_if_neededclient_utilsZdecode_base64_to_binaryZfile_bytes_to_filer"   r   abspathZ
temp_filesaddtempfileNamedTemporaryFileZDEFAULT_TEMP_DIRrF   Z	orig_nameopenreadr3   )frI   rG   rH   path_r   	file_datar<   r9   r:   process_single_file   s<   



 z4UploadButton.preprocess.<locals>.process_single_filer   r   c                   s   g | ]} |qS r9   r9   ).0rV   )rZ   r9   r:   
<listcomp>   s    z+UploadButton.preprocess.<locals>.<listcomp>)rC   rE   )r   r1   r2   )r7   rA   r9   )rZ   r7   r:   
preprocess   s   
 

zUploadButton.preprocess)
full_widthr   r^   c                K  s6   t   |durtd |rdnd| _|dur|| _| S )zc
        This method is deprecated. Please set these arguments in the constructor instead.
        NzoUse `scale` in place of full_width in the constructor. scale=1 will make the button expand, whereas 0 will not.   )r   r   r   r   )r7   r^   r   r8   r9   r9   r:   style   s   
zUploadButton.style)r   N)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   r?   r   r?   r   r(   r   r(   )rA   rB   rC   rD   )r^   r?   r   r'   )r5   
__module____qualname____doc__r6   r;   staticmethodr   ZNO_VALUEr@   r]   r`   r9   r9   r9   r:   r      s@    
=
?r   )rc   
__future__r   rR   r/   typingr   r   r   Zgradio_clientr   rO   Zgradio_client.documentationr   r   Zgradio_client.serializingr	   ZgradioZgradio.components.baser
   r   r   Zgradio.deprecationr   r   Zgradio.eventsr   r   r   r9   r9   r9   r:   <module>   s    