
    &eD                        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
 d dlmZ d dl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 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( d dl)m*Z*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0 ee+e*eee+e*f                  df         Z1g dZ2dee         deee+e*f                  fdZ3e G d d                      Z4 G d d          Z5dS )    )	dataclass)dedent)ListOptionalSequenceUnioncastoverload)LiteralN)config)current_form_id)check_callback_rulescheck_session_state_rules get_label_visibility_proto_value)FileUploaderState)UploadedFileInfo)FileUploader)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsregister_widget)compute_widget_id)DeletedFileUploadedFile)KeyLabelVisibilitymaybe_raise_label_warningsto_key))z.jpgz.jpeg)z.mpgz.mpeg)z.mp4z.mpeg4)z.tifz.tiff)z.htmz.htmlwidget_valuereturnc                    | g S t                      }|g S | j        }t          |          dk    rg S |j                            |j        d |D                       }d |D             }g }|D ]p}|                    |j                  }|+t          ||j	                  }|
                    |           I|
                    t          |j                             q|S )Nr   c                     g | ]	}|j         
S  file_id.0fs     Hlib/python3.11/site-packages/streamlit/elements/widgets/file_uploader.py
<listcomp>z%_get_upload_files.<locals>.<listcomp>K   s    888!)888    )
session_idfile_idsc                     i | ]
}|j         |S r&   r'   r)   s     r,   
<dictcomp>z%_get_upload_files.<locals>.<dictcomp>N   s    666!A666r.   )r   uploaded_file_infolenuploaded_file_mgr	get_filesr/   getr(   r   	file_urlsappendr   )	r"   ctxr3   file_recs_list	file_recscollected_filesr+   maybe_file_recuploaded_files	            r,   _get_upload_filesr@   ;   s    	


C
{	%8
!##	*44>88%7888 5  N
 76~666I>@O ; ;"qy11%(EEM""=1111"";qy#9#9::::r.   c                   J    e Zd ZU eed<   dee         dedefdZ	dedefdZ
dS )	FileUploaderSerdeaccept_multiple_filesui_value	widget_idr#   c                     t          |          }t          |          dk    r| j        rg nd }n| j        r|n|d         }|S )Nr   )r@   r4   rC   )selfrD   rE   upload_filesreturn_values        r,   deserializezFileUploaderSerde.deserializea   sZ     )22|!!484N.XbbTXLL !% :OQ  r.   filesc                 H   t                      }|s|S t          |t                    s|g}|D ]t}t          |t                    r|j                                        }|j        |_        |j        |_        |j        |_        |j	        
                    |j                   u|S N)FileUploaderStateProto
isinstancelistr   r3   addr(   namesizer8   CopyFrom
_file_urls)rG   rK   state_protor+   	file_infos        r,   	serializezFileUploaderSerde.serializen   s    ,.. 	E4(( 	GE 	7 	7A![)) /:/M/Q/Q/S/SI !	IVINVIN((6666r.   N)__name__
__module____qualname__bool__annotations__r   rN   strSomeUploadedFilesrJ   rX   r&   r.   r,   rB   rB   ]   sx          !78EH	   0 5K      r.   rB   c                   &   e Zd Ze	 	 	 	 	 dddddedeeee         df         ded         d	ee	         d
ee         dee
         dee         dee         dededeee                  fd            Ze	 	 	 	 	 	 dddddedeeee         df         ded         d	ee	         d
ee         dee
         dee         dee         dededee         fd            Zeddddddddddeded         deeee         df         d	ee	         d
ee         dee
         dee         dee         dededeee                  fd            Zedddddddddd	deded         deeee         df         d	ee	         d
ee         dee
         dee         dee         dededee         fd            Z ed          	 	 	 	 	 	 	 d ddddedeeee         df         ded	ee	         d
ee         dee
         dee         dee         dededeeee         df         fd            Z	 	 	 	 	 	 	 d dddddedeeee         df         ded	ee	         d
ee         dee
         dee         dee         dededee         deeee         df         fdZed!d            ZdS )"FileUploaderMixinNFvisible)disabledlabel_visibilitylabeltyperC   Tkeyhelp	on_changeargskwargsrc   rd   r#   c	                    d S rM   r&   rG   re   rf   rC   rg   rh   ri   rj   rk   rc   rd   s              r,   file_uploaderzFileUploaderMixin.file_uploader   	     	r.   c	                    d S rM   r&   rm   s              r,   rn   zFileUploaderMixin.file_uploader   ro   r.   )rf   rg   rh   ri   rj   rk   rc   rd   c       	             d S rM   r&   rG   re   rC   rf   rg   rh   ri   rj   rk   rc   rd   s              r,   rn   zFileUploaderMixin.file_uploader   ro   r.   )	rC   rf   rg   rh   ri   rj   rk   rc   rd   c       	             d S rM   r&   rr   s              r,   rn   zFileUploaderMixin.file_uploader   ro   r.   rn   c	                ^    t                      }|                     |||||||||	|
|          S )a  Display a file uploader widget.
        By default, uploaded files are limited to 200MB. You can configure
        this using the `server.maxUploadSize` config option. For more info
        on how to set config options, see
        https://docs.streamlit.io/library/advanced-features/configuration#set-configuration-options

        Parameters
        ----------
        label : str
            A short label explaining to the user what this file uploader 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, gray/grey, rainbow.

            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.

        type : str or list of str or None
            Array of allowed extensions. ['png', 'jpg']
            The default is None, which means all extensions are allowed.

        accept_multiple_files : bool
            If True, allows the user to upload multiple files at the same time,
            in which case the return value will be a list of files.
            Default: False

        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
            A tooltip that gets displayed next to the file uploader.

        on_change : callable
            An optional callback invoked when this file_uploader'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 file uploader if set to
            True. The default is False. This argument can only be supplied by
            keyword.
        label_visibility : "visible", "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".

        Returns
        -------
        None or UploadedFile or list of UploadedFile
            - If accept_multiple_files is False, returns either None or
              an UploadedFile object.
            - If accept_multiple_files is True, returns a list with the
              uploaded files as UploadedFile objects. If no files were
              uploaded, returns an empty list.

            The UploadedFile class is a subclass of BytesIO, and therefore
            it is "file-like". This means you can pass them anywhere where
            a file is expected.

        Examples
        --------
        Insert a file uploader that accepts a single file at a time:

        >>> import streamlit as st
        >>> import pandas as pd
        >>> from io import StringIO
        >>>
        >>> uploaded_file = st.file_uploader("Choose a file")
        >>> if uploaded_file is not None:
        ...     # To read file as bytes:
        ...     bytes_data = uploaded_file.getvalue()
        ...     st.write(bytes_data)
        >>>
        ...     # To convert to a string based IO:
        ...     stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
        ...     st.write(stringio)
        >>>
        ...     # To read file as string:
        ...     string_data = stringio.read()
        ...     st.write(string_data)
        >>>
        ...     # Can be used wherever a "file-like" object is accepted:
        ...     dataframe = pd.read_csv(uploaded_file)
        ...     st.write(dataframe)

        Insert a file uploader that accepts multiple files at a time:

        >>> import streamlit as st
        >>>
        >>> uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True)
        >>> for uploaded_file in uploaded_files:
        ...     bytes_data = uploaded_file.read()
        ...     st.write("filename:", uploaded_file.name)
        ...     st.write(bytes_data)

        .. output::
           https://doc-file-uploader.streamlit.app/
           height: 375px

        )re   rf   rC   rg   rh   ri   rj   rk   rc   rd   r:   )r   _file_uploader)rG   re   rf   rC   rg   rh   ri   rj   rk   rc   rd   r:   s               r,   rn   zFileUploaderMixin.file_uploader   sN    \ !"""""7- # 
 
 	
r.   )rd   rc   r:   r:   c	                P   t          |          }t          | j        |           t          d |d           t	          ||	           t          d||||||t          | j                  |r|j        nd 	  	        }|rwt          |t                    r|g}d |D             }d |D             }t          D ]?\  }}||v r||vr|                    |           ||v r||vr|                    |           @t                      }||_        ||_        ||ng |j        d d <   t!          j        d          |_        ||_        t          | j                  |_        |
|_        t-          |	          |j        _        |t3          |          |_        t7          |          }t9          d||||||j        |j        |	  	        }| j                            d|           t          |j        t@                    rd S t          |j        tB                    rd	 |j        D             S |j        S )
NF)default_valuerg   writes_allowedrn   )user_keyre   rf   rC   rg   rh   form_idpagec                 4    g | ]}|d          dk    r|nd| S )r   .r&   )r*   	file_types     r,   r-   z4FileUploaderMixin._file_uploader.<locals>.<listcomp>  s@        'q\S00		o)oo  r.   c                 6    g | ]}|                                 S r&   )lower)r*   ts     r,   r-   z4FileUploaderMixin._file_uploader.<locals>.<listcomp>  s     ,,,!AGGII,,,r.   zserver.maxUploadSize)ry   on_change_handlerrj   rk   deserializer
serializerr:   c                 <    g | ]}t          |t                    |S r&   )rO   r   r)   s     r,   r-   z4FileUploaderMixin._file_uploader.<locals>.<listcomp>  s'    TTT!A{9S9STATTTr.   )"r!   r   dgr   r    r   r   page_script_hashrO   r^   
TYPE_PAIRSr9   FileUploaderProtoidre   rf   r   
get_optionmax_upload_size_mbmultiple_filesrz   rc   r   rd   valuer   rh   rB   r   rJ   rX   _enqueuer   rP   )rG   re   rf   rC   rg   rh   ri   rj   rk   rd   rc   r:   r   xyfile_uploader_protoserdewidget_states                     r,   ru   z FileUploaderMixin._file_uploader{  s    SkkTWi000!#eTTTT"5*:;;;"7#DG,,),6%%$

 

 

  	#$$$ v !%  D
 -,t,,,D" # #199$KKNNN99$KKNNN/11!#$)!.2.>ddB #171B"2
 2
. .C*&5dg&>&>#'/$5U6
 6
,2 '-d||$!"788
 ''*

 

 

 	*=>>> l(+66 	U4*D11 	UTT|1TTTT!!r.   (streamlit.delta_generator.DeltaGeneratorc                 "    t          d|           S )zGet our DeltaGenerator.r   )r	   )rG   s    r,   r   zFileUploaderMixin.dg  s     >EEEr.   )NNNNN)FNNNNN)NFNNNNN)r#   r   )rY   rZ   r[   r
   r^   r   r   r   r   r   r   r   r   r\   r   r   r   rn   r   r   ru   propertyr   r&   r.   r,   ra   ra      s         "".2%))- ,5   C#,-  't}	
 c] sm N+ z" &  * 
$|$	%   X$ 
 16!".2%))- ,5   C#,-  'u~	
 c] sm N+ z" &  * 
,	   X.  15!".2%))-,5    't}	
 C#,- c] sm N+ z" &  * 
$|$	%   X$ 
 1604!".2%))-,5    'u~	
 C#,- c] sm N+ z" &  * 
,	   X  ^O$$ 15&+!".2%))-Z
 ,5Z
 Z
 Z
Z
 C#,-Z
  $	Z

 c]Z
 smZ
 N+Z
 z"Z
 &Z
 Z
 *Z
 
|T,/5	6Z
 Z
 Z
 %$Z
~ 15&+!".2%))-^" -6*.^" ^" ^"^" C#,-^"  $	^"
 c]^" sm^" N+^" z"^" &^" *^" ^" &'^" 
|T,/5	6^" ^" ^" ^"@ F F F XF F Fr.   ra   )6dataclassesr   textwrapr   typingr   r   r   r   r	   r
   typing_extensionsr   	streamlitr   streamlit.elements.formr   streamlit.elements.utilsr   r   r   streamlit.proto.Common_pb2r   rN   r   UploadedFileInfoProto streamlit.proto.FileUploader_pb2r   r   streamlit.runtime.metrics_utilr   streamlit.runtime.scriptrunnerr   r   streamlit.runtime.stater   r   r   r   streamlit.runtime.state.commonr   'streamlit.runtime.uploaded_file_managerr   r   streamlit.type_utilr   r   r    r!   r_   r   r@   rB   ra   r&   r.   r,   <module>r      s   " ! ! ! ! !       B B B B B B B B B B B B B B B B % % % % % %           3 3 3 3 3 3         
 S R R R R R P P P P P P N N N N N N 9 9 9 9 9 9 O O O O O O O O            = < < < < < M M M M M M M M X X X X X X X X X X X X|[(	)*
   
12	%k)
*+   D ! ! ! ! ! ! ! !H\F \F \F \F \F \F \F \F \F \Fr.   