o
    i                     @  s   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
 d
ZG dd de
ZdS )z7Replacements for sys.displayhook that publish over ZMQ.    )annotationsN)
ContextVar)DisplayHook)Sessionextract_header)AnyInstance)encode_images
json_cleanc                   @  s@   e Zd ZdZdZdd Zdd Zdd Zed	d
 Z	dd Z
dS )ZMQDisplayHookzOA simple displayhook that publishes the object's repr over a ZeroMQ
    socket.s   execute_resultc                 C  s,   || _ || _td| _| ji  i | _dS )zInitialize the hook.parent_headerN)session
pub_socketr   _parent_headerset_parent_header_global)selfr   r    r   P/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/ipykernel/displayhook.py__init__   s
   

zZMQDisplayHook.__init__c                 C  s   dS )z$This method is replaced in kernelappr   r   r   r   r   r   get_execution_count"   s   z"ZMQDisplayHook.get_execution_countc                 C  s^   |du rdS |t _tj  tj  |  dt|ii d}| jj	| j
d|| j| jd dS )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contentsr   r   r   __call__&   s    



zZMQDisplayHook.__call__c                 C  &   z| j  W S  ty   | j Y S w Nr   getLookupErrorr   r   r   r   r   r   ;   
   
zZMQDisplayHook.parent_headerc                 C     t |}| j| || _dS zSet the parent header.Nr   r   r   r   r   r   r   r   r   r   
set_parentB      
zZMQDisplayHook.set_parentN)__name__
__module____qualname____doc__r&   r   r   r)   propertyr   r4   r   r   r   r   r      s    	
r   c                      s   e Zd ZU dZdZeeddZeddZ	de
d< de
d<  fd	d
Zedd Zdd Zdd Zdd ZdddZdd Z  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                   s,   t  j|i | td| _| ji  d S )Nr   )superr   r   r   r   )r   argskwargs	__class__r   r   r   U   s   
zZMQShellDisplayHook.__init__c                 C  r*   r+   r,   r   r   r   r   r   Z   r/   z!ZMQShellDisplayHook.parent_headerc                 C  r0   r1   r2   r3   r   r   r   r4   a   r5   zZMQShellDisplayHook.set_parentc                 C  s*   | j r| j jdi i d| jd| _dS dS )zStart the display hook.r   )r   r   )r   N)r   r=   r   r   r   r   r   start_displayhookg   s   z%ZMQShellDisplayHook.start_displayhookc                 C  s   | j r| j| j d d< dS dS )zWrite the output prompt.contentr   N)r=   prompt_countr   r   r   r   write_output_prompts   s   z'ZMQShellDisplayHook.write_output_promptc                 C  s2   | j rtt|| j d d< || j d d< dS dS )z!Write format data to the message.rD   r   r   N)r=   r
   r	   )r   format_dictmd_dictr   r   r   write_format_datax   s   z%ZMQShellDisplayHook.write_format_datac                 C  sP   t j  t j  | jr#| jd d r#| jr#| jj| j| j| jd d| _dS )z%Finish up all displayhook activities.rD   r   )r   N)	r    r!   r"   r#   r=   r   r%   r   r&   r   r   r   r   finish_displayhook~   s
   


z&ZMQShellDisplayHook.finish_displayhookr+   )r6   r7   r8   r9   r&   r   r   r   r   r   __annotations__r   r:   r   r4   rC   rF   rI   rJ   __classcell__r   r   rA   r   r;   I   s   
 


r;   )r9   
__future__r   r   r    typingtcontextvarsr   IPython.core.displayhookr   jupyter_client.sessionr   r   	traitletsr   r   ipykernel.jsonutilr	   r
   r   r;   r   r   r   r   <module>   s    6