o
    Gf@                     @   s  d Z ddlmZ ddl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 ddlmZ ddlmZ ddlmZmZmZ d	d
lmZmZmZ d	dlmZmZ dd ZddddddddZG dd deZG dd deZG dd deZG dd dZ ej!G dd deZ"dS )z,Interactive figures in the IPython notebook.    )	b64encodeN)Comm)display
JavascriptHTML)is_interactiveGcf)_Backend
CloseEventNavigationToolbar2   )FigureCanvasWebAggCoreFigureManagerWebAggNavigationToolbar2WebAgg)TimerTornadoTimerAsyncioc                  C   s8   dd t  D } t s| dtt j  d| S )z
    Return a string showing the figure and connection status for the backend.

    This is intended as a diagnostic tool, and not for general use.
    c                 S   s0   g | ]}d j |jj pd|j |jdqS )z{fig} - {socket}zFigure )Zfigsocket)formatcanvasfigureZ	get_labelnumweb_sockets).0manager r   Alib/python3.10/site-packages/matplotlib/backends/backend_nbagg.py
<listcomp>   s    
z#connection_info.<locals>.<listcomp>zFigures pending show: 
)r	   Zget_all_fig_managersr   appendlenfigsjoin)resultr   r   r   connection_info   s   
r$   z
fa fa-homezfa fa-arrow-leftzfa fa-arrow-rightzfa fa-square-ozfa fa-arrowszfa fa-floppy-o)homeZbackZforwardZzoom_to_rectmovedownloadNc                   @   s    e Zd Zdd ejd D ZdS )NavigationIPyc                 C   s,   g | ]\}}}}|t v r||t | |fqS r   )_FONT_AWESOME_CLASSES)r   textZtooltip_textZ
image_fileZname_of_methodr   r   r   r   8   s    
zNavigationIPy.<listcomp>))ZDownloadzDownload plotr'   r'   N)__name__
__module____qualname__r   Z	toolitemsr   r   r   r   r(   5   s    
r(   c                       s   e Zd Ze ZZ fddZedd Zdd Z	dd Z
d	d
 Zedd Zed fdd	Zdd Zdd Zdd Zdd Z  ZS )FigureManagerNbAggc                    s   d| _ t || d S )NF)_shownsuper__init__)selfr   r   	__class__r   r   r1   C   s   zFigureManagerNbAgg.__init__c                    sH   || |  |t  r      fdd} d|S )Nc                    s      t d S N)mpl_disconnectr	   destroy)Zeventr   Zcidr   r   r   r7   O   s   
z6FigureManagerNbAgg.create_with_canvas.<locals>.destroyclose_event)r   show	draw_idleZmpl_connect)clsZcanvas_classr   r   r7   r   r8   r   create_with_canvasG   s   
z%FigureManagerNbAgg.create_with_canvasc                 C   s   t tt  d S r5   )r   r   r.   get_javascriptr2   r   r   r   
display_jsV   s   zFigureManagerNbAgg.display_jsc                 C   sj   | j s|   |   n| j  d| _ t| dr | j| j t s3ddl	m
} |j| jd  d S d S )NT_cidgcfr   r   )r/   r@   _create_commr   r;   hasattrr6   rA   r   matplotlib._pylab_helpersr	   r!   popr   )r2   r	   r   r   r   r:   \   s   


zFigureManagerNbAgg.showc                 C   s   d| _ |   dS )zJ
        A special method to re-show the figure in the notebook.

        FN)r/   r:   r?   r   r   r   reshowm   s   zFigureManagerNbAgg.reshowc                 C   s
   t | jS r5   )boolr   r?   r   r   r   	connectedu   s   
zFigureManagerNbAgg.connectedNc                    sV   |d u r	t  }n|}t j|d |ttjd j	dd |d u r)|
 S d S )N)streamzweb_backend/js/nbagg_mpl.jszutf-8)encoding)ioStringIOr0   r>   writepathlibZPath__file__parentZ	read_textgetvalue)r<   rI   outputr3   r   r   r>   y   s   
z!FigureManagerNbAgg.get_javascriptc                 C   s   t | }| | |S r5   )
CommSocketZadd_web_socketr2   commr   r   r   rB      s   
zFigureManagerNbAgg._create_commc                 C   s.   |  d t| jD ]}|  q
|   d S )Nclose)Z_send_eventlistr   on_closeclearup_closedrT   r   r   r   r7      s   

zFigureManagerNbAgg.destroyc                 C   s8   dd | j D | _ t| j dkrtd| j  dS dS )zClear up any closed Comms.c                 S   s   h | ]}|  r|qS r   )is_openr   r   r   r   r   	<setcomp>   s    
z4FigureManagerNbAgg.clearup_closed.<locals>.<setcomp>r   r9   N)r   r    r   r   Z_processr?   r   r   r   rY      s   z!FigureManagerNbAgg.clearup_closedc                    s    fdd| j D | _ d S )Nc                    s   h | ]
}|j j kr|qS r   )rU   comm_idr[   r]   r   r   r\      s    z1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r2   r]   r   r^   r   remove_comm      zFigureManagerNbAgg.remove_commr5   )r+   r,   r-   r(   Z_toolbar2_classZ
ToolbarClsr1   classmethodr=   r@   r:   rF   propertyrH   r>   rB   r7   rY   r_   __classcell__r   r   r3   r   r.   @   s     

r.   c                   @   s   e Zd ZeZdS )FigureCanvasNbAggN)r+   r,   r-   r.   Zmanager_classr   r   r   r   rd      s    rd   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )rS   aL  
    Manages the Comm connection between IPython and the browser (client).

    Comms are 2 way, with the CommSocket being able to publish a message
    via the send_json method, and handle a message with on_message. On the
    JS side figure.send_message and figure.ws.onmessage do the sending and
    receiving respectively.

    c              
      s   d _  _tt _ttdj  ztddjid_W n t	y3 } zt
d|d }~ww jj j d_ fdd}j| d S )	Nz<div id=%r></div>
matplotlibid)datazTUnable to create an IPython notebook Comm instance. Are you in the IPython notebook?Fc                    s$   d_  | d d     d S )NTcontentr]   )
_ext_closer_   rY   )Zclose_messager   r2   r   r   	_on_close   s   z&CommSocket.__init__.<locals>._on_close)supports_binaryr   struuidZuuid4r   r   r   rU   AttributeErrorRuntimeErrorZon_msg
on_messageri   rX   )r2   r   errrk   r   rj   r   r1      s"   zCommSocket.__init__c                 C   s   | j p| jj S r5   )ri   rU   Z_closedr?   r   r   r   rZ      s   zCommSocket.is_openc                 C   s2   |   rz| j  W d S  ty   Y d S w d S r5   )rZ   rU   rV   KeyErrorr?   r   r   r   rX      s   zCommSocket.on_closec                 C   s   | j dt|i d S )Nrg   )rU   sendjsondumps)r2   rh   r   r   r   	send_json   r`   zCommSocket.send_jsonc                 C   sL   | j r| jjddi|gd d S t|d}d| }| jd|i d S )Nblobz	image/png)Zbuffersasciizdata:image/png;base64,rg   )rl   rU   rt   r   decode)r2   rx   rg   Zdata_urir   r   r   send_binary   s
   
zCommSocket.send_binaryc                 C   s^   t |d d }|d dkr|   | j  d S |d dkr'|d | _d S | j| d S )Nrh   rg   typeclosingrl   value)ru   loadsrX   r   rY   rl   Zhandle_json)r2   messager   r   r   rq      s   zCommSocket.on_messageN)
r+   r,   r-   __doc__r1   rZ   rX   rw   r{   rq   r   r   r   r   rS      s    	

rS   c                   @   s   e Zd ZeZeZdS )_BackendNbAggN)r+   r,   r-   rd   ZFigureCanvasr.   ZFigureManagerr   r   r   r   r      s    r   )#r   base64r   rK   ru   rN   rn   Zipykernel.commr   ZIPython.displayr   r   r   re   r   rD   r	   Zmatplotlib.backend_basesr
   r   r   Zbackend_webagg_corer   r   r   r   r   r$   r)   r(   r.   rd   rS   Zexportr   r   r   r   r   <module>   s8    _M