
    oi                        d Z 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
 ddlmZmZ ddlmZmZ ddlmZmZ  G d	 d
      Z G d de
      Zy)z7Replacements for sys.displayhook that publish over ZMQ.    )annotationsN)
ContextVar)DisplayHook)Sessionextract_header)AnyInstance)encode_images
json_cleanc                  <    e Zd ZdZdZd Zd Zd Zed        Z	d Z
y)	ZMQDisplayHookzOA simple displayhook that publishes the object's repr over a ZeroMQ
    socket.s   execute_resultc                    || _         || _        t        d      | _        | j                  j	                  i        i | _        y)zInitialize the hook.parent_headerN)session
pub_socketr   _parent_headerset_parent_header_global)selfr   r   s      ]/home/ubuntu/docker-apps/notebooks/venv/lib/python3.12/site-packages/ipykernel/displayhook.py__init__zZMQDisplayHook.__init__   s9    $:D_:U#%'"    c                     y)z$This method is replaced in kernelappr    r   s    r   get_execution_countz"ZMQDisplayHook.get_execution_count"   s    r   c                R   |y|t         _        t        j                  j	                          t        j
                  j	                          | j                         dt        |      ii d}| j                  j                  | j                  d|| j                  | j                         y)zHandle a hook call.Nz
text/plain)execution_countdatametadataexecute_result)parentident)builtins_sysstdoutflushstderrr   reprr   sendr   r   topic)r   objcontentss      r   __call__zZMQDisplayHook.__call__&   s    ;




#779!49-

 	OO%%** 	 	
r   c                n    	 | j                   j                         S # t        $ r | j                  cY S w xY wNr   getLookupErrorr   r   s    r   r   zZMQDisplayHook.parent_header;   7    	.&&**,, 	.---	.    44c                ^    t        |      }| j                  j                  |       || _        yzSet the parent header.Nr   r   r   r   r   r"   r   s      r   
set_parentzZMQDisplayHook.set_parentB   (    &v..%2"r   N)__name__
__module____qualname____doc__r,   r   r   r/   propertyr   r;   r   r   r   r   r      s6     E(
* . .3r   r   c                       e Zd ZU dZdZ eed      Z ed      Z	de
d<   de
d<    fd	Zed
        Zd Zd Zd ZddZd Z xZS )ZMQShellDisplayHookzA displayhook subclass that publishes data using ZeroMQ. This is intended
    to work with an InteractiveShell instance. It sends a dict of different
    representations of the object.NT)
allow_nonezContextVar[dict[str, Any]]r   zdict[str, t.Any] | Nonemsgc                z    t        |   |i | t        d      | _        | j                  j	                  i        y )Nr   )superr   r   r   r   )r   argskwargs	__class__s      r   r   zZMQShellDisplayHook.__init__U   s6    $)&)(9#r   c                n    	 | j                   j                         S # t        $ r | j                  cY S w xY wr1   r2   r   s    r   r   z!ZMQShellDisplayHook.parent_headerZ   r5   r6   c                ^    t        |      }| j                  j                  |       || _        yr8   r9   r:   s      r   r;   zZMQShellDisplayHook.set_parenta   r<   r   c                ~    | j                   r1| j                   j                  di i d| j                        | _        yy)zStart the display hook.r!   )r   r    )r"   N)r   rE   r   r   s    r   start_displayhookz%ZMQShellDisplayHook.start_displayhookg   sB    <<||''  " )) ( DH r   c                V    | j                   r| j                  | j                   d   d<   yy)zWrite the output prompt.contentr   N)rE   prompt_countr   s    r   write_output_promptz'ZMQShellDisplayHook.write_output_prompts   s(    88595F5FDHHY 12 r   c                    | j                   r7t        t        |            | j                   d   d<   || j                   d   d<   yy)z!Write format data to the message.rP   r   r    N)rE   r   r
   )r   format_dictmd_dicts      r   write_format_dataz%ZMQShellDisplayHook.write_format_datax   s>    88*4];5O*PDHHY'.5DHHY
+ r   c                V   t         j                  j                          t         j                  j                          | j                  rZ| j                  d   d   rH| j
                  r<| j
                  j                  | j                  | j                  | j                         d| _        y)z%Finish up all displayhook activities.rP   r   )r#   N)	r&   r'   r(   r)   rE   r   r+   r   r,   r   s    r   finish_displayhookz&ZMQShellDisplayHook.finish_displayhook~   sk    



88+F3LLdootxxtzzJr   r1   )r=   r>   r?   r@   r,   r	   r   r   r   r   __annotations__r   rA   r   r;   rN   rR   rV   rX   __classcell__)rJ   s   @r   rC   rC   I   sd    & Ew40G%J..	  $
 . .3
G
6r   rC   )r@   
__future__r   r$   r&   typingtcontextvarsr   IPython.core.displayhookr   jupyter_client.sessionr   r   	traitletsr   r	   ipykernel.jsonutilr
   r   r   rC   r   r   r   <module>rc      s<    = #  
  " 0 : # 833 33l;+ ;r   