
    &e!\                        U d dl Z 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mZ d dlmZ d dlmZmZ d dl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# 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/m0Z0 erd dl1m2Z2 dZ3ee4d<   e
e5e6e	ee j7        f         Z8e G d d                      Z9 G d d          Z:	 d de5de8dedee5         dee5         ddfdZ;dS )!    N)	dataclass)dedent)TYPE_CHECKINGBinaryIOOptionalTextIOUnioncast)FinalLiteral)runtime)current_form_id
is_in_form)check_callback_rulescheck_session_state_rules)StreamlitAPIException)Button)DownloadButton)
LinkButton)gather_metrics)ScriptRunContextget_script_run_ctx)
WidgetArgsWidgetCallbackWidgetKwargsregister_widget)compute_widget_id)Keyto_key)DeltaGeneratorz

For more information, refer to the
[documentation for forms](https://docs.streamlit.io/library/api-reference/control-flow/st.form).
FORM_DOCS_INFOc                   @    e Zd ZdedefdZd	dee         dedefdZdS )
ButtonSerdevreturnc                      t          |          S N)bool)selfr$   s     Alib/python3.11/site-packages/streamlit/elements/widgets/button.py	serializezButtonSerde.serialize6   s    Aww     ui_value	widget_idc                 
    |pdS )NF )r)   r.   r/   s      r*   deserializezButtonSerde.deserialize9   s     5 r,   N)r-   )__name__
__module____qualname__r(   r+   r   strr2   r1   r,   r*   r#   r#   4   sh        4 D    ! !HTN !s !D ! ! ! ! ! !r,   r#   c                      e Zd Z ed          	 	 	 	 	 d#dddddedee         dee         d	ee         d
ee         dee	         de
d         dededefd            Z ed          	 	 	 	 	 	 	 d$dddddededee         dee         dee         dee         d	ee         d
ee         dee	         de
d         dededefd            Z ed          ddddddededee         de
d         dededdfd            Z	 	 	 	 	 	 	 d$ddddddededee         dee         dee         dee         d	ee         d
ee         dee	         de
d         dededee         defdZdddddededee         de
d         dededdfdZ	 	 	 d%ddddddedee         dee         d ed	ee         d
ee         dee	         de
d         dededee         defd!Zed&d"            ZdS )'ButtonMixinbuttonN	secondaryF)typedisableduse_container_widthlabelkeyhelpon_clickargskwargsr;   primaryr:   r<   r=   r%   c                    t          |          }t                      }
|dvrt          d| d          | j                            |||d||||||	|
          S )am  Display a button widget.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this button is for.
            The label can optionally contain Markdown and supports the following
            elements: Bold, Italics, Strikethroughs, Inline Code, and Emojis.

            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``.
        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 when the button is
            hovered over.
        on_click : callable
            An optional callback invoked when this button is clicked.
        args : tuple
            An optional tuple of args to pass to the callback.
        kwargs : dict
            An optional dict of kwargs to pass to the callback.
        type : "secondary" or "primary"
            An optional string that specifies the button type. Can be "primary" for a
            button with additional emphasis or "secondary" for a normal button. Defaults
            to "secondary".
        disabled : bool
            An optional boolean, which disables the button if set to True. The
            default is False.
        use_container_width: bool
            An optional boolean, which makes the button stretch its width to match the parent container.

        Returns
        -------
        bool
            True if the button was clicked on the last run of the app,
            False otherwise.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> st.button("Reset", type="primary")
        >>> if st.button('Say hello'):
        ...     st.write('Why hello there')
        ... else:
        ...     st.write('Goodbye')

        .. output::
           https://doc-buton.streamlit.app/
           height: 220px

        rD   [The type argument to st.button must be "primary" or "secondary". 
The argument passed was "".F)is_form_submitterrA   rB   rC   r<   r;   r=   ctx)r   r   r   dg_button)r)   r>   r?   r@   rA   rB   rC   r;   r<   r=   rJ   s              r*   r9   zButtonMixin.button>   s    j Skk "" ///'5,05 5 5  
 w# 3  
 
 	
r,   download_buttondata	file_namemimec
                    t                      }|
dvrt          d|
 d          |                     |||||||||	||
||          S )a  Display a download button widget.

        This is useful when you would like to provide a way for your users
        to download a file directly from your app.

        Note that the data to be downloaded is stored in-memory while the
        user is connected, so it's a good idea to keep file sizes under a
        couple hundred megabytes to conserve memory.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this button is for.
            The label can optionally contain Markdown and supports the following
            elements: Bold, Italics, Strikethroughs, Inline Code, and Emojis.

            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``.
        data : str or bytes or file
            The contents of the file to be downloaded. See example below for
            caching techniques to avoid recomputing this data unnecessarily.
        file_name: str
            An optional string to use as the name of the file to be downloaded,
            such as 'my_file.csv'. If not specified, the name will be
            automatically generated.
        mime : str or None
            The MIME type of the data. If None, defaults to "text/plain"
            (if data is of type *str* or is a textual *file*) or
            "application/octet-stream" (if data is of type *bytes* or is a
            binary *file*).
        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 when the button is
            hovered over.
        on_click : callable
            An optional callback invoked when this button is clicked.
        args : tuple
            An optional tuple of args to pass to the callback.
        kwargs : dict
            An optional dict of kwargs to pass to the callback.
        type : "secondary" or "primary"
            An optional string that specifies the button type. Can be "primary" for a
            button with additional emphasis or "secondary" for a normal button. Defaults
            to "secondary".
        disabled : bool
            An optional boolean, which disables the download button if set to
            True. The default is False.
        use_container_width: bool
            An optional boolean, which makes the button stretch its width to match the
            parent container.


        Returns
        -------
        bool
            True if the button was clicked on the last run of the app,
            False otherwise.

        Examples
        --------
        Download a large DataFrame as a CSV:

        >>> import streamlit as st
        >>>
        >>> @st.cache
        ... def convert_df(df):
        ...     # IMPORTANT: Cache the conversion to prevent computation on every rerun
        ...     return df.to_csv().encode('utf-8')
        >>>
        >>> csv = convert_df(my_large_df)
        >>>
        >>> st.download_button(
        ...     label="Download data as CSV",
        ...     data=csv,
        ...     file_name='large_df.csv',
        ...     mime='text/csv',
        ... )

        Download a string as a file:

        >>> import streamlit as st
        >>>
        >>> text_contents = '''This is some text'''
        >>> st.download_button('Download some text', text_contents)

        Download a binary file:

        >>> import streamlit as st
        >>>
        >>> binary_contents = b'example content'
        >>> # Defaults to 'application/octet-stream'
        >>> st.download_button('Download binary file', binary_contents)

        Download an image:

        >>> import streamlit as st
        >>>
        >>> with open("flower.png", "rb") as file:
        ...     btn = st.download_button(
        ...             label="Download image",
        ...             data=file,
        ...             file_name="flower.png",
        ...             mime="image/png"
        ...           )

        .. output::
           https://doc-download-buton.streamlit.app/
           height: 335px

        rD   rG   rH   )r>   rN   rO   rP   r?   r@   rA   rB   rC   r<   r;   r=   rJ   )r   r   _download_button)r)   r>   rN   rO   rP   r?   r@   rA   rB   rC   r;   r<   r=   rJ   s                 r*   rM   zButtonMixin.download_button   s    d !""///'5,05 5 5  
 $$ 3 % 
 
 	
r,   link_button)r@   r;   r<   r=   urlr    c                f    |dvrt          d| d          |                     ||||||          S )a8	  Display a link button element.

        When clicked, a new tab will be opened to the specified URL. This will
        create a new session for the user if directed within the app.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this button is for.
            The label can optionally contain Markdown and supports the following
            elements: Bold, Italics, Strikethroughs, Inline Code, and Emojis.

            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``.
        url : str
            The url to be opened on user click
        help : str
            An optional tooltip that gets displayed when the button is
            hovered over.
        type : "secondary" or "primary"
            An optional string that specifies the button type. Can be "primary" for a
            button with additional emphasis or "secondary" for a normal button. Defaults
            to "secondary".
        disabled : bool
            An optional boolean, which disables the link button if set to
            True. The default is False.
        use_container_width: bool
            An optional boolean, which makes the button stretch its width to match the
            parent container.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> st.link_button("Go to gallery", "https://streamlit.io/gallery")

        .. output::
           https://doc-link-button.streamlit.app/
           height: 200px

        rD   z`The type argument to st.link_button must be "primary" or "secondary". 
The argument passed was "rH   )r>   rT   r@   r<   r;   r=   )r   _link_button)r)   r>   rT   r@   r;   r<   r=   s          r*   rS   zButtonMixin.link_buttonU  sk    H ///'7.27 7 7  
    3 ! 
 
 	
r,   )r;   r<   r=   rJ   rJ   c
                r   t          |          }t          d |d           t          d|||||||
||r|j        nd 
  
        }t	          | j                  rt          dt                     t                      }||_	        ||_
        ||_        d|_        |
|_        t          | j                                        ||||           ||_        |t#          |          |_        t'                      }t)          d|||||	|j        |j        |	  	        }| j                            d|           |j        S )NFdefault_valuer?   writes_allowedrM   )	user_keyr>   rO   rP   r?   r@   r;   r=   pagez7`st.download_button()` can't be used in an `st.form()`.r[   on_change_handlerrB   rC   deserializer
serializerrJ   )r   r   r   page_script_hashr   rK   r   r!   DownloadButtonProtoidr=   r>   defaultr;   marshall_file_get_delta_path_strr<   r   r@   r#   r   r2   r+   _enqueuevalue)r)   r>   rN   rO   rP   r?   r@   rA   rB   rC   r;   r<   r=   rJ   rc   download_button_protoserdebutton_states                     r*   rR   zButtonMixin._download_button  s}   " Skk!#eTTTT 3),6%%$
 
 
 dg 	'Z.ZZ   !4 5 5#% 4G1&+#(-%%)"G''))41Fi	
 	
 	
 *2&)/!&&!&*

 

 

 	*,ABBB!!r,   c                    t                      }||_        ||_        ||_        ||_        ||_        |t          |          |_        | j        	                    d|          S )NrS   )
LinkButtonProtor>   rT   r;   r=   r<   r   r@   rK   rg   )r)   r>   rT   r@   r;   r<   r=   link_button_protos           r*   rV   zButtonMixin._link_button  sk     ,--"' #!%0C-%-"%+D\\"w/@AAAr,   rI   c                   |st          | j        |           t          d |d           t          d|||||||
|r|j        nd 	  	        }t          j                    rZt          | j                  r|st          dt                     t          | j                  s|rt          dt                     t                      }||_        ||_        d|_        ||_        t          | j                  |_        ||_        |
|_        |	|_        |t)          |          |_        t-                      }t/          d||||||j        |j        |	  	        }| j                            d|           |j        S )NFrX   r9   )r[   r>   r?   r@   rI   r;   r=   r\   z.`st.button()` can't be used in an `st.form()`.z=`st.form_submit_button()` must be used inside an `st.form()`.r]   )r   rK   r   r   ra   r   existsr   r   r!   ButtonProtorc   r>   rd   rI   r   form_idr;   r=   r<   r   r@   r#   r   r2   r+   rg   rh   )r)   r>   r?   r@   rI   rA   rB   rC   r;   r<   r=   rJ   rc   button_protorj   rk   s                   r*   rL   zButtonMixin._button  s    ! 	4 (333!#eTTTT/ 3),6%%$

 

 

" > 	$'"" +< +U^UU    (( -> +dTbdd   #}}"$):&.tw77 +>( ( &tL&&*

 

 

 	<000!!r,   c                 "    t          d|           S )zGet our DeltaGenerator.r    )r
   )r)   s    r*   rK   zButtonMixin.dgP  s     $d+++r,   )NNNNN)NNNNNNN)NNN)r%   r    )r3   r4   r5   r   r6   r   r   r   r   r   r   r(   r9   DownloadButtonDataTyperM   rS   r   rR   rV   rL   propertyrK   r1   r,   r*   r8   r8   =   sO       ^H ""-1%))-j
 1<$)j
 j
 j
j
 c]j
 sm	j

 >*j
 z"j
 &j
 ,-j
 j
 "j
 
j
 j
 j
 j
X ^%&&
 $("!"-1%))-g
 1<$)g
 g
 g
g
 %g
 C=	g

 smg
 c]g
 smg
 >*g
 z"g
 &g
 ,-g
 g
 "g
 
g
 g
 g
 '&g
R ^M"" #0;$)P
 P
 P
P
 P

 smP
 ,-P
 P
 "P
 
P
 P
 P
 #"P
l $("!"-1%))-C" 1<$)*.C" C" C"C" %C" C=	C"
 smC" c]C" smC" >*C" z"C" &C" ,-C" C" "C" &'C"  
!C" C" C" C"V 1<$)B B BB B sm	B ,-B B "B 
B B B B8 .2%))-K" 1<$)*.K" K" K"K" c]K" sm	K"
  K" >*K" z"K" &K" ,-K" K" "K" &'K" 
K" K" K" K"Z , , , X, , ,r,   r8   coordinatesrN   proto_download_buttonmimetyperO   r%   c                    t          |t                    r|                                }|pd}n]t          |t          j                  r.|                                }|                                }|pd}nt          |t                    r|}|pd}nt          |t          j                  r.|                    d           |	                                }|pd}nt          |t          j
                  r.|                    d           |                                }|pd}nit          |t          j                  r0|                    d           |                                pd}|pd}nt          dt          |          z            t          j                    r1t          j                    j                            ||| |d          }nd}||_        d S )	Nz
text/plainzapplication/octet-streamr   r,   zInvalid binary data format: %sT)rO   is_for_static_downloadr-   )
isinstancer6   encodeioTextIOWrapperreadbytesBytesIOseekgetvalueBufferedReader	RawIOBaseRuntimeErrorr;   r   rp   get_instancemedia_file_mgraddrT   )rw   rN   rx   ry   rO   data_as_bytesstring_datafile_urls           r*   re   re   V  s    $ J+|	D"*	+	+ Jiikk#**,,+|	D%	 	  J99	D"*	%	% J		!99	D"+	,	, 	J		!		99	D",	'	' J		!		*s99;d4jjHIII~ 
'))8<<#' = 
 
  (r,   r'   )<r~   dataclassesr   textwrapr   typingr   r   r   r   r	   r
   typing_extensionsr   r   	streamlitr   streamlit.elements.formr   r   streamlit.elements.utilsr   r   streamlit.errorsr   streamlit.proto.Button_pb2r   rq   "streamlit.proto.DownloadButton_pb2r   rb   streamlit.proto.LinkButton_pb2r   rm   streamlit.runtime.metrics_utilr   streamlit.runtime.scriptrunnerr   r   streamlit.runtime.stater   r   r   r   streamlit.runtime.state.commonr   streamlit.type_utilr   r   streamlit.delta_generatorr    r!   __annotations__r6   r   r   ru   r#   r8   re   r1   r,   r*   <module>r      s   
				 ! ! ! ! ! !       I I I I I I I I I I I I I I I I , , , , , , , ,       ? ? ? ? ? ? ? ? T T T T T T T T 2 2 2 2 2 2 < < < < < < T T T T T T H H H H H H 9 9 9 9 9 9 O O O O O O O O            = < < < < < + + + + + + + + 9888888    sE68R\IJ  ! ! ! ! ! ! ! !V, V, V, V, V, V, V, V,|  $.) .).)
 .) /.) sm	.)
 }.) 
.) .) .) .) .) .)r,   