U
    /e                     @  s  d Z ddlmZ ddlZeeZddlmZm	Z	m
Z
mZmZmZmZ erTddlZnddlmZ edZddlmZ d	d
lmZmZmZmZmZmZmZmZmZ er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+ d	dl,m-Z- d	dlm.Z.m/Z/ dZ0er4eee) df Z1e
de	f Z2G dd dZ3G dd dZ4G dd dZ5G dd dZ6G dd dZ7G dd  d Z8G d!d" d"Z9G d#d$ d$Z:G d%d& d&Z;G d'd( d(Z<G d)d* d*e<Z=G d+d, d,e=Z>G d-d. d.e=Z?G d/d0 d0e=Z@G d1d2 d2e=ZAG d3d4 d4e=ZBG d5d6 d6e=ZCG d7d8 d8e=ZDG d9d: d:e=ZEG d;d< d<e<ZFG d=d> d>e<ZGe
e<gdf ZHdS )?a)   Provide events that represent various changes to Bokeh Documents.

These events are used internally to signal changes to Documents. For
information about user-facing (e.g. UI or tool) events, see the reference
for :ref:`bokeh.events`.

These events are employed for incoming and outgoing websocket messages and
internally for triggering callbacks. For example, the sequence of events that
happens when a user calls a Document API or sets a property resulting in a
"patch event" to the Document:

.. code-block::

    user invokes Document API
        -> Document API triggers event objects
        -> registered callbacks are executed
        -> Sesssion callback generates JSON message from event object
        -> Session sends JSON message over websocket

But events may also be triggered from the client, and arrive as JSON messages
over the transport layer, which is why the JSON handling and Document API must
be separated. Consider the alternative sequence of events:

.. code-block::

    Session recieves JSON message over websocket
        -> Document calls event.handle_json
        -> handle_json invokes appropriate Document API
        -> Document API triggers event objects
        -> registered callbacks are executed
        -> Session callback suppresses outgoing event

As a final note, message "ping-pong" is avoided by recording a "setter" when
events objects are created. If the session callback notes the event setter is
itself, then no further action (e.g. sending an outgoing change event identical
to the incoming event it just processed) is taken.

    )annotationsN)TYPE_CHECKINGAnyCallableListSetUnioncast   )import_optionalpandas)make_id   )	ColumnDataChangedColumnsPatchedColumnsStreamedDocumentPatchedMessageSentModelChanged	RootAddedRootRemovedTitleChanged)Setter)Unknown)Model)ColumnarDataSourceDataDict)	BufferRef)SessionCallback)Document)PatchesReferenceJson)ColumnDataChangedEventColumnsStreamedEventColumnsPatchedEventDocumentChangedEventDocumentPatchedEventModelChangedEventRootAddedEventRootRemovedEventSessionCallbackAddedSessionCallbackRemovedTitleChangedEventMessageSentEvent.c                   @  s   e Zd ZdddddZdS )DocumentChangedMixinr%   Noneeventreturnc                 C  s   d S N selfr1   r4   r4   9/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/document/events.py_document_changed       z&DocumentChangedMixin._document_changedN)__name__
__module____qualname__r8   r4   r4   r4   r7   r.      s   r.   c                   @  s   e Zd ZdddddZdS )DocumentPatchedMixinr&   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _document_patched   r9   z&DocumentPatchedMixin._document_patchedN)r:   r;   r<   r>   r4   r4   r4   r7   r=      s   r=   c                   @  s   e Zd ZdddddZdS )DocumentMessageSentMixinr-   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _document_message_sent   r9   z/DocumentMessageSentMixin._document_message_sentN)r:   r;   r<   r@   r4   r4   r4   r7   r?      s   r?   c                   @  s   e Zd ZdddddZdS )DocumentModelChangedMixinr'   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _document_model_changed   r9   z1DocumentModelChangedMixin._document_model_changedN)r:   r;   r<   rB   r4   r4   r4   r7   rA      s   rA   c                   @  s   e Zd ZdddddZdS )ColumnDataChangedMixinr"   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _column_data_changed   r9   z+ColumnDataChangedMixin._column_data_changedN)r:   r;   r<   rD   r4   r4   r4   r7   rC      s   rC   c                   @  s   e Zd ZdddddZdS )ColumnsStreamedMixinr#   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _columns_streamed   r9   z&ColumnsStreamedMixin._columns_streamedN)r:   r;   r<   rF   r4   r4   r4   r7   rE      s   rE   c                   @  s   e Zd ZdddddZdS )ColumnsPatchedMixinr$   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _columns_patched   r9   z$ColumnsPatchedMixin._columns_patchedN)r:   r;   r<   rH   r4   r4   r4   r7   rG      s   rG   c                   @  s   e Zd ZdddddZdS )SessionCallbackAddedMixinr*   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _session_callback_added   r9   z1SessionCallbackAddedMixin._session_callback_addedN)r:   r;   r<   rJ   r4   r4   r4   r7   rI      s   rI   c                   @  s   e Zd ZdddddZdS )SessionCallbackRemovedMixinr+   r/   r0   c                 C  s   d S r3   r4   r5   r4   r4   r7   _session_callback_removed   r9   z5SessionCallbackRemovedMixin._session_callback_removedN)r:   r;   r<   rL   r4   r4   r4   r7   rK      s   rK   c                   @  s`   e Zd ZU dZded< ded< ded< ddddd	d
ddZd ddddZdd	dddZdS )r%   zX Base class for all internal events representing a change to a
    Bokeh Document.

    r   documentSetter | NonesetterInvoker | Nonecallback_invokerNr/   )rM   rO   rQ   r2   c                 C  s   || _ || _|| _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                In the context of a Bokeh server application, incoming updates
                to properties will be annotated with the session that is
                doing the updating. This value is propagated through any
                subsequent change notifications that the update triggers.
                The session can compare the event setter to itself, and
                suppress any updates that originate from itself.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)

        N)rM   rO   rQ   )r6   rM   rO   rQ   r4   r4   r7   __init__   s    zDocumentChangedEvent.__init__boolr0   c                 C  s   dS )


        Fr4   r5   r4   r4   r7   combine   s    zDocumentChangedEvent.combiner   receiverr2   c                 C  s   t |drtt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._document_changed`` if it exists.

        r8   N)hasattrr	   r.   r8   r6   rW   r4   r4   r7   dispatch   s    
zDocumentChangedEvent.dispatch)NN)r:   r;   r<   __doc____annotations__rR   rU   rZ   r4   r4   r4   r7   r%      s   
r%   c                      s`   e Zd ZdZi Zdd Zddd fddZd	d
ddddZeddddddddZ	  Z
S )r&   z] A Base class for events that represent updating Bokeh Models and
    their properties.

    c                 C  s   | j | j| j< d S r3   )_handle_json	_handlerskind)clsr4   r4   r7   __init_subclass__   s    z&DocumentPatchedEvent.__init_subclass__r   r/   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._document_patched`` if it exists.

        r>   N)superrZ   rX   r	   r=   r>   rY   	__class__r4   r7   rZ      s    
zDocumentPatchedEvent.dispatch
Set[Model]Buffersr   
referencesbuffersr2   c                 C  s
   t  dS )a   Create a JSON representation of this event suitable for sending
        to clients.

        *Sub-classes must implement this method.*

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        N)NotImplementedErrorr6   rh   ri   r4   r4   r7   generate   s    zDocumentPatchedEvent.generater   List[ReferenceJson]r   doc
event_jsonrh   rO   r2   c                 C  s>   |d }t j|d}|dkr,td||| ||| dS )rT   r_   NzUnknown patch event )r&   r^   getRuntimeError)ro   rp   rh   rO   Z
event_kindhandlerr4   r4   r7   handle_json   s
    z DocumentPatchedEvent.handle_json)r:   r;   r<   r[   r^   ra   rZ   rl   staticmethodrt   __classcell__r4   r4   rc   r7   r&      s   
r&   c                      st   e Zd ZdZdZddddddd	 fd
dZddd fddZddddddZeddddddddZ	  Z
S )r-   z

    r   Nr   strzUnion[Any, bytes]rN   rP   )rM   msg_typemsg_datarO   rQ   c                   s    t  ||| || _|| _d S r3   )rb   rR   rx   ry   )r6   rM   rx   ry   rO   rQ   rc   r4   r7   rR     s    zMessageSentEvent.__init__r   r/   rV   c                   s*   t  | t|dr&tt||  d S )Nr@   )rb   rZ   rX   r	   r?   r@   rY   rc   r4   r7   rZ     s    
zMessageSentEvent.dispatchre   rf   rg   c                 C  sZ   t | j| jd d}t| jts*| j|d< n,|d k	s6tt }t|d| jf}|	| |S )N)r_   rx   ry   ry   )id)
r   r_   rx   
isinstancery   bytesAssertionErrorr   dictappend)r6   rh   ri   msgZ	buffer_idbufr4   r4   r7   rl     s    
zMessageSentEvent.generater   rm   r   rn   c                 C  s.   | j j|d g }|D ]}||d  qd S )Nrx   ry   )	callbacksZ_message_callbacksrq   )ro   rp   rh   rO   Zmessage_callbackscbr4   r4   r7   r]   (  s    zMessageSentEvent._handle_json)NNr:   r;   r<   r[   r_   rR   rZ   rl   ru   r]   rv   r4   r4   rc   r7   r-     s      r-   c                      s   e Zd ZdZdZd$ddddddd	d
dd	 fddZdddddZddd fddZddddddZe	dddd dd!d"d#Z
  ZS )%r'   a:   A concrete event representing updating an attribute and value of a
    specific Bokeh Model.

    This is the "standard" way of updating most Bokeh model attributes. For
    special casing situations that can optimized (e.g. streaming, etc.), a
    ``hint`` may be supplied that overrides normal mechanisms.

    r   Nr   r   rw   r   zUnknown | NonezDocumentPatchedEvent | NonerN   rP   )	rM   modelattroldnewserializable_newhintrO   rQ   c
           
        sT   |dkrt |ttfr|j}t |||	 || _|| _|| _|| _	|| _
|| _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            model (Model) :
                A Model to update

            attr (str) :
                The name of the attribute to update on the model.

            old (object) :
                The old value of the attribute

            new (object) :
                The new value of the attribute

            serializable_new (object) :
                A serialized (JSON) version of the new value. It may be
                ``None`` if a hint is supplied.

            hint (DocumentPatchedEvent, optional) :
                When appropriate, a secondary event may be supplied that
                modifies the normal update process. For example, in order
                to stream or patch data more efficiently than the standard
                update mechanism.

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)


        N)r{   r#   r$   rO   rb   rR   r   r   r   r   r   r   )
r6   rM   r   r   r   r   r   r   rO   rQ   rc   r4   r7   rR   :  s    -zModelChangedEvent.__init__r%   rS   r0   c                 C  s   t |tsdS | j|jkrdS | j|jkr.dS | jdk	rP|jdk	rP| j|jS | j|jkr| j|jkr|j| _|j	| _	|j
| _
dS dS )rT   FNT)r{   r'   rO   rM   r   rU   r   r   r   r   rQ   r5   r4   r4   r7   rU   q  s    
zModelChangedEvent.combiner   r/   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._document_model_changed`` if it exists.

        rB   N)rb   rZ   rX   r	   rA   rB   rY   rc   r4   r7   rZ     s    
zModelChangedEvent.dispatchre   rf   rg   c                 C  sp   ddl m} | jdk	r$| j||S | j}t||}| j |krL|| j  || t| j	| j j
| j|ddS )a   Create a JSON representation of this event suitable for sending
        to clients.

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        r
   )collect_modelsN)r_   r   r   r   r   )r   r   r   rl   r   setdiscardupdater   r_   refr   )r6   rh   ri   r   valueZ
value_refsr4   r4   r7   rl     s    



zModelChangedEvent.generater   rm   r   rn   c                 C  sx   |d d }|| j krH| j |r8td| d d S td| d| j | }|d }|d }|j||||d d S )	Nr   rz   Cannot apply patch to z? which is not in the document anymore. This is usually harmless which is not in the documentr   r   modelsrO   )r   seenlogtracerr   set_from_json)ro   rp   rh   rO   Z
patched_idZpatched_objr   r   r4   r4   r7   r]     s    

zModelChangedEvent._handle_json)NNN)r:   r;   r<   r[   r_   rR   rU   rZ   rl   ru   r]   rv   r4   r4   rc   r7   r'   .  s   	    $7
4r'   c                      st   e Zd ZdZdZddddddd	 fd
dZddd fddZddddddZeddddddddZ	  Z
S )r"   z A concrete event representing efficiently replacing *all*
    existing data for a :class:`~bokeh.models.sources.ColumnDataSource`

    r   Nr   r   zList[str] | NonerN   rP   )rM   column_sourcecolsrO   rQ   c                   s    t  ||| || _|| _dS )a]  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            column_source (ColumnDataSource) :

            cols (list[str]) :
                optional explicit list of column names to update. If None, all
                columns will be updated (default: None)

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)


        N)rb   rR   r   r   )r6   rM   r   r   rO   rQ   rc   r4   r7   rR     s    zColumnDataChangedEvent.__init__r   r/   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._column_data_changed`` if it exists.

        rD   N)rb   rZ   rX   r	   rC   rD   rY   rc   r4   r7   rZ     s    
zColumnDataChangedEvent.dispatchre   rf   rg   c                 C  s8   ddl m} || jj|| jd}t| j| jj|| jdS )a   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'          : 'ColumnDataChanged'
                'column_source' : <reference to a CDS>
                'new'           : <new data to steam to column_source>
                'cols'          : <specific columns to update>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.


        r
   )transform_column_source_data)ri   r   )r_   r   r   r   )util.serializationr   r   datar   r   r_   r   )r6   rh   ri   r   Z	data_dictr4   r4   r7   rl     s    zColumnDataChangedEvent.generater   rm   r   rn   c                 C  sN   |d d }|| j kr&td| d| j | }|d }|jd|||d d S )Nr   rz   r   r   r   r   r   )r   rr   r   )ro   rp   rh   rO   	source_idsourcer   r4   r4   r7   r]   0  s    

z#ColumnDataChangedEvent._handle_json)NNNr   r4   r4   rc   r7   r"     s         
)r"   c                      s   e Zd ZU dZdZded< ddddd	d
dd fddZddd fddZddddddZe	ddddddddZ
  ZS ) r#   z| A concrete event representing efficiently streaming new data
    to a :class:`~bokeh.models.sources.ColumnDataSource`

    r   r   r   Nr   r   zDataDict | pd.DataFramez
int | NonerN   rP   )rM   r   r   rolloverrO   rQ   c                   sJ   t  ||| || _tr:t tjr: fdd jD   | _|| _dS )al  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            column_source (ColumnDataSource) :
                The data source to stream new data to.

            data (dict or DataFrame) :
                New data to stream.

                If a DataFrame, will be stored as ``{c: df[c] for c in df.columns}``

            rollover (int) :
                A rollover limit. If the data source columns exceed this
                limit, earlier values will be discarded to maintain the
                column length under the limit.

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)

        c                   s   i | ]}| | qS r4   r4   ).0cr   r4   r7   
<dictcomp>i  s      z1ColumnsStreamedEvent.__init__.<locals>.<dictcomp>N)	rb   rR   r   pdr{   Z	DataFramecolumnsr   r   )r6   rM   r   r   r   rO   rQ   rc   r   r7   rR   C  s    "zColumnsStreamedEvent.__init__r   r/   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._columns_streamed`` if it exists.

        rF   N)rb   rZ   rX   r	   rE   rF   rY   rc   r4   r7   rZ   n  s    
zColumnsStreamedEvent.dispatchre   rf   rg   c                 C  s   t | j| jj| j| jdS )a   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'          : 'ColumnsStreamed'
                'column_source' : <reference to a CDS>
                'data'          : <new data to steam to column_source>
                'rollover'      : <rollover limit>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        )r_   r   r   r   )r   r_   r   r   r   r   rk   r4   r4   r7   rl   x  s    zColumnsStreamedEvent.generater   rm   r   rn   c                 C  sV   |d d }|| j kr&td| d| j | }|d }|dd }|||| d S )Nr   rz   zCannot stream to r   r   r   )r   rr   rq   _stream)ro   rp   rh   rO   r   r   r   r   r4   r4   r7   r]     s    

z!ColumnsStreamedEvent._handle_json)NN)r:   r;   r<   r[   r_   r\   rR   rZ   rl   ru   r]   rv   r4   r4   rc   r7   r#   9  s   
   +
$r#   c                      st   e Zd ZdZdZddddddd	 fd
dZddd fddZddddddZeddddddddZ	  Z
S )r$   z A concrete event representing efficiently applying data patches
    to a :class:`~bokeh.models.sources.ColumnDataSource`

    r   Nr   r   r    rN   rP   )rM   r   patchesrO   rQ   c                   s    t  ||| || _|| _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            column_source (ColumnDataSource) :
                The data source to apply patches to.

            patches (list) :

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)

        N)rb   rR   r   r   )r6   rM   r   r   rO   rQ   rc   r4   r7   rR     s    zColumnsPatchedEvent.__init__r   r/   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._columns_patched`` if it exists.

        rH   N)rb   rZ   rX   r	   rG   rH   rY   rc   r4   r7   rZ     s    
zColumnsPatchedEvent.dispatchre   rf   rg   c                 C  s   t | j| jj| jdS )a   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'          : 'ColumnsPatched'
                'column_source' : <reference to a CDS>
                'patches'       : <patches to apply to column_source>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        )r_   r   r   )r   r_   r   r   r   rk   r4   r4   r7   rl     s
    zColumnsPatchedEvent.generater   rm   r   rn   c                 C  sH   |d d }|| j kr&td| d| j | }|d }||| d S )Nr   rz   r   r   r   )r   rr   patch)ro   rp   rh   rO   r   r   r   r4   r4   r7   r]     s    

z ColumnsPatchedEvent._handle_json)NNr   r4   r4   rc   r7   r$     s      
"r$   c                      sn   e Zd ZdZdZdddddd fd	d
ZdddddZddddddZeddddddddZ	  Z
S )r,   zS A concrete event representing a change to the title of a Bokeh
    Document.

    r   Nr   rw   rN   rP   )rM   titlerO   rQ   c                   s   t  ||| || _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            title (str) :
                The new title to set on the Document

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)


        N)rb   rR   r   )r6   rM   r   rO   rQ   rc   r4   r7   rR   	  s    zTitleChangedEvent.__init__r%   rS   r0   c                 C  sB   t |tsdS | j|jkrdS | j|jkr.dS |j| _|j| _dS )rT   FT)r{   r,   rO   rM   r   rQ   r5   r4   r4   r7   rU   %  s    
zTitleChangedEvent.combinere   rf   rg   c                 C  s   t | j| jdS )a7   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'  : 'TitleChanged'
                'title' : <new title to set>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        )r_   r   )r   r_   r   rk   r4   r4   r7   rl   7  s    zTitleChangedEvent.generater   rm   r   r/   rn   c                 C  s   |  |d | d S )Nr   )	set_title)ro   rp   rh   rO   r4   r4   r7   r]   W  s    zTitleChangedEvent._handle_json)NN)r:   r;   r<   r[   r_   rR   rU   rl   ru   r]   rv   r4   r4   rc   r7   r,     s       r,   c                      s`   e Zd ZdZdZddddddd	 fd
dZddddddZeddddddddZ  Z	S )r(   zq A concrete event representing a change to add a new Model to a
    Document's collection of "root" models.

    r   Nr   r   rN   rP   r/   rM   r   rO   rQ   r2   c                   s   t  ||| || _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            model (Model) :
                The Bokeh Model to add as a Document root.

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)

        Nrb   rR   r   r6   rM   r   rO   rQ   rc   r4   r7   rR   c  s    zRootAddedEvent.__init__re   rf   rg   c                 C  s"   | | j  t| j| jjdS )a8   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'  : 'RootAdded'
                'title' : <reference to a Model>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        r_   r   )r   r   rh   r   r_   r   rk   r4   r4   r7   rl   }  s
    zRootAddedEvent.generater   rm   r   rn   c                 C  s$   |d d }|| }|  || d S Nr   rz   )Zadd_rootro   rp   rh   rO   Zroot_idZroot_objr4   r4   r7   r]     s    zRootAddedEvent._handle_json)NN
r:   r;   r<   r[   r_   rR   rl   ru   r]   rv   r4   r4   rc   r7   r(   [  s   !r(   c                      s`   e Zd ZdZdZddddddd	 fd
dZddddddZeddddddddZ  Z	S )r)   z| A concrete event representing a change to remove an existing Model
    from a Document's collection of "root" models.

    r   Nr   r   rN   rP   r/   r   c                   s   t  ||| || _dS )a  

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            model (Model) :
                The Bokeh Model to remove as a Document root.

            setter (ClientSession or ServerSession or None, optional) :
                This is used to prevent "boomerang" updates to Bokeh apps.
                (default: None)

                See :class:`~bokeh.document.events.DocumentChangedEvent`
                for more details.

            callback_invoker (callable, optional) :
                A callable that will invoke any Model callbacks that should
                be executed in response to the change that triggered this
                event. (default: None)


        Nr   r   rc   r4   r7   rR     s    zRootRemovedEvent.__init__re   rf   rg   c                 C  s   t | j| jjdS )a:   Create a JSON representation of this event suitable for sending
        to clients.

        .. code-block:: python

            {
                'kind'  : 'RootRemoved'
                'title' : <reference to a Model>
            }

        Args:
            references (dict[str, Model]) :
                If the event requires references to certain models in order to
                function, they may be collected here.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

            buffers (set) :
                If the event needs to supply any additional Bokeh protocol
                buffers, they may be added to this set.

                **This is an "out" parameter**. The values it contains will be
                modified in-place.

        r   )r   r_   r   r   rk   r4   r4   r7   rl     s    zRootRemovedEvent.generater   rm   r   rn   c                 C  s$   |d d }|| }|  || d S r   )Zremove_rootr   r4   r4   r7   r]     s    zRootRemovedEvent._handle_json)NNr   r4   r4   rc   r7   r)     s    r)   c                      s>   e Zd ZdZdddd fddZddd	 fd
dZ  ZS )r*   z A concrete event representing a change to add a new callback (e.g.
    periodic, timeout, or "next tick") to a Document.

    r   r   r/   rM   callbackr2   c                   s   t  | || _dS )z

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            callback (SessionCallback) :
                The callback to add

        Nrb   rR   r   r6   rM   r   rc   r4   r7   rR     s    zSessionCallbackAdded.__init__r   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._session_callback_added`` if
        it exists.

        rJ   N)rb   rZ   rX   r	   rI   rJ   rY   rc   r4   r7   rZ     s    
zSessionCallbackAdded.dispatchr:   r;   r<   r[   rR   rZ   rv   r4   r4   rc   r7   r*     s   r*   c                      s>   e Zd ZdZdddd fddZddd	 fd
dZ  ZS )r+   z A concrete event representing a change to remove an existing callback
    (e.g. periodic, timeout, or "next tick") from a Document.


    r   r   r/   r   c                   s   t  | || _dS )z

        Args:
            document (Document) :
                A Bokeh document that is to be updated.

            callback (SessionCallback) :
                The callback to remove

        Nr   r   rc   r4   r7   rR     s    zSessionCallbackRemoved.__init__r   rV   c                   s*   t  | t|dr&tt||  dS )z Dispatch handling of this event to a receiver.

        This method will invoke ``receiver._session_callback_removed`` if
        it exists.

        rL   N)rb   rZ   rX   r	   rK   rL   rY   rc   r4   r7   rZ   !  s    
zSessionCallbackRemoved.dispatchr   r4   r4   rc   r7   r+     s   r+   )Ir[   
__future__r   logging	getLoggerr:   r   typingr   r   r   r   r   r   r	   r   r   Zutil.dependenciesr   r   r   jsonr   r   r   r   r   r   r   r   r   Zcore.has_propsr   Z
core.typesr   r   r   Zmodels.sourcesr   r   Zprotocol.messager   Zserver.callbacksr   rM   r   r    r!   __all__rf   ZInvokerr.   r=   r?   rA   rC   rE   rG   rI   rK   r%   r&   r-   r'   r"   r#   r$   r,   r(   r)   r*   r+   ZDocumentChangeCallbackr4   r4   r4   r7   <module>   s\   *
$
,59) (dm[ZII 