
    ek                       d dl mZ d dlZd dlZd dlZd dlmZ d dl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 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# 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 G d d                      Z0 G d d          Z1 G d de           Z2dS )    )annotationsN)	dataclass)AnyDictListUnion)Document)extension_dirs)Protocol)Receiver)ServerConnection)BokehSessionContext)ProtocolHandler)ServerSession)StaticHandler)	WSHandler)get_session_idget_token_payload)Comm   )edit_readonly    build_single_handler_application)	Resources)server_html_page_for_session)
set_curdocstatec                  .    e Zd ZU ded<   ded<   ded<   dS )_RequestProxyzDict[str, List[bytes]]	argumentszDict[str, str]cookieszDict[str, str | List[str]]headersN)__name__
__module____qualname____annotations__     9lib/python3.11/site-packages/panel/io/jupyter_executor.pyr    r    "   s8          &%%%''''''r)   r    c                      e Zd Zd ZddZdS )
Mimebundlec                    || _         d S N_mimebundle)self
mimebundles     r*   __init__zMimebundle.__init__+   s    %r)   Nc                    | j         i fS r.   r/   )r1   includeexcludes      r*   _repr_mimebundle_zMimebundle._repr_mimebundle_.   s    ##r)   )NN)r$   r%   r&   r3   r7   r(   r)   r*   r,   r,   )   s7        & & &$ $ $ $ $ $r)   r,   c                  b    e Zd ZdZd ZddZd Zdd
ZddZddZ	ddZ
ddZ	 d d!dZd"dZdS )#PanelExecutora  
    The PanelExecutor is intended to be run inside a kernel where it
    runs a Panel application renders the HTML and then establishes a
    Jupyter Comm channel to communicate with the PanelWSProxy in order
    to send and receive messages to and from the frontend.
    c                D   || _         || _        || _        |                     |          | _        t          |          | _        t          | j                  | _        | j        	                    | j
                   t                      | _        t          | j                  | _        t                      | _        t"          j                                        | _        d | _        t-          t.          j                            dd          | j        t4          j        d          | _        |                                  	 |                                 | _        tA          | j        | d | j                  | _!        d S # tD          $ r}|| _#        d | _        Y d }~d S d }~ww xY w)N)target_nameBOKEH_RESOURCESserverT)moderoot_urlpath_versionerabsolute)$pathtokenr?   _get_payloadpayloadr   
session_idr   common_msg_receive_msgr   protocolr   receiverr   handlertornadolocksLock
write_lock_contextr   osenvirongetr   append_version	resources
_set_state_create_server_sessionsessionr   
connection	Exception	exception)r1   rB   rC   r?   es        r*   r3   zPanelExecutor.__init__:   sc   	
 ((//(//T_555		*+++ 

 //&((!-,,.." 18<<t}(7$
 
 
 		 6688DL.t}dD$,WWDOOO 	  	  	 DNDLLLLLLL	 s    :E< <
FFFrC   strreturnDict[str, Any]c                    t          |          }d|v rNd|v rJd|d         vr@d                    d |d                                         D                       |d         d<   |S )Nr"   r#   Cookiez; c                "    g | ]\  }}| d | S )=r(   ).0kvs      r*   
<listcomp>z.PanelExecutor._get_payload.<locals>.<listcomp>Y   s3     6 6 6#q!1

q

6 6 6r)   )r   joinitems)r1   rC   rE   s      r*   rD   zPanelExecutor._get_payloadT   s    #E**  Y'%9%9	 222+/99 6 6'.y'9'?'?'A'A6 6 6 , ,GIx( r)   c                    dt           _        t          t                     5  | j        dz   t           _        | j        t           _        d d d            d S # 1 swxY w Y   d S )NT/)r   _jupyter_kernel_contextr   r?   base_urlrel_path)r1   s    r*   rW   zPanelExecutor._set_state^   s    (,%5!! 	+ 	+!]S0EN!]EN	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   &AAANonec                T    t          j        |                     |                     d S r.   )asyncioensure_future_receive_msg_asyncr1   msgs     r*   rI   zPanelExecutor._receive_msgd   s'    d55c::;;;;;r)   c                  K   	 |                      |d         d                    d {V }n1# t          $ r$}|                     d|            d }Y d }~nd }~ww xY w	 |r:|                     |           d {V }|r|                     |           d {V  d S d S d S # t          $ r#}|                     d|            Y d }~d S d }~ww xY w)Ncontentdataz"server failed to parse a message: z#server failed to handle a message: )_receiver[   _internal_error_handle	_schedule)r1   rv   messager]   works        r*   rt   z PanelExecutor._receive_msg_asyncg   sO     	 MM#i.*@AAAAAAAAGG 	 	 	   !Ia!I!IJJJGGGGGG			L /!\\'22222222 /............./ // / 	L 	L 	L  !Jq!J!JKKKKKKKKK	Ls,   ', 
AAA:B 
C(CCrv   c                @    | j                             |ddi           d S )Nstatusinternal_errorrG   sendru   s     r*   r{   zPanelExecutor._internal_errorx   $    	sX'7899999r)   c                @    | j                             |ddi           d S )Nr   protocol_errorr   ru   s     r*   _protocol_errorzPanelExecutor._protocol_error{   r   r)   r   c                   t                      }t          | j        d |          x| _        }t	          d | j                            di                                           D             | j                            d          | j                            d                    |_        | j	        |_
        t          j        |          |_        | j                            d          s| j                            d          rdd	lm}  || j        d 
          }nt#          | j                  }t%          |          5  |                    |           d d d            n# 1 swxY w Y   t(          j        j                                        }t1          | j        ||| j	                  }|                    |           |S )Nc                .    i | ]\  }}|d  |D             S )c                8    g | ]}|                     d           S )zutf-8)encode)re   rg   s     r*   rh   zCPanelExecutor._create_server_session.<locals>.<dictcomp>.<listcomp>   s$    999188G,,999r)   r(   )re   rf   vss      r*   
<dictcomp>z8PanelExecutor._create_server_session.<locals>.<dictcomp>   s-    qqqeaq99b999qqqr)   r!   r"   r#   )r!   r"   r#   z.yamlz.ymlr   r   )argv)io_looprC   )r	   r   rF   rQ   r    rE   rT   rj   _requestrC   _tokenweakrefref_session_contextrB   endswithlumen.commandr   r   initialize_documentrM   ioloopIOLoopcurrentr   _set_session)r1   docsession_contextbuild_lumen_appapplooprY   s          r*   rX   z$PanelExecutor._create_server_session~   s   jj*=OT3+
 +
 	

 $1qqt|GWGWXcegGhGhGnGnGpGpqqqL$$Y//L$$Y//$
 $
 $
 
 "&  '{?;;9g&& 	>$)*<*<V*D*D 	>      "/$)$777CC249==C__ 	) 	)##C(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) ~$,,..d$*UUU$$W---s   =EE#&E#FTr~   !Union[bytes, str, Dict[str, Any]]binaryboollockedc                   K   d|i}|r | j                             i ||g           d S | j                             ||           d S )Nr   )metadatabuffers)r   r   )r1   r~   r   r   r   s        r*   write_messagezPanelExecutor.write_message   sZ       f% 	7INN27)NDDDDDINN7XN66666r)   r,   c                   | j         t          dd| j         i          S t          | j         j                  5  | j         j        j        st          ddi          cddd           S t          | j         | j        | j         j        j        | j         j        j	        | j         j        j
                  }ddd           n# 1 swxY w Y   t          |d t          j                    D             d          S )z~
        Renders the application to an IPython.display.HTML object to
        be served by the `PanelJupyterHandler`.
        Nz
text/errorz!Session did not start correctly: zThe Panel application being served did not serve any contents. Ensure you mark one or more Panel components in your notebook or script with .servable(), e.g. pn.Row('Hello World!').servable().)rV   titletemplatetemplate_variablesc                4    i | ]\  }}|t          |          S r(   )r^   )re   nameexts      r*   r   z(PanelExecutor.render.<locals>.<dictcomp>   s1     - - -#,4c#hh- - -r)   )z	text/htmlzapplication/bokeh-extensions)rY   r,   r\   r   documentrootsr   rV   r   r   r   r
   rj   )r1   htmls     r*   renderzPanelExecutor.render   sn   
 <|-aQUQ_-a-abccc-.. 	 	<(. ! [#  	 	 	 	 	 	 	 	 0.l+1.7#'<#8#K  D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  - -0>0D0F0F- - -
 
   	s   "C )AC  CCN)rC   r^   r_   r`   )r_   rp   )rv   r^   r_   rp   )r_   r   )FT)r~   r   r   r   r   r   r_   rp   )r_   r,   )r$   r%   r&   __doc__r3   rD   rW   rI   rt   r{   r   rX   r   r   r(   r)   r*   r9   r9   2   s              4   + + +< < < <L L L L": : : :: : : :       H .27 7 7 7 7     r)   r9   )3
__future__r   rr   rR   r   dataclassesr   typingr   r   r   r   rM   bokeh.documentr	   bokeh.embed.bundler
   bokeh.protocolr   bokeh.protocol.receiverr   bokeh.server.connectionr   bokeh.server.contextsr   bokeh.server.protocol_handlerr   bokeh.server.sessionr   !bokeh.server.views.static_handlerr   bokeh.server.views.wsr   bokeh.util.tokenr   r   ipykernel.commr   utilr   markdownr   rV   r   r=   r   r   r   r    r,   r9   r(   r)   r*   <module>r      sc   " " " " " "  				  ! ! ! ! ! !             # # # # # # - - - - - - # # # # # # , , , , , , 4 4 4 4 4 4 5 5 5 5 5 5 9 9 9 9 9 9 . . . . . . ; ; ; ; ; ; + + + + + + > > > > > > > >                   6 6 6 6 6 6             0 0 0 0 0 0 $ $ $ $ $ $ $ $ ( ( ( ( ( ( ( ($ $ $ $ $ $ $ $T T T T TI T T T T Tr)   