
    -eh`                       U d Z ddlmZ ddlZ ej        e          ZddlZddlZddl	Z	ddl
mZmZmZmZmZmZmZmZmZ ddlmZ er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! er2ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl m2Z2 dZ3dZ4dZ5dZ6dZ7dZ8ed         Z9 G d d          Z: G d de          Z; G d  d!e          Z< G d" d#e          Z= G d$ d%e          Z>	 dwdxd0Z?dddd1dyd8Z@dzd>ZAd{dAZBd|dEZCd}dFZD	 	 d~ddNZEdddOddVZFeeeGdf         geHf         ZIdWeJdX<   e7dfdd`ZKeddc            ZLedde            ZLdddfZLi ZMdgeJdh<   daNdIeJdi<   dddpZOddrZPddtZQdduZRddvZSdS )z

    )annotationsN)	TYPE_CHECKINGAnyCallableLiteralProtocol	TypedDictUnioncastoverload)uuid4Comm)	TypeAlias   )ID)make_id)warn   )curstate)Application)Document)ColumnDataChangedEventColumnsPatchedEventColumnsStreamedEventDocumentPatchedEventModelChangedEvent)Bundle)Model	Resources)State	text/htmlzapplication/javascriptz$application/vnd.bokehjs_load.v0+jsonz$application/vnd.bokehjs_exec.v0+jsonzlocalhost:8888)CommsHandledestroy_server	get_commsinstall_notebook_hookinstall_jupyter_hooksload_notebookpublish_display_datapush_notebookrun_notebook_hookshow_appshow_doc)jupyterzeppelinc                      e Zd ZU dZi Zded<   ded<   ded<   ddZddZedd            Z	edd            Z
d dZd!dZd"dZd#dZdS )$r$   

    r   _json
int | None_cellnor   _doccommsr   cell_docreturnNonec                   d | _         	 ddlm}  |            }|J |j        }|J t	          t          |                    dd                              d         }|| _         n2# t          $ r%}t          	                    d|           Y d }~nd }~ww xY w|| _
        || _        | j                                         d S )Nr   )get_ipythonr   T)include_latestz.Could not get Notebook cell number, reason: %s)r5   IPythonr<   history_managernextiterget_tail	Exceptionlogdebug_commsr6   hold)selfr7   r8   r<   iphmp_promptes           1lib/python3.11/site-packages/bokeh/io/notebook.py__init__zCommsHandle.__init__l   s    		K++++++B>>>#B>>>DQt!D!DEEFFqIH#DLL 	K 	K 	KIIFJJJJJJJJ	K 	
 		s   AA' '
B1BBstrc                *    | j         d| j          dS dS )Nz2<p><code>&lt;Bokeh Notebook handle for <strong>In[z]</strong>&gt;</code></p>z1<p><code>&lt;Bokeh Notebook handle&gt;</code></p>)r5   rH   s    rM   _repr_html_zCommsHandle._repr_html_   s#    <#oooooFF    c                    | j         S N)rF   rQ   s    rM   r7   zCommsHandle.comms   s
    {rS   c                    | j         S rU   )r6   rQ   s    rM   doczCommsHandle.doc   s
    yrS   eventr   c                x    |j         j        | j        j        v r!| j        j                            |           d S d S rU   modelidrW   models	callbackstrigger_on_changerH   rX   s     rM   _document_model_changedz#CommsHandle._document_model_changed   <    ;>TX_,,H0077777 -,rS   r   c                x    |j         j        | j        j        v r!| j        j                            |           d S d S rU   rZ   r`   s     rM   _column_data_changedz CommsHandle._column_data_changed   rb   rS   r   c                x    |j         j        | j        j        v r!| j        j                            |           d S d S rU   rZ   r`   s     rM   _columns_streamedzCommsHandle._columns_streamed   rb   rS   r   c                x    |j         j        | j        j        v r!| j        j                            |           d S d S rU   rZ   r`   s     rM   _columns_patchedzCommsHandle._columns_patched   rb   rS   N)r7   r   r8   r   r9   r:   )r9   rO   )r9   r   )r9   r   )rX   r   r9   r:   )rX   r   r9   r:   )rX   r   r9   r:   )rX   r   r9   r:   )__name__
__module____qualname____doc__r3   __annotations__rN   rR   propertyr7   rW   ra   rd   rf   rh    rS   rM   r$   r$   d   s           EOOOONNN   *G G G G    X    X8 8 8 88 8 8 88 8 8 88 8 8 8 8 8rS   r$   c                      e Zd Zdd
ZdS )Load	resourcesr!   verboseboolhide_bannerload_timeoutintr9   r:   c                    d S rU   ro   )rH   rr   rs   ru   rv   s        rM   __call__zLoad.__call__         rS   N)
rr   r!   rs   rt   ru   rt   rv   rw   r9   r:   ri   rj   rk   ry   ro   rS   rM   rq   rq      s        nnnnnnrS   rq   c                      e Zd Zd
dZd	S )ShowDocobjr   stater"   notebook_handler$   r9   c                    d S rU   ro   )rH   r~   r   r   s       rM   ry   zShowDoc.__call__   rz   rS   Nr~   r   r   r"   r   r$   r9   r$   r{   ro   rS   rM   r}   r}      s        bbbbbbrS   r}   c                      e Zd ZddZdS )ShowAppappr   r   r"   notebook_urlstr | ProxyUrlFunckwr   r9   r:   c                    d S rU   ro   )rH   r   r   r   r   s        rM   ry   zShowApp.__call__   rz   rS   N)
r   r   r   r"   r   r   r   r   r9   r:   r{   ro   rS   rM   r   r      s        pppppprS   r   c                  .    e Zd ZU ded<   ded<   ded<   dS )Hooksrq   loadr}   rW   r   r   N)ri   rj   rk   rm   ro   rS   rM   r   r      s+         JJJLLLLLLLLrS   r   Fnotebook_typeNotebookTyper   r.   r-   	overwritert   r9   r:   c                v    | t           v r|st          d| d          t          |||          t           | <   dS )ax   Install a new notebook display hook.

    Bokeh comes with support for Jupyter notebooks built-in. However, there are
    other kinds of notebooks in use by different communities. This function
    provides a mechanism for other projects to instruct Bokeh how to display
    content in other notebooks.

    This function is primarily of use to developers wishing to integrate Bokeh
    with new notebook types.

    Args:
        notebook_type (str) :
            A name for the notebook type, e.e. ``'Jupyter'`` or ``'Zeppelin'``

            If the name has previously been installed, a ``RuntimeError`` will
            be raised, unless ``overwrite=True``

        load (callable) :
            A function for loading BokehJS in a notebook type. The function
            will be called with the following arguments:

            .. code-block:: python

                load(
                    resources,   # A Resources object for how to load BokehJS
                    verbose,     # Whether to display verbose loading banner
                    hide_banner, # Whether to hide the output banner entirely
                    load_timeout # Time after which to report a load fail error
                )

        show_doc (callable) :
            A function for displaying Bokeh standalone documents in the
            notebook type. This function will be called with the following
            arguments:

            .. code-block:: python

                show_doc(
                    obj,            # the Bokeh object to display
                    state,          # current bokeh.io "state"
                    notebook_handle # whether a notebook handle was requested
                )

            If the notebook platform is capable of supporting in-place updates
            to plots then this function may return an opaque notebook handle
            that can  be used for that purpose. The handle will be returned by
            ``show()``, and can be used by as appropriate to update plots, etc.
            by additional functions in the library that installed the hooks.

        show_app (callable) :
            A function for displaying Bokeh applications in the notebook
            type. This function will be called with the following arguments:

            .. code-block:: python

                show_app(
                    app,          # the Bokeh Application to display
                    state,        # current bokeh.io "state"
                    notebook_url, # URL to the current active notebook page
                    **kw          # any backend-specific keywords passed as-is
                )

        overwrite (bool, optional) :
            Whether to allow an existing hook to be overwritten by a new
            definition (default: False)

    Returns:
        None

    Raises:
        RuntimeError
            If ``notebook_type`` is already installed and ``overwrite=False``

    zhook for notebook type z already exists)r   rW   r   N)_HOOKSRuntimeErrorr   )r   r   r.   r-   r   s        rM   r'   r'      sN    X yU]UUUVVV!txHHHF=rS   )documentr   handler   Document | Noner   State | Noner   CommsHandle | Nonec                ,   ddl m} |t                      }| s|j        } | st	          d           dS ||j        }|st	          d           dS t          |j        j        j	                  }t          |          dk    rdS g |j        j        _	         |                                dt          t          d         |                    }|j                            |j                   |j                            |j                   |j                            |j                   |j        D ]e}t'          j        |j                  }|                                }|j                            |           |j                            |g	           fdS )
a6   Update Bokeh plots in a Jupyter notebook output cells with new data
    or property values.

    When working the the notebook, the ``show`` function can be passed the
    argument ``notebook_handle=True``, which will cause it to return a
    handle object that can be used to update the Bokeh output later. When
    ``push_notebook`` is called, any property updates (e.g. plot titles or
    data source values, etc.) since the last call to ``push_notebook`` or
    the original ``show`` call are applied to the Bokeh output in the
    previously rendered Jupyter output cell.

    Several example notebooks can be found in the GitHub repository in
    the :bokeh-tree:`examples/output/jupyter/push_notebook` directory.

    Args:

        document (Document, optional):
            A |Document| to push from. If None uses ``curdoc()``. (default:
            None)

        state (State, optional) :
            A :class:`State` object. If None, then the current default
            state (set by |output_file|, etc.) is used. (default: None)

    Returns:
        None

    Examples:

        Typical usage is typically similar to this:

        .. code-block:: python

            from bokeh.plotting import figure
            from bokeh.io import output_notebook, push_notebook, show

            output_notebook()

            plot = figure()
            plot.circle([1,2,3], [4,6,5])

            handle = show(plot, notebook_handle=True)

            # Update the plot title in the earlier cell
            plot.title.text = "New Title"
            push_notebook(handle=handle)

    r   )r   NzNo document to pushzzCannot find a last shown plot to update. Call output_notebook() and show(..., notebook_handle=True) before push_notebook()r   z	PATCH-DOCr   )buffers)protocolr   r   r   r   last_comms_handlelistrW   r^   _held_eventslencreater   r7   sendheader_jsonmetadata_jsoncontent_jsonr   jsondumpsrefto_bytes)	r   r   r   BokehProtocoleventsmsgbufferheaderpayloads	            rM   r+   r+     s   d 544444}

 "> "###~(   J  	K  	K  	K&*&344F
 6{{a(*FJ%
-//
 
 d48N3OQW.X.X
Y
YC
Lco&&&
Lc'(((
Lc&'''+ - -FJ''//##&!!!7),,,,	- -rS   actionLiteral['load', 'doc', 'app']argsr   kwargsc                    | t           vrt          d|           t           |          |         t          d| d|d          t          |          |         |i |S )a   Run an installed notebook hook with supplied arguments.

    Args:
        noteboook_type (str) :
            Name of an existing installed notebook hook

        actions (str) :
            Name of the hook action to execute, ``'doc'`` or ``'app'``

    All other arguments and keyword arguments are passed to the hook action
    exactly as supplied.

    Returns:
        Result of the hook action, as-is

    Raises:
        RuntimeError
            If the hook or specific action is not installed

    z,no display hook installed for notebook type Nznotebook hook for z did not install z action)r   r   )r   r   r   r   s       rM   r,   r,   [  sv    * F""[-[[\\\mV$,cccRXcccddd- ($9&999rS   	server_idr   c                   t                      j                            | d          }|t                              d|            dS 	 |                                D ]}|                                 |                                 t                      j        | = dS # t          $ r+}t                              d| d|            Y d}~dS d}~ww xY w)z Given a UUID id of a div removed or replaced in the Jupyter
    notebook, destroy the corresponding server sessions and stop it.

    Nz#No server instance found for uuid: z Could not destroy server for id z: )	r   uuid_to_servergetrD   rE   get_sessionsdestroystoprC   )r   serversessionrL   s       rM   r%   r%   z  s    
 ZZ&**9d;;F~		E	EEFFFI**,, 	 	GOOJJ%i000 I I I		GYGGAGGHHHHHHHHHIs   
AB 
C) CCtarget_namerO   r   c                (    ddl m}  || i           S )a    Create a Jupyter comms object for a specific target, that can
    be used to update Bokeh documents in the Jupyter notebook.

    Args:
        target_name (str) : the target name the Comms object should connect to

    Returns
        Jupyter Comms

    r   r   )r   data)ipykernel.commr   )r   r   s     rM   r&   r&     s+     $#####4Kb1111rS   c                 H    t          dt          t          t                     dS )r2   r/   N)r'   r)   r.   r-   ro   rS   rM   r(   r(     s     )]HhGGGGGrS     rr   Resources | Noners   ru   rv   rw   c                   ddl m} ddlm} ddlm} ddlm} ddlm} ddl	m
}	 |  ||                                	          } |s| j        d
k    rd
}
d
}nXt          | j                  dk    r| j        d         n| j        }
t          | j                  dk    r| j        d         n| j        }d | j        D             }t           r|r|                    d            |	            }|                    |||
|||          }nd}d}| a |d|           }t'          |||d          }t'          |||d          }|t)          d|i           t)          t*          |t,          |i           dS )a   Prepare the IPython notebook for displaying Bokeh plots.

    Args:
        resources (Resource, optional) :
            how and where to load BokehJS from (default: CDN)

        verbose (bool, optional) :
            whether to report detailed settings (default: False)

        hide_banner (bool, optional):
            whether to hide the Bokeh banner (default: False)

        load_timeout (int, optional) :
            Timeout in milliseconds when plots assume load timed out (default: 5000)

    .. warning::
        Clearing the output cell containing the published BokehJS
        resources HTML code may cause Bokeh CSS styling to be removed.

    Returns:
        None

    r   )__version__)NOTEBOOK_LOAD)bundle_for_objs_and_resourcesr    )settings) make_globally_unique_css_safe_idN)modeinliner   r   c                8    g | ]}|j         d k    d|j        z   S )r   z	Warning: )typetext).0r   s     rM   
<listcomp>z!load_notebook.<locals>.<listcomp>  s,    ___sCHX^L^L^K#(*L^L^L^rS   z"Warning: BokehJS previously loaded)
element_idrs   js_infocss_infobokeh_versionwarningsT)register_mimeFr#   ) r   core.templatesr   embed.bundler   rr   r!   r   util.serializationr   r   r   js_files	css_filesmessages_NOTEBOOK_LOADEDappendrender_loading_jsr*   JS_MIME_TYPELOAD_MIME_TYPE)rr   rs   ru   rv   r   r   r   r!   r   r   r   r   r   r   htmlbundlenb_jsjl_jss                     rM   r)   r)     s   6 ......<<<<<<%%%%%%######EEEEEEI8#5#5#7#7888	
  >X%%'/G(0HH/293E/F/F!/K/Ki(++QZQcG14Y5H1I1IQ1N1Ny*1--T]TgH__i6H___ 	B 	BOO@AAA5577
##&##$'$ $ 
 
 
 **4;;F
LMMME
LNNNEk40111     rS   )	transientr   dict[str, Any]metadatadict[Any, Any] | Noner   dict[str, Any] | Nonec               *    ddl m}  || |fd|i| dS )r2   r   )r*   r   N)IPython.displayr*   )r   r   r   r   r*   s        rM   r*   r*     s<    
 544444xGG9GGGGGGrS   r   ProxyUrlFuncr   r   r"   r   r   portr   c                   t          j                     ddlm} ddlm} |                                }t          |          }t          |          r |d          }nt          |          } |d| if|||gd|}	t          t                      j                  }
|	t                      j        |
<   |	                                 t          |          r ||	j                  }nt#          ||	j                  }t          j        d|            t          j        d	|            dd
lm}  ||d          }t+          t,          |t.          dit.          d|
ii           dS )al   Embed a Bokeh server application in a Jupyter Notebook output cell.

    Args:
        app (Application or callable) :
            A Bokeh Application to embed inline in a Jupyter notebook.

        state (State) :
            ** Unused **

        notebook_url (str or callable) :
            The URL of the notebook server that is running the embedded app.

            If ``notebook_url`` is a string, the value string is parsed to
            construct the origin and full server URLs.

            If notebook_url is a callable, it must accept one parameter,
            which will be the server port, or None. If passed a port,
            the callable must generate the server URL, otherwise if passed
            None, it must generate the origin URL for the server.

            If the environment variable JUPYTER_BOKEH_EXTERNAL_URL is set
            to the external URL of a JupyterHub, notebook_url is overridden
            with a callable which enables Bokeh to traverse the JupyterHub
            proxy without specifying this parameter.

        port (int) :
            A port for the embedded server will listen on.

            By default the port is 0, which results in the server listening
            on a random dynamic port.

    Any additional keyword arguments are passed to :class:`~bokeh.server.Server` (added in version 1.1)

    Returns:
        None

    r   )IOLoopr   )ServerN/)io_loopr   allow_websocket_originzServer URL is zOrigin URL is )server_document)rr   r   r   r   )loggingbasicConfigtornado.ioloopr   server.serverr   current_update_notebook_url_from_envcallable_origin_urlr   r   hexr   r   startr   _server_urlrE   embedr   r*   HTML_MIME_TYPEEXEC_MIME_TYPE)r   r   r   r   r   r   r   looporiginr   r   urlr   scripts                 rM   r-   r-     s   X %%%%%%&&&&&&>>D0>>L +d##\**VS#J_4QWPX__\^__F577;I+1HJJi(
LLNNN 5l6;'',44M(3(()))M+6++,,,''''''_SD111F 	i0     rS   r~   r   c                    d S rU   ro   )r~   r   s     rM   r.   r.   R  s    03rS   r   c                    d S rU   ro   )r~   r   r   s      rM   r.   r.   T  s    UXUXrS   c                   | |j         j        vr|j                             |            ddlm} |rt                      nd} || |          \  }}}t          t          |i           t          t          |t          dit          d| j
        ii           |rEt          t          |          |          }|j         j                            |           ||_        |S dS )r2   r   )notebook_contentNr   r\   r   )r   rootsadd_rootembed.notebookr  r   r*   r  r   r  r\   r$   r&   r^   on_change_dispatch_tor   )	r~   r   r   r  comms_targetr	  divr8   r   s	            rM   r.   r.   W  s     %.&&&$$$111111 /97999TL..sLAAVS(.#.///,C~`dfifl_mNnoooo
  Y|44h?? 66v>>>"(4rS   zdict[str, Hooks]r   r   Tr   r   r   	ID | Noner   c                B    ddl m} |                    | |d||          S )r2   r   )AUTOLOAD_NB_JST)r   	elementidforcetimeoutr   )r   r  r   )r   r   rv   r   r  s        rM   r   r   x  sB     0/////   % !   rS   r  c                f    |                      d          r|                     d          d         } | S )r2   httpz//r   )
startswithsplit)r  s    rM   r   r     s1     ~~f !iiooa JrS   r4   c                    |d| nd}|                      d          r"|                     dd          d          | d S d|                     d          d          | d S )	r2   N:r   r  r   r   r   zhttp://)r  rsplitr  )r  r   port_s      rM   r  r    s|     *JJJJE
~~f 9**S!$$Q'5555583*8E83888rS   c                (   t           j        d         }t          j                            |          j        }| |S t           j        d         }d|  }t          j                            ||          }t          j                            ||          }|S )a   Callable to configure Bokeh's show method when a proxy must be
    configured. If port is None we're asking about the URL
    for the origin header.

    Taken from documentation here:

       https://docs.bokeh.org/en/latest/docs/user_guide/output/jupyter.html#jupyterhub

    and made an implicit override when JUPYTER_BOKEH_EXTERNAL_URL is defined in
    a user's environment to the external hostname of the hub, e.g. https://our-hub.edu

    Args:
       port (int):
           random port generated by bokeh to avoid re-using recently closed ports

       Returns:
           str:
               URL capable of traversing the JupyterHub proxy to return to this notebook session.
    JUPYTER_BOKEH_EXTERNAL_URLNJUPYTERHUB_SERVICE_PREFIXzproxy/)osenvironurllibparseurlparsenetlocurljoin)r   base_urlhostservice_url_pathproxy_url_pathuser_urlfull_urls          rM   _remote_jupyter_proxy_urlr2    s    ( z67H<  **1D |z"=>$d__N|##H.>??H|##Hn==HOrS   c                    t           j                            d          r,| t          k    rt                              d           t          S | S )a  If the environment variable ``JUPYTER_BOKEH_EXTERNAL_URL`` is defined, returns a function which
    generates URLs which can traverse the JupyterHub proxy.  Otherwise returns ``notebook_url`` unmodified.

    A warning is issued if ``notebook_url`` is not the default and
    ``JUPYTER_BOKEH_EXTERNAL_URL`` is also defined since setting the
    environment variable makes specifying ``notebook_url`` irrelevant.

    Args:
       notebook_url (str | ProxyUrlFunc):
          Either a URL string which defaults or a function that given a port
          number will generate a URL suitable for traversing the JupyterHub proxy.

    Returns:
       str | ProxyUrlFunc
          Either a URL string or a function that generates a URL string given a port number.  The
          latter function may be user supplied as the input parameter or defined internally by Bokeh
          when ``JUPYER_BOKEH_EXTERNAL_URL`` is set.

    r#  z[Environment var 'JUPYTER_BOKEH_EXTERNAL_URL' is defined. Ignoring 'notebook_url' parameter.)r%  r&  r   DEFAULT_JUPYTER_URLrD   warningr2  )r   s    rM   r   r     sF    ( 
z~~233 ...KKuvvv((rS   )F)r   r   r   rq   r.   r}   r-   r   r   rt   r9   r:   )r   r   r   r   r   r   r9   r:   )
r   r   r   r   r   r   r   r   r9   r   )r   r   r9   r:   )r   rO   r9   r   )r9   r:   )NFFr   )
rr   r   rs   rt   ru   rt   rv   rw   r9   r:   rU   )
r   r   r   r   r   r   r   r   r9   r:   )r   r   r   r"   r   r   r   rw   r   r   r9   r:   )r~   r   r   r"   r9   r:   r   )r~   r   r   r"   r   r   r9   r   )r   T)
r   r   r   r  rv   rw   r   rt   r9   rO   )r  rO   r9   rO   )r  rO   r   r4   r9   rO   )r   r4   r9   rO   )r   r   r9   r   )Trl   
__future__r   r   	getLoggerri   rD   r   r%  r'  typingr   r   r   r   r   r	   r
   r   r   uuidr   r   r   typing_extensionsr   
core.typesr   r   r   util.warningsr   r   r   application.applicationr   document.documentr   document.eventsr   r   r   r   r   r   r   r[   r   rr   r!   r"   r  r   r   r  r4  __all__r   r$   rq   r}   r   r   r'   r+   r,   r%   r&   r(   r)   r*   rw   rO   r   rm   r-   r.   r   r   r   r   r  r2  r   ro   rS   rM   <module>rA     si     # " " " " " g!!  				 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
        ,######++++++       ( ( ( ( ( (                   555555,,,,,,              &%%%%%%%%%%% )77& $ ,->8 >8 >8 >8 >8 >8 >8 >8@o o o o o8 o o oc c c c ch c c cq q q q qh q q q    I    .3NI NI NI NI NI` 26T%)W- W- W- W- W- W-r: : : :>I I I I&2 2 2 2H H H H GL7;O O O O ObH  ~B H H H H H H #E#t)$4#5s#:; ; ; ; ;
 ,?	R R R R Rh 
 3 3 3 
 3	 X X X 
 X    :     %)  ) ) ) )       	9 	9 	9 	9! ! ! !H     rS   