
    g#                       d dl 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 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" erd dl#m$Z$ d dl%m&Z&  G d de      Z' G d de      Z(y)    )annotationsN)TYPE_CHECKINGAny)BaseCustomComponent)is_dataframe_like)get_dg_singleton_instance)current_form_id)check_cache_replay_rules)compute_and_register_element_id)StreamlitAPIException)
ArrowTable)
SpecialArg)Element)gather_metrics)get_script_run_ctx)register_widget)is_bytes_liketo_bytes)DeltaGenerator)WidgetCallbackc                      e Zd ZdZy)MarshallComponentExceptionzCClass for exceptions generated during custom component marshalling.N)__name__
__module____qualname____doc__     ]/var/www/openai/venv/lib/python3.12/site-packages/streamlit/components/v1/custom_component.pyr   r   (   s    Mr   r   c                  ~    e Zd ZdZdddd	 	 	 	 	 	 	 d
dZ ed      dddd	 	 	 	 	 	 	 d
d       ZddZddZdd	Z	y)CustomComponentzA Custom Component declaration.Ndefaultkey	on_changec               .     | j                   ||||d|S )zAn alias for create_instance.r"   )create_instance)selfr#   r$   r%   argskwargss         r   __call__zCustomComponent.__call__1   s/     $t##	

 
 	
r   r'   c               6    t        |      dkD  rt        d|d    d      	 ddl}ddlm t                t        |fi d}i }g |j                         D ]  \  }	}
t        |
      r3t               }|	|_        t        |
      |_        j                  |       Dt!        |
      rIt               }|	|_        j#                  |j$                  j&                  |
       j                  |       |
||	<    	 t)        j*                  |      d fd	}t/               j0                  }t3               } |||      }|j5                  d
|j6                         |S # t
        $ r t        d      w xY w# t,        $ r}t        d|      d}~ww xY w)aq  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".
        on_change: WidgetCallback or None
            An optional callback invoked when the widget's value changes. No arguments are passed to it.
        **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.)r#   r$   z(Could not convert component args to JSONc           	        j                   j                  _        t        |       j                  _        j
                  j
                  j                  _        fd}t               }
7 |        t        d
t        |       j                   j
                        }n-t        d
t        |       j                   j
                        }|j                  _        d	d}t        j                  j                  |d |d      }|j                  }
 |        |	}|S t        |t              rj                  |      }|S )
Nc                 r     j                   _         j                   j                  j                         y N)component_instance	json_argsspecial_argsextend)elementserialized_json_argsr3   s   r   marshall_element_argszZCustomComponent.create_instance.<locals>.marshall_component.<locals>.marshall_element_args   s+    7K**4**77>>|Lr   r1   )user_keyform_idnameurlr2   r3   )r8   r9   r:   r;   c                    | S r0   r   )ui_value	widget_ids     r   deserialize_componentzZCustomComponent.create_instance.<locals>.marshall_component.<locals>.deserialize_component   s    r   c                    | S r0   r   )xs    r   <lambda>zMCustomComponent.create_instance.<locals>.marshall_component.<locals>.<lambda>   s    Qr   
json_value)deserializer
serializerctxon_change_handler
value_type) )r:   r1   component_namer	   r9   r;   r   r   idr   value
isinstanceArrowTableProtoarrow_proto_to_dataframe)dgr5   r7   rF   computed_idr?   component_statewidget_valuer-   r#   r$   r%   r(   r6   r3   s    `      r   marshall_componentz;CustomComponent.create_instance.<locals>.marshall_component   s?   8<		G&&51@1DG&&.xx#15**.M %&C{%'=( +B/2!- >( +B/ -8G&&)  .**--2&"+'O +00L%'#&   L/:.GGUr   r1   )rP   r   r5   r   returnr   )lenr   pyarrowstreamlit.components.v1r-   ImportErrorr   r
   dictitemsr   r   r$   r   bytesappendr   marshallarrow_dataframedatajsondumps	Exceptionr   main_dgr   _enqueuer1   )r(   r#   r$   r%   r)   r*   rW   all_argsr2   arg_namearg_val	bytes_argdataframe_argexrT   rP   r5   return_valuer-   r6   r3   s   ````              @@@r   r'   zCustomComponent.create_instanceB   s   D t9q=,z$q'/-RSS	? 	!" Cgc"BC	!)!1HgW%&L	 (	"*7"3	##I."7+ *$,!(()F)F)K)KWU##M2&-	(# "2	#'::i#8 C	  C	 N '(00))"g6
('*D*DEi  	'P 	D  	,:B 	s#   
E&  E> &E;>	FFFc                    t        |t              xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S )zEquality operator.)rM   r!   r:   pathr;   module_namer(   others     r   __eq__zCustomComponent.__eq__   so     uo. 6		UZZ'6		UZZ'6 EII%6   E$5$55	
r   c                    | |k(   S )zInequality operator.r   rp   s     r   __ne__zCustomComponent.__ne__   s    
 5=  r   c                n    d| j                    d| j                  | j                   S | j                   S )N'z': )r:   rn   r;   )r(   s    r   __str__zCustomComponent.__str__   s6    499+Sdii.C RSS RSSr   )r#   r   r$   z
str | Noner%   zWidgetCallback | NonerU   r   )rU   bool)rU   str)
r   r   r   r   r+   r   r'   rr   rt   rw   r   r   r   r!   r!   .   s    )
 +/
 
 	

 )
 

" %& +/\ \ 	\
 )\ 
\ '\|
!Tr   r!   ))
__future__r   ra   typingr   r   0streamlit.components.types.base_custom_componentr   streamlit.dataframe_utilr   $streamlit.delta_generator_singletonsr   !streamlit.elements.lib.form_utilsr	   streamlit.elements.lib.policiesr
   streamlit.elements.lib.utilsr   streamlit.errorsr   streamlit.proto.Components_pb2r   rN   r   streamlit.proto.Element_pb2r   streamlit.runtime.metrics_utilr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   streamlit.runtime.stater   streamlit.type_utilr   r   streamlit.delta_generatorr   streamlit.runtime.state.commonr   r   r!   r   r   r   <module>r      sa    #  % P 6 J = D H 2 H 5 / 9 V 3 78=	!6 	ET) ETr   