o
    Yh'                     @  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   i/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/components/v1/custom_component.pyr   (   s    r   c                   @  sh   e Zd ZdZddddddddZedddddddddZdddZej	Z	dddZ
d ddZdS )!CustomComponentzA Custom Component declaration.Ndefaultkey	on_change	tab_indexargsr   r   r    
str | Noner!   WidgetCallback | Noner"   
int | Nonekwargsreturnc                O  s   | j |||||d|S )zAn alias for create_instance.r   )create_instance)selfr   r    r!   r"   r#   r'   r   r   r   __call__1   s   
zCustomComponent.__call__r)   c             
     s  t |dkrtd|d  ddur&ttr"tts"dks&tdzddl}ddlm  W n t	y=   tdw t
  t|d	}i }	g | D ]5\}
}t|rit }|
|_t||_| qPt|rt }|
|_ |jj| | qP||	|
< qPzt|	W n ty } ztd
|d}~ww d fdd}t 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".
        on_change: WidgetCallback or None
            An optional callback invoked when the widget's value changes. No arguments are passed to it.
        tab_index : int, optional
            Specifies the tab order of the iframe containing the component.
            Possible values are:
            - ``None`` (default): Browser default behavior.
            - ``-1``: Removes the iframe from the natural tab order, but it can still be focused programmatically.
            - ``0`` or positive integer: Includes the iframe in the natural tab order.
        **kwargs
            Keyword args to pass to the component.

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

        r   z
Argument 'z' needs a labelNz6tab_index must be None, -1, or a non-negative integer.)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_d ur j_d fdd}t }d u r>|  td| j jd}ntd| j jd}| j_	dd
d}t
 jj	|dd |dd}|j}d urj|  |d u rr}|S t|tr||}|S )Nr(   Nonec                     s    j _ j j d S N)component_instance	json_argsspecial_argsextendr   )r/   serialized_json_argsr4   r   r   marshall_element_args   s   zZCustomComponent.create_instance.<locals>.marshall_component.<locals>.marshall_element_argsr2   )user_keyr.   nameurlr3   r4   )r8   r.   r9   r:   ui_valuer   c                 S     | S r1   r   )r;   r   r   r   deserialize_component   s   zZCustomComponent.create_instance.<locals>.marshall_component.<locals>.deserialize_componentc                 S  r<   r1   r   )xr   r   r   <lambda>   s    zMCustomComponent.create_instance.<locals>.marshall_component.<locals>.<lambda>
json_value)deserializer
serializerctxon_change_handler
value_type)r(   r0   )r;   r   r(   r   )r9   r2   component_namer   form_idr:   r"   r   r
   idr   value
isinstanceArrowTableProtoarrow_proto_to_dataframe)r.   r/   r7   rC   computed_idr=   component_statewidget_valuer-   r   r    r!   r*   r6   r4   r"   )r/   r   marshall_component   sZ   






z;CustomComponent.create_instance.<locals>.marshall_componentr2   )r.   r   r/   r   r(   r   )lenr   rJ   intboolr   pyarrowstreamlit.components.v1r-   ImportErrorr	   dictitemsr   r   r    r   bytesappendr   marshallarrow_dataframedatajsondumps	Exceptionr   main_dgr   _enqueuer2   )r*   r   r    r!   r"   r#   r'   rU   all_argsr3   arg_namearg_val	bytes_argdataframe_argexrQ   r.   r/   return_valuer   rP   r   r)   D   sb   )


I
zCustomComponent.create_instanceotherobjectrT   c                 C  s:   t |to| j|jko| j|jko| j|jko| j|jkS )zEquality operator.)rJ   r   r9   pathr:   module_namer*   rk   r   r   r   __eq__   s   




zCustomComponent.__eq__c                 C  s
   | |k S )zInequality operator.r   ro   r   r   r   __ne__  s   
zCustomComponent.__ne__strc                 C  s(   d| j  d| jd ur| j S | j S )N'z': )r9   rm   r:   )r*   r   r   r   __str__	  s   (zCustomComponent.__str__)r#   r   r   r   r    r$   r!   r%   r"   r&   r'   r   r(   r   )rk   rl   r(   rT   )r(   rr   )r   r   r   r   r+   r   r)   rp   r   __hash__rq   rt   r   r   r   r   r   .   s$     
2

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   rK   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,   