
    XhL                        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
mZ ddlmZ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)DisplayHook)Sessionextract_header)AnyDictInstance)encode_images
json_cleanc                  ,    e Zd ZdZdZd Zd Zd Zd Zy)ZMQDisplayHookzOA simple displayhook that publishes the object's repr over a ZeroMQ
    socket.s   execute_resultc                .    || _         || _        i | _        y)zInitialize the hook.N)session
pub_socketparent_header)selfr   r   s      I/var/www/html/myenv/lib/python3.12/site-packages/ipykernel/displayhook.py__init__zZMQDisplayHook.__init__   s    $    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-x@R@RZ^ZdZd 	 	
r   c                $    t        |      | _        y)zSet the parent header.Nr   r   r   r   s     r   
set_parentzZMQDisplayHook.set_parent3       +F3r   N)	__name__
__module____qualname____doc__r)   r   r   r,   r0   r   r   r   r   r      s"     E 
"4r   r   c                  v    e Zd ZU dZdZ eed      Z ed      Z	 e
i       Zded<   d Zd Zd	 Zdd
Zd Zy)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dict[str, t.Any] | Nonemsgc                $    t        |      | _        y)z%Set the parent for outbound messages.Nr.   r/   s     r   r0   zZMQShellDisplayHook.set_parentD   r1   r   c                ~    | j                   r1| j                   j                  di i d| j                        | _        yy)zStart the display hook.r   )r   r   )r   N)r   r9   r   r   s    r   start_displayhookz%ZMQShellDisplayHook.start_displayhookH   sB    <<||''  " )) ( DH r   c                V    | j                   r| j                  | j                   d   d<   yy)zWrite the output prompt.contentr   N)r9   prompt_countr   s    r   write_output_promptz'ZMQShellDisplayHook.write_output_promptT   s(    88595F5FDHHY 12 r   c                    | j                   r7t        t        |            | j                   d   d<   || j                   d   d<   yy)z!Write format data to the message.r>   r   r   N)r9   r   r
   )r   format_dictmd_dicts      r   write_format_dataz%ZMQShellDisplayHook.write_format_dataY   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.r>   r   )r    N)	r#   r$   r%   r&   r9   r   r(   r   r)   r   s    r   finish_displayhookz&ZMQShellDisplayHook.finish_displayhook_   sk    



88+F3LLdootxxtzzJr   )N)r2   r3   r4   r5   r)   r	   r   r   r   r   r   r   __annotations__r0   r<   r@   rD   rF   r   r   r   r7   r7   8   sM    & Ew40G%JHM	  4
G
6r   r7   )r5   
__future__r   r!   r#   typingtIPython.core.displayhookr   jupyter_client.sessionr   r   	traitletsr   r   r	   ipykernel.jsonutilr
   r   r   r7   r   r   r   <module>rO      s<    = #  
  0 : ) ) 8#4 #4L-+ -r   