a
    ض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yr   ddlmZ Y n0 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   q/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-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)home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   r1   M   s    zFigureManagerNbAgg.__init__c                 C   s   t tt  d S N)r   r   r.   get_javascriptr2   r   r   r   
display_jsQ   s    zFigureManagerNbAgg.display_jsc                 C   s,   | j s|   |   n
| j  d| _ d S )NT)r/   r8   _create_commr   	draw_idler7   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;   r7   r   r   r   reshow_   s    zFigureManagerNbAgg.reshowc                 C   s
   t | jS r5   )boolr   r7   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 rR|
 S d S )N)streamzweb_backend/js/nbagg_mpl.jszutf-8)encoding)ioStringIOr0   r6   writepathlibPath__file__parent	read_textgetvalue)clsr?   outputr3   r   r   r6   k   s    
z!FigureManagerNbAgg.get_javascriptc                 C   s   t | }| | |S r5   )
CommSocketZadd_web_socketr2   commr   r   r   r9   x   s    
zFigureManagerNbAgg._create_commc                 C   s.   |  d t| jD ]}|  q|   d S )Nclose)Z_send_eventlistr   on_closeclearup_closedrM   r   r   r   destroy}   s    

zFigureManagerNbAgg.destroyc                 C   s.   dd | j D | _ t| j dkr*| j  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_eventr7   r   r   r   rR      s    z!FigureManagerNbAgg.clearup_closedc                    s    fdd| j D | _ d S )Nc                    s   h | ]}|j j kr|qS r   )rN   comm_idrU   rX   r   r   rV      s   z1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r2   rX   r   rY   r   remove_comm   s    zFigureManagerNbAgg.remove_comm)N)r+   r,   r-   r(   Z
ToolbarClsr1   r8   r;   r<   propertyr>   classmethodr6   r9   rS   rR   rZ   __classcell__r   r   r3   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 )rL   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	yr } zt
d|W Y d }~n
d }~0 0 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contentrX   )
_ext_closerZ   rR   )Zclose_messager   r2   r   r   	_on_close   s    z&CommSocket.__init__.<locals>._on_close)supports_binaryr   struuiduuid4r   r   r   rN   AttributeErrorRuntimeErrorZon_msg
on_messagerc   rQ   )r2   r   errre   r   rd   r   r1      s    zCommSocket.__init__c                 C   s   | j p| jj S r5   )rc   rN   _closedr7   r   r   r   rT      s    zCommSocket.is_openc                 C   s.   |   r*z| j  W n ty(   Y n0 d S r5   )rT   rN   rO   KeyErrorr7   r   r   r   rQ      s
    zCommSocket.on_closec                 C   s   | j dt|i d S )Nra   )rN   sendjsondumps)r2   rb   r   r   r   	send_json   s    zCommSocket.send_jsonc                 C   sJ   | j r| jjddi|gd n(t|d}d|}| jd|i d S )Nblobz	image/png)buffersasciizdata:image/png;base64,{0}ra   )rf   rN   rp   r   decoder   )r2   rt   ra   Zdata_urir   r   r   send_binary   s
    
zCommSocket.send_binaryc                 C   sZ   t |d d }|d dkr2|   | j  n$|d dkrJ|d | _n| j| d S )Nrb   ra   typeclosingrf   value)rq   loadsrQ   r   rR   rf   Zhandle_json)r2   messager   r   r   rl      s    zCommSocket.on_messageN)
r+   r,   r-   __doc__r1   rT   rQ   rs   rx   rl   r   r   r   r   rL      s   	

rL   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 r5   )mpl_disconnectr	   rS   )eventr   Zcidr   r   r   rS      s    
z>_BackendNbAgg.new_figure_manager_given_figure.<locals>.destroyrW   )r^   r.   r   r;   r   r:   Zmpl_connect)r   r   rS   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 ]8}|  t|drJ|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.show)N)
r+   r,   r-   r^   ZFigureCanvasr.   ZFigureManagerstaticmethodr   r;   r   r   r   r   r      s   
r   )$r~   base64r   rA   rq   rD   rh   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^   rL   Zexportr   r   r   r   r   <module>   s:   GM