o
    :a!                     @   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m	Z	m
Z
 zddlmZ W n ey9   ddlmZ Y nw ddlmZ ddlmZ ddlmZmZ dd	lmZmZmZ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)display
JavascriptHTML)Comm)is_interactiveGcf)_Backend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   C/usr/lib/python3/dist-packages/matplotlib/backends/backend_nbagg.py
<listcomp>#   s    
z#connection_info.<locals>.<listcomp>zFigures pending show: 
)r	   get_all_fig_managersr   appendlenfigsjoin)resultr   r   r   connection_info   s   
r#   zfa fa-home icon-homez fa fa-arrow-left icon-arrow-leftz"fa fa-arrow-right icon-arrow-rightzfa fa-square-o icon-check-emptyzfa fa-arrows icon-movezfa fa-floppy-o icon-save)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   B   s    
zNavigationIPy.<listcomp>))ZDownloadzDownload plotr&   r&   N)__name__
__module____qualname__r   Z	toolitemsr   r   r   r   r'   ?   s    
r'   c                       sv   e Zd ZeZ f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   r0   M   s   zFigureManagerNbAgg.__init__c                 C   s   t tt  d S N)r   r   r-   get_javascriptr1   r   r   r   
display_jsQ   s   zFigureManagerNbAgg.display_jsc                 C   s,   | j s|   |   n| j  d| _ d S )NT)r.   r7   _create_commr   	draw_idler6   r   r   r   showW   s
   


zFigureManagerNbAgg.showc                 C   s   d| _ |   dS )zJ
        A special method to re-show the figure in the notebook.

        FN)r.   r:   r6   r   r   r   reshow_   s   zFigureManagerNbAgg.reshowc                 C   s
   t | jS r4   )boolr   r6   r   r   r   	connectedg   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StringIOr/   r5   writepathlibZPath__file__parentZ	read_textgetvalue)clsr>   outputr2   r   r   r5   k   s   
z!FigureManagerNbAgg.get_javascriptc                 C   s   t | }| | |S r4   )
CommSocketZadd_web_socketr1   commr   r   r   r8   x   s   
zFigureManagerNbAgg._create_commc                 C   s.   |  d t| jD ]}|  q
|   d S )Nclose)Z_send_eventlistr   on_closeclearup_closedrJ   r   r   r   destroy}   s   

zFigureManagerNbAgg.destroyc                 C   s2   dd | j D | _ t| j dkr| 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   N)r   r   r   close_eventr6   r   r   r   rO      s   z!FigureManagerNbAgg.clearup_closedc                    s    fdd| j D | _ d S )Nc                    s   h | ]
}|j j kr|qS r   )rK   comm_idrR   rU   r   r   rS      s    

z1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r1   rU   r   rV   r   remove_comm      zFigureManagerNbAgg.remove_commr4   )r*   r+   r,   r'   Z
ToolbarClsr0   r7   r:   r;   propertyr=   classmethodr5   r8   rP   rO   rW   __classcell__r   r   r2   r   r-   J   s    
r-   c                   @   s   e Zd ZdS )FigureCanvasNbAggN)r*   r+   r,   r   r   r   r   r\      s    r\   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 )rI   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contentrU   )
_ext_closerW   rO   )Zclose_messager   r1   r   r   	_on_close   s   z&CommSocket.__init__.<locals>._on_close)supports_binaryr   struuidZuuid4r   r   r   rK   AttributeErrorRuntimeErrorZon_msg
on_messagera   rN   )r1   r   errrc   r   rb   r   r0      s"   zCommSocket.__init__c                 C   s   | j p| jj S r4   )ra   rK   Z_closedr6   r   r   r   rQ      s   zCommSocket.is_openc                 C   s2   |   rz| j  W d S  ty   Y d S w d S r4   )rQ   rK   rL   KeyErrorr6   r   r   r   rN      s   zCommSocket.on_closec                 C   s   | j dt|i d S )Nr_   )rK   sendjsondumps)r1   r`   r   r   r   	send_json   rX   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,{0}r_   )rd   rK   rl   r   decoder   )r1   rp   r_   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 )Nr`   r_   typeclosingrd   value)rm   loadsrN   r   rO   rd   Zhandle_json)r1   messager   r   r   ri      s   zCommSocket.on_messageN)
r*   r+   r,   __doc__r0   rQ   rN   ro   rs   ri   r   r   r   r   rI      s    	

rI   c                   @   s.   e Zd ZeZeZedd ZedddZ	dS )_BackendNbAggc                    sJ   t | t | t r  |j   fdd} d|S )Nc                    s      t d S r4   )mpl_disconnectr	   rP   )Zeventr   Zcidr   r   r   rP      s   
z>_BackendNbAgg.new_figure_manager_given_figure.<locals>.destroyrT   )r\   r-   r   r:   r   r9   Zmpl_connect)r   r   rP   r   r|   r   new_figure_manager_given_figure   s   

z-_BackendNbAgg.new_figure_manager_given_figureNc                 C   sd   ddl m} | }|sd S t }|D ]}|  t|dr%|j|j |s/|j	
|jd  qd S )Nr   r   _cidgcf)matplotlib._pylab_helpersr	   r   r   r:   hasattrr   r{   r~   r    popr   )blockr	   Zmanagersinteractiver   r   r   r   r:      s   
z_BackendNbAgg.showr4   )
r*   r+   r,   r\   ZFigureCanvasr-   ZFigureManagerstaticmethodr}   r:   r   r   r   r   rz      s    
rz   )$ry   base64r   r@   rm   rC   rf   ZIPython.displayr   r   r   Zipykernel.commr   ImportErrorZIPython.kernel.commr]   r   r   r	   Zmatplotlib.backend_basesr
   r   Z'matplotlib.backends.backend_webagg_corer   r   r   r   r   r#   r(   r'   r-   r\   rI   Zexportrz   r   r   r   r   <module>   s>    GM