
    &e.                       d dl mZ d dl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 d d	lmZmZ d d
lmZ d dlmZmZmZ erd dlmZ  G d de          ZddZddZd dZd!d"dZ  G d d          Z!dS )#    )annotationsN)TYPE_CHECKING
NamedTuplecast)Literal)runtime)StreamlitAPIException)	Block_pb2)gather_metrics)ScriptRunContextget_script_run_ctx)dg_stack)
WidgetArgsWidgetCallbackWidgetKwargs)DeltaGeneratorc                      e Zd ZU dZded<   dS )FormDataz%Form data stored on a DeltaGenerator.strform_idN)__name__
__module____qualname____doc____annotations__     7lib/python3.11/site-packages/streamlit/elements/form.pyr   r   !   s!         // LLLLLr   r   this_dgr   returnFormData | Nonec                    t          j                    sdS | j        | j        S | | j        k    r4t	          t          j                              D ]}|j        	|j        c S n| j        }||j        |j        S dS )zFind the FormData for the given DeltaGenerator.

    Forms are blocks, and can have other blocks nested inside them.
    To find the current form, we walk up the dg_stack until we find
    a DeltaGenerator that has FormData.
    N)r   exists
_form_data_main_dgreversedr   get_parent)r   dgparents      r   _current_formr+   (   s     > t%!!'""" 8<>>** 	% 	%B}(}$$$ )	% &"3"?$$4r   r)   r   c                6    t          |           }|dS |j        S )zReturn the form_id for the current form, or the empty string if we're
    not inside an `st.form` block.

    (We return the empty string, instead of None, because this value is
    assigned to protobuf message fields, and None is not valid.)
    N )r+   r   )r)   	form_datas     r   current_form_idr/   E   s%     b!!Irr   boolc                (    t          |           dk    S )z6True if the DeltaGenerator is inside an st.form block.r-   )r/   )r)   s    r   
is_in_formr2   R   s    2"$$r   user_key
str | Nonec                    | t          j        d|  d          }nt          j        d          }|                    d          S )Nz;
            There are multiple identical forms with `key='z'`.

            To fix this, please make sure that the `key` argument is unique for
            each `st.form` you create.
            a  
            There are multiple identical forms with the same generated key.

            When a form is created, it's assigned an internal key based on
            its structure. Multiple forms with an identical structure will
            result in the same internal key, which causes this error.

            To fix this error, please pass a unique `key` argument to
            `st.form`.
            
)textwrapdedentstrip)r3   messages     r   _build_duplicate_form_messager;   W   s^    /;C  
 
 /	
 
 ==r   c                      e Zd Z ed          	 d%ddd&d            Z ed          	 	 	 	 	 d'ddddd(d            Z	 	 	 	 	 d'ddddd d)d#Zed*d$            ZdS )+	FormMixinformFT)borderkeyr   clear_on_submitr0   r?   r    r   c                  ddl m} t          | j                  rt	          d           |d|d           |}t                      }|B||j        v}|r|j                            |           nt	          t          |                    t          j
                    }||j        _        ||j        _        ||j        _        | j                            |          }	t!          |          |	_        |	S )a  Create a form that batches elements together with a "Submit" button.

        A form is a container that visually groups other elements and
        widgets together, and contains a Submit button. When the form's
        Submit button is pressed, all widget values inside the form will be
        sent to Streamlit in a batch.

        To add elements to a form object, you can use "with" notation
        (preferred) or just call methods directly on the form. See
        examples below.

        Forms have a few constraints:

        * Every form must contain a ``st.form_submit_button``.
        * ``st.button`` and ``st.download_button`` cannot be added to a form.
        * Forms can appear anywhere in your app (sidebar, columns, etc),
          but they cannot be embedded inside other forms.
        * Within a form, the only widget that can have a callback function is
          ``st.form_submit_button``.

        Parameters
        ----------
        key : str
            A string that identifies the form. Each form must have its own
            key. (This key is not displayed to the user in the interface.)
        clear_on_submit : bool
            If True, all widgets inside the form will be reset to their default
            values after the user presses the Submit button. Defaults to False.
            (Note that Custom Components are unaffected by this flag, and
            will not be reset to their defaults on form submission.)
        border : bool
            Whether to show a border around the form. Defaults to True.

            .. note::
                Not showing a border can be confusing to viewers since interacting with a
                widget in the form will do nothing. You should only remove the border if
                there's another border (e.g. because of an expander) or the form is small
                (e.g. just a text input and a submit button).

        Examples
        --------
        Inserting elements using "with" notation:

        >>> import streamlit as st
        >>>
        >>> with st.form("my_form"):
        ...    st.write("Inside the form")
        ...    slider_val = st.slider("Form slider")
        ...    checkbox_val = st.checkbox("Form checkbox")
        ...
        ...    # Every form must have a submit button.
        ...    submitted = st.form_submit_button("Submit")
        ...    if submitted:
        ...        st.write("slider", slider_val, "checkbox", checkbox_val)
        ...
        >>> st.write("Outside the form")

        .. output::
           https://doc-form1.streamlit.app/
           height: 425px

        Inserting elements out of order:

        >>> import streamlit as st
        >>>
        >>> form = st.form("my_form")
        >>> form.slider("Inside the form")
        >>> st.slider("Outside the form")
        >>>
        >>> # Now add a submit button to the form:
        >>> form.form_submit_button("Submit")

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

        r   )check_session_state_rulesz&Forms cannot be nested in other forms.NF)default_valuer@   writes_allowed)streamlit.elements.utilsrC   r2   r)   r	   r   form_ids_this_runaddr;   r
   Blockr>   r   rA   r?   _blockr   r$   )
selfr@   rA   r?   rC   r   ctxnew_form_idblock_protoblock_dgs
             r   r>   zFormMixin.forms   s   d 	GFFFFFdg 	R'(PQQQ!!#eTTTT  ""?!)>>K P%))'2222+,I#,N,NOOOo''#* +:("(7>>+.. 'w//r   form_submit_buttonSubmitN	secondary)typedisableduse_container_widthlabelhelpr4   on_clickWidgetCallback | NoneargsWidgetArgs | NonekwargsWidgetKwargs | NonerS   Literal['primary', 'secondary']rT   rU   c                   t                      }	|dvrt          d| d          |                     |||||||||		  	        S )a/  Display a form submit button.

        When this button is clicked, all widget values inside the form will be
        sent to Streamlit in a batch.

        Every form must have a form_submit_button. A form_submit_button
        cannot exist outside a form.

        For more information about forms, check out our
        `blog post <https://blog.streamlit.io/introducing-submit-button-and-forms/>`_.

        Parameters
        ----------
        label : str
            A short label explaining to the user what this button is for.
            Defaults to "Submit".
        help : str or None
            A tooltip that gets displayed when the button is hovered over.
            Defaults to None.
        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.
        )primaryrR   z[The type argument to st.button must be "primary" or "secondary". 
The argument passed was "z".)	rV   rW   rX   rZ   r\   rS   rT   rU   rL   )r   r	   _form_submit_button)
rK   rV   rW   rX   rZ   r\   rS   rT   rU   rL   s
             r   rP   zFormMixin.form_submit_button   s    l !"" ///'5,05 5 5  
 '' 3 ( 

 

 
	
r   )rS   rT   rU   rL   rL   ScriptRunContext | Nonec                   t          | j                  }
d|
 d| }| j                            |||d|||||||	          S )NzFormSubmitter:-T)rV   r@   rW   is_form_submitterrX   rZ   r\   rS   rT   rU   rL   )r/   r)   _button)rK   rV   rW   rX   rZ   r\   rS   rT   rU   rL   r   submit_button_keys               r   ra   zFormMixin._form_submit_button-  sg     "$'**>W>>u>>w!" 3  
 
 	
r   c                "    t          d|           S )zGet our DeltaGenerator.r   )r   )rK   s    r   r)   zFormMixin.dgJ  s     $d+++r   )F)r@   r   rA   r0   r?   r0   r    r   )rQ   NNNN)rV   r   rW   r4   rX   rY   rZ   r[   r\   r]   rS   r^   rT   r0   rU   r0   r    r0   )rV   r   rW   r4   rX   rY   rZ   r[   r\   r]   rS   r^   rT   r0   rU   r0   rL   rb   r    r0   )r    r   )	r   r   r   r   r>   rP   ra   propertyr)   r   r   r   r=   r=   r   s       ^F05lIMl l l l l l\ ^()) *."&&*H
 1<$)H
 H
 H
 H
 H
 *)H
X *."&&*
 1<$)'+
 
 
 
 
 
: , , , X, , ,r   r=   )r   r   r    r!   )r)   r   r    r   )r)   r   r    r0   )N)r3   r4   r    r   )"
__future__r   r7   typingr   r   r   typing_extensionsr   	streamlitr   streamlit.errorsr	   streamlit.protor
   streamlit.runtime.metrics_utilr   streamlit.runtime.scriptrunnerr   r   1streamlit.runtime.scriptrunner.script_run_contextr   streamlit.runtime.stater   r   r   streamlit.delta_generatorr   r   r+   r/   r2   r;   r=   r   r   r   <module>ru      s   # " " " " "  2 2 2 2 2 2 2 2 2 2 % % % % % %       2 2 2 2 2 2 % % % % % % 9 9 9 9 9 9 O O O O O O O O F F F F F F L L L L L L L L L L 9888888    z      :
 
 
 
% % % %
    6[, [, [, [, [, [, [, [, [, [,r   