a
    Df                     @  s  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlZddlmZ ddlm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mZmZ ddlm Z  ddl!m!Z! erddl"m#Z# ddlm$Z$ ddl%m&Z& ddl'm(Z( G dd dej)Z*dddddZ+d;dddddd d!Z,d<d"d#d$dd%d&d'Z-d=d"ddd$d)d*d+Z.d,d- Z/e0g d.Z1d>d"d/d0d1d2d3d4Z2ed?dd6d7d8d9d:Z3dS )@z*
Utilities for manipulating bokeh models.
    )annotationsN)contextmanager)TYPE_CHECKINGAnyIterableListOptionalSet)
Serializer)Document)ColumnDataChangedEventDocumentPatchedEventModelChangedEvent)	PatchJson)	DataModel)ColumnDataSourceFlexBoxModel)	patch_doc   )state)HoldPolicyType)DocumentChangedEvent)Message)Commc                   @  s0   e Zd ZdZdddddZdddddZd	S )
comparable_arrayz1
    Array subclass that allows comparisons.
    r   bool)otherreturnc                 C  s   t j| |ddS NT)Z	equal_nannpZarray_equalselfr    r$   [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/model.py__eq__*   s    zcomparable_array.__eq__c                 C  s   t j| |dd S r   r    r"   r$   r$   r%   __ne__-   s    zcomparable_array.__ne__N)__name__
__module____qualname____doc__r&   r'   r$   r$   r$   r%   r   %   s   r   zList[DocumentChangedEvent]None)eventsr   c                 C  sp   | D ]f}t t|ddtr$d|j_qt |trt |jtrt |jt	j
rt|jj|jj|j}||_||_qdS )zp
    Patch events applies patches to events that are to be dispatched
    avoiding various issues in Bokeh.
    hintN)
isinstancegetattrr   r.   colsr   modelr   newr!   ndarrayr   shapeZdtypeZserializable_new)r-   eZ	new_arrayr$   r$   r%   monkeypatch_events0   s    
r7   Tr   r   z$Optional[List[DocumentChangedEvent]]zMessage[Any] | None)docbinaryr-   r   c                   s   |du rt | jj}|rtjr"dS dd |D   s8dS t  t| jj|dt	
 d}t }t|d|i|} fdd| jjD | j_| jfdd	 |rjD ]}|| q|S )
z`
    Returns a json diff required to update an existing plot with
    the latest plot data.
    Nc                 S  s   g | ]}t |tr|qS r$   )r/   r   ).0eventr$   r$   r%   
<listcomp>P       zdiff.<locals>.<listcomp>)Z
referencesdeferred)r-   Zuse_buffersc                   s   g | ]}| vr|qS r$   r$   )r:   r6   )patch_eventsr$   r%   r<   X   r=   c                   s     |  S )N)Zhas_ref)r2   )
serializerr$   r%   <lambda>Y   r=   zdiff.<locals>.<lambda>)list	callbacksZ_held_eventsr   _holdr7   r
   modelsZsynced_referencesr   encoder   Zcreate_headerZflush_syncedbuffersZ
add_buffer)r8   r9   r-   Z
patch_jsonheadermsgbufferr$   )r?   r@   r%   diffD   s$    

rK   r   zDocument | NonezSet[Model] | None)objreplaceskipr   c                 C  sZ   t  }| dtiD ]@}|r&||v r&q|j}d|_|r@|| |rJ||_|| q|S )zI
    Removes the document from any previously displayed bokeh object
    typeN)setselectr   documentZ	_documentremove_rootadd)rL   rM   rN   rE   r2   Zprev_docr$   r$   r%   rS   _   s    
rS   F)rL   r8   holdrN   c                 C  s2   t | |d}||  |jjdu r.|r.|  |S )zX
    Adds a model to the supplied Document removing it from any existing Documents.
    )rN   N)rS   Zadd_rootrC   
hold_valuerU   )rL   r8   rU   rN   rE   r$   r$   r%   
add_to_docp   s
    
rW   c                 C  s   | di  dg D ]}| ddks| ddkr6q| d| d di}t|ts\q| d	i  D ]0\}}t|trld
d t| D |d	 |< qlqdS )zf
    Required for handling messages containing JSON serialized typed
    array from the frontend.
    contentr-   kindZModelChangedattrdataidr2   r3   c                 S  s   g | ]\}}|qS r$   r$   )r:   _vr$   r$   r%   r<      r=   z!patch_cds_msg.<locals>.<listcomp>N)getZ
select_oner/   r   itemsdictsorted)r2   rI   r;   Zcdscolvaluesr$   r$   r%   patch_cds_msg{   s    

re   )	childrentextnameZtoolbarZ	renderersZbelowcenterleftrightintzOptional[Iterable[str]]str)rL   depthignoredr   c                 C  s2  |du rt }ddlm} t| |r.| t } d}t| j}t| j	dd
 }g }|D ]b\}}	||v rjqXt|	trdt|	j }	nt|	}	t|	dkr|	dd d	 }	|d
||	f  qXd|}
t| tr|dj|d7 }| jD ]$}|tt||d dd d7 }q|d|
 7 }n|dj||
d7 }|S )zl
    Returns a string repr for a bokeh model, useful for recreating
    panel objects using pure bokeh.
    N   )Viewable F)Zinclude_defaultsz%s()   z...z%s=%sz, z{cls}(children=[
)clsr   )rn   z,
z  z], %s)z{cls}({props}))rt   props)_DEFAULT_IGNORED_REPRZviewablerq   r/   Zget_rootr   rO   r(   rb   Zproperties_with_valuesr`   r   reprlenappendjoinr   formatrf   textwrapindent
bokeh_repr)rL   rn   ro   rq   rrt   Z
propertiesru   kr^   Z
props_reprZ	child_objr$   r$   r%   r~      s4    




"r~   combiner   zComm | None)r8   policycommc              	   c  s   | pt j} | du rdV  dS | jj}zZ|du r8|   n
| | dV  W |rX|| j_q|durvddlm} || | |   n6|r|| j_n&|durddlm} || | |   0 dS )a  
    Context manager that holds events on a particular Document
    allowing them all to be collected and dispatched when the context
    manager exits. This allows multiple events on the same object to
    be combined if the policy is set to 'combine'.

    Arguments
    ---------
    doc: Document
        The Bokeh Document to hold events on.
    policy: HoldPolicyType
        One of 'combine', 'collect' or None determining whether events
        setting the same property are combined or accumulated to be
        dispatched when the context manager exits.
    comm: Comm
        The Comm to dispatch events on when the context manager exits.
    Nr   )push)	r   ZcurdocrC   rV   ZunholdrU   rD   Znotebookr   )r8   r   r   Zheldr   r$   r$   r%   rU      s,    







rU   )TN)NN)FN)r   N)r   N)4r+   
__future__r   r|   
contextlibr   typingr   r   r   r   r   r	   numpyr!   Zbokeh.core.serializationr
   Zbokeh.documentr   Zbokeh.document.eventsr   r   r   Zbokeh.document.jsonr   Zbokeh.modelr   Zbokeh.modelsr   r   r   Z!bokeh.protocol.messages.patch_docr   r   Zbokeh.core.enumsr   r   Zbokeh.protocol.messager   Zpyviz_commsr   r4   r   r7   rK   rS   rW   re   	frozensetrv   r~   rU   r$   r$   r$   r%   <module>   s:     $