
    %e85              	       ~   d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	 d dl
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 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#  ee$          Z% G d de          Z& G d d          Z'	 	 dde(dee(         dee(         de'fdZ) G d d          Z*dS )    N)AnyDictOptionalTypeUnion)	type_utilutil)current_form_id)StreamlitAPIException)
get_logger)
ArrowTable)
SpecialArg)Element)gather_metrics)get_script_run_ctx)NoValueregister_widget)compute_widget_id)to_bytesc                       e Zd ZdZdS )MarshallComponentExceptionzCClass for exceptions generated during custom component marshalling.N)__name__
__module____qualname____doc__     Blib/python3.11/site-packages/streamlit/components/v1/components.pyr   r   &   s        MMDr   r   c                      e Zd ZdZ	 	 ddedee         dee         fdZdefdZedee         fd	            Z	ddd
de
dee         de
fdZ ed          ddd
de
dee         de
fd            ZdefdZdefdZdefdZdS )CustomComponentzA Custom Component declaration.Nnamepathurlc                 ^    ||||t          d          || _        || _        || _        d S )Nz1Either 'path' or 'url' must be set, but not both.)r   r!   r"   r#   )selfr!   r"   r#   s       r   __init__zCustomComponent.__init__/   sF     LS[d.>3?'C   		r   returnc                 *    t          j        |           S Nr	   repr_r%   s    r   __repr__zCustomComponent.__repr__>       z$r   c                 \    | j         dS t          j                             | j                   S )z4The absolute path that the component is served from.N)r"   osabspathr,   s    r   r1   zCustomComponent.abspathA   s'     94wty)))r   defaultkeyr3   r4   c                "     | j         |||d|S )zAn alias for create_instance.r2   )create_instance)r%   r3   r4   argskwargss        r   __call__zCustomComponent.__call__H   s#     $t#T7NNvNNNr   r6   c                    t          |          dk    rt          d|d          d          	 ddl}ddlm n# t
          $ r t          d          w xY wt          |fi d}i }g |                                D ]\  }}	t          j
        |	          r?t                      }
||
_        t          |	          |
_                            |
           Xt          j        |	          rKt                      }||_                            |j        j        |	                               |           |	||<   	 t)          j        |          n"# t,          $ r}t          d|          d}~ww xY wd	t.          d
t0          t2          t4          t6                   f         f fd}t8          j        }t/                      } |||          }|                    d|j        |          }|S )a  Create a new instance of the component.

        Parameters
        ----------
        *args
            Must be empty; all args must be named. (This parameter exists to
            enforce correct use of the function.)
        default: any or None
            The default return value for the component. This is returned when
            the component's frontend hasn't yet specified a value with
            `setComponentValue`.
        key: str or None
            If not None, this is the user key we use to generate the
            component's "widget ID".
        **kwargs
            Keyword args to pass to the component.

        Returns
        -------
        any or None
            The component's widget value.

        r   z
Argument 'z' needs a labelN)component_arrowzTo use Custom Components in Streamlit, you need to install
PyArrow. To do so locally:

`pip install pyarrow`

And if you're using Streamlit Cloud, add "pyarrow" to your requirements.txt.r2   z(Could not convert component args to JSONelementr'   c                    j         j        _        t          |           j        _        j        j        j        _        fd}t                      }
C |             t          d
j         t          |           j        
|r|j        nd 	  	        }n6t          d
j         t          |           j        
|r|j        nd           }|j        _	        d	d}t          dj        
j         |d |          }|j        }

 |             |	}n*t          |t                    r                    |          }||nt          S )
Nc                  ^     j         _         j         j                                       d S r)   )component_instance	json_argsspecial_argsextend)r<   serialized_json_argsrA   s   r   marshall_element_argszZCustomComponent.create_instance.<locals>.marshall_component.<locals>.marshall_element_args   s.    7K*4*7>>|LLLLLr   r?   )user_keyr!   form_idr#   r4   r@   rA   page)rE   r!   rF   r#   r4   rG    c                     | S r)   r   )ui_value	widget_ids     r   deserialize_componentzZCustomComponent.create_instance.<locals>.marshall_component.<locals>.deserialize_component   s    r   c                     | S r)   r   )xs    r   <lambda>zMCustomComponent.create_instance.<locals>.marshall_component.<locals>.<lambda>   s    Q r   )element_typeelement_protorE   widget_func_namedeserializer
serializerctx)rH   )r!   r?   component_namer
   rF   r#   r   r   page_script_hashidr   value
isinstanceArrowTableProtoarrow_proto_to_dataframer   )dgr<   rD   rU   rX   rL   component_statewidget_valuer;   r3   r4   r%   rC   rA   s    `      r   marshall_componentz;CustomComponent.create_instance.<locals>.marshall_component   s   8<	G&51@1D1DG&.x#15*.M M M M M M M %&&C{%%'''&( +B//2!-14>--$
 
 
 '( +B//14>--$   -/G&)        .1%8!%2&;  O +0L%%'''#&L/:: V.GGUU
 $0#;<<Hr   r?   ) lenr   pyarrowstreamlit.components.v1r;   ImportErrorr   dictitemsr   is_bytes_liker   r4   r   bytesappendis_dataframe_likemarshallarrow_dataframedatajsondumps	Exceptionr   r   r   r   r   	streamlit_main_enqueuer?   )r%   r3   r4   r7   r8   rb   all_argsr@   arg_namearg_val	bytes_argdataframe_argexr`   r]   r<   return_valueresultr;   rC   rA   s   ```               @@@r   r6   zCustomComponent.create_instanceR   s   > t99q==,-R$q'-R-R-RSSS	NNN??????? 	 	 	'P  	 CCgc"B"BCC	!)!1!1 	. 	.Hg&w// .&LL	 (	"*7"3"3	##I....,W55 . *$,!(()F)KWUUU##M2222&-	(##	#':i#8#8   	 	 	,:B  	
L	IG L	Ic4=>P8Q L	I L	I L	I L	I L	I L	I L	I L	I L	I L	I L	I` _))))"g66 '"<l
 
 s!   
? AE 
E6!E11E6c                     t          |t                    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S )zEquality operator.)rZ   r    r!   r"   r#   r%   others     r   __eq__zCustomComponent.__eq__   sK     uo.. &	UZ'&	UZ'& EI%		
r   c                     | |k     S )zInequality operator.r   r}   s     r   __ne__zCustomComponent.__ne__  s    5=  r   c                 B    d| j          d| j        | j        n| j         S )N'z': r!   r"   r#   r,   s    r   __str__zCustomComponent.__str__  s)    S49SSdi.CSSSr   NN)r   r   r   r   strr   r&   r-   propertyr1   r   r9   r   r6   boolr   r   r   r   r   r   r    r    ,   s       ))
 #!	  sm c]	    #         *# * * * X* !	O O O O c]	O 
O O O O ^%&& !	c c c c c]	c 
c c c '&cJ
t 
 
 
 
!t ! ! ! !T T T T T T Tr   r    r!   r"   r#   r'   c                    t          j                    }|J |j        }|J t          j        |          }|J |j        }|dk    rUt          j        |          }t          j                            |          }t          j        	                    |          \  }}	| d|  }
t          |
||          }t                                                              |           |S )a  Create and register a custom component.

    Parameters
    ----------
    name: str
        A short, descriptive name for the component. Like, "slider".
    path: str or None
        The path to serve the component's frontend files from. Either
        `path` or `url` must be specified, but not both.
    url: str or None
        The URL that the component is served from. Either `path` or `url`
        must be specified, but not both.

    Returns
    -------
    CustomComponent
        A CustomComponent that can be called like a function.
        Calling the component will create a new instance of the component
        in the Streamlit app.

    N__main__.r   )inspectcurrentframef_back	getmoduler   getfiler0   r"   basenamesplitextr    ComponentRegistryinstanceregister_component)r!   r"   r#   current_framecaller_framemodulemodule_name	file_pathfilename_rV   	components               r   declare_componentr   	  s    8 (**M$$$ !'L### |,,F/K j  OL11	7##I..))(33Q $,,d,,N  ^$CHHHI  33I>>>r   c                       e Zd ZU  ej                    Zej        ed<   dZed          ed<   e	dd            Z
d ZdefdZdeddfd	Zd
edee         fdZdS )r   _instance_lockN	_instancer'   c                     | j         9| j        5  | j         t                      | _         ddd           n# 1 swxY w Y   | j         S )z'Returns the singleton ComponentRegistryN)r   r   r   )clss    r   r   zComponentRegistry.instanceI  s     = # 8 8=($5$7$7CM8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 }s   6::c                 D    i | _         t          j                    | _        d S r)   )_components	threadingLock_lockr,   s    r   r&   zComponentRegistry.__init__U  s    79^%%


r   c                 *    t          j        |           S r)   r*   r,   s    r   r-   zComponentRegistry.__repr__Y  r.   r   r   c                    |j         }|2t          j                            |          st	          d| d          | j        5  | j                            |j                  }|| j        |j        <   ddd           n# 1 swxY w Y   |"||k    rt          
                    d||           t                              d|           dS )zRegister a CustomComponent.

        Parameters
        ----------
        component : CustomComponent
            The component to register.
        NzNo such component directory: 'r   z&%s overriding previously-registered %szRegistered component %s)r1   r0   r"   isdirr   r   r   getr!   LOGGERwarningdebug)r%   r   r1   existings       r   r   z$ComponentRegistry.register_component\  s    #rw}}W'='='(S(S(S(STTTZ 	9 	9'++IN;;H/8DY^,	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 I$9$9NN8   	.	:::::s   /A>>BBr!   c                 N    | j                             |d          }||j        ndS )zReturn the filesystem path for the component with the given name.

        If no such component is registered, or if the component exists but is
        being served from a URL, return None instead.
        N)r   r   r1   )r%   r!   r   s      r   get_component_pathz$ComponentRegistry.get_component_pathw  s/     $((t44	$-$9y  tCr   )r'   r   )r   r   r   r   r   r   __annotations__r   r   classmethodr   r&   r   r-   r    r   r   r   r   r   r   r   E  s         %3Y^%5%5NIN555/3Ix+,333	 	 	 [	& & & #        ;O ; ; ; ; ;6Ds Dx} D D D D D Dr   r   r   )+r   rn   r0   r   typingr   r   r   r   r   rq   r   r	   streamlit.elements.formr
   streamlit.errorsr   streamlit.loggerr   streamlit.proto.Components_pb2r   r[   r   streamlit.proto.Element_pb2r   streamlit.runtime.metrics_utilr   streamlit.runtime.scriptrunnerr   streamlit.runtime.stater   r   streamlit.runtime.state.commonr   streamlit.type_utilr   r   r   r   r    r   r   r   r   r   r   <module>r      sE     				     3 3 3 3 3 3 3 3 3 3 3 3 3 3     % % % % % % % % 3 3 3 3 3 3 2 2 2 2 2 2 ' ' ' ' ' ' H H H H H H 5 5 5 5 5 5 / / / / / / 9 9 9 9 9 9 = = = = = = < < < < < < < < < < < < < < ( ( ( ( ( (	H			 	 	 	 	!6 	 	 	ZT ZT ZT ZT ZT ZT ZT ZT~ 9 9
9
3-9 
#9 	9 9 9 9x9D 9D 9D 9D 9D 9D 9D 9D 9D 9Dr   