o
    h#                     @  s  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rvd dl#m$Z$ d dl%m&Z& G dd deZ'G dd deZ(dS )    )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                   @  s   e Zd ZdZdS )MarshallComponentExceptionzCClass for exceptions generated during custom component marshalling.N)__name__
__module____qualname____doc__ r   r   \/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/components/v1/custom_component.pyr   (   s    r   c                   @  s^   e Zd ZdZdddddddZeddddddddZdddZdddZdddZ	dS )CustomComponentzA Custom Component declaration.Ndefaultkey	on_changer   r   r    
str | Noner!   WidgetCallback | Nonereturnc                O  s   | j ||||d|S )zAn alias for create_instance.r   )create_instance)selfr   r    r!   argskwargsr   r   r   __call__1   s   	zCustomComponent.__call__r%   c             
     sV  t |dkrtd|d  dzddl}ddlm  W n ty'   tdw t  t|d}i }g |	 D ]5\}	}
t
|
rSt }|	|_t|
|_| q:t|
rkt }|	|_ |jj|
 | q:|
||	< q:zt|W n ty } ztd|d}~ww d fdd}t j}t }|||}|d|j |S )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 JSONdgr   elementr   r$   r   c              	     s   j  j_t|  j_jd urj j_ fdd}t }d u r7|  tdt| j jd}ntdt| j jd}| j_ddd}t	 jj|d	d
 |dd}|j
}d ure|  |d u rm}|S t|trw|}|S )Nc                     s    j _ j j d S N)component_instance	json_argsspecial_argsextendr   )r,   serialized_json_argsr0   r   r   marshall_element_args   s   zZCustomComponent.create_instance.<locals>.marshall_component.<locals>.marshall_element_argsr.   )user_keyform_idnameurlr/   r0   )r4   r5   r6   r7    c                 S     | S r-   r   )ui_value	widget_idr   r   r   deserialize_component   s   zZCustomComponent.create_instance.<locals>.marshall_component.<locals>.deserialize_componentc                 S  r9   r-   r   )xr   r   r   <lambda>   s    zMCustomComponent.create_instance.<locals>.marshall_component.<locals>.<lambda>
json_value)deserializer
serializerctxon_change_handler
value_type)r8   )r6   r.   component_namer   r5   r7   r   r
   idr   value
isinstanceArrowTableProtoarrow_proto_to_dataframe)r+   r,   r3   rB   computed_idr<   component_statewidget_valuer*   r   r    r!   r&   r2   r0   )r,   r   marshall_component   sV   






z;CustomComponent.create_instance.<locals>.marshall_componentr.   )r+   r   r,   r   r$   r   )lenr   pyarrowstreamlit.components.v1r*   ImportErrorr   r	   dictitemsr   r   r    r   bytesappendr   marshallarrow_dataframedatajsondumps	Exceptionr   main_dgr   _enqueuer.   )r&   r   r    r!   r'   r(   rQ   all_argsr/   arg_namearg_val	bytes_argdataframe_argexrO   r+   r,   return_valuer   rN   r   r%   B   sP   "


G
zCustomComponent.create_instanceboolc                 C  s:   t |to| j|jko| j|jko| j|jko| j|jkS )zEquality operator.)rH   r   r6   pathr7   module_namer&   otherr   r   r   __eq__   s   




zCustomComponent.__eq__c                 C  s
   | |k S )zInequality operator.r   rj   r   r   r   __ne__   s   
zCustomComponent.__ne__strc                 C  s(   d| j  d| jd ur| j S | j S )N'z': )r6   rh   r7   )r&   r   r   r   __str__   s   (zCustomComponent.__str__)r   r   r    r"   r!   r#   r$   r   )r$   rg   )r$   rn   )
r   r   r   r   r)   r   r%   rl   rm   rp   r   r   r   r   r   .   s     


r   ))
__future__r   r[   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   rI   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   r   <module>   s,   