o
    ig!                     @   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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 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)cbookis_interactiveGcf)_BackendNavigationToolbar2)FigureCanvasWebAggCoreFigureManagerWebAggNavigationToolbar2WebAggTimerTornadoc                  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 {})figsocket)formatcanvasfigure	get_labelnumweb_sockets).0manager r   \/var/www/edux/Edux_v2/venv/lib/python3.10/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backforward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   texttooltip_text
image_filename_of_methodr   r   r   r   A   s    
zNavigationIPy.<listcomp>))DownloadzDownload plotr+   r+   N)__name__
__module____qualname__r   	toolitemsr   r   r   r   r,   >   s    
r,   c                       sr   e Zd ZeZ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                 C   s   d| _ t| || d S )NF)_shownr   __init__)selfr   r   r   r   r   r9   L   s   zFigureManagerNbAgg.__init__c                 C   s   t tt  d S N)r   r   r7   get_javascriptr:   r   r   r   
display_jsP   s   zFigureManagerNbAgg.display_jsc                 C   s,   | j s|   |   n| j  d| _ d S )NT)r8   r>   _create_commr   	draw_idler=   r   r   r   showV   s
   


zFigureManagerNbAgg.showc                 C   s   d| _ |   dS )zJ
        A special method to re-show the figure in the notebook.

        FN)r8   rA   r=   r   r   r   reshow^   s   zFigureManagerNbAgg.reshowc                 C   s
   t | jS r;   )boolr   r=   r   r   r   	connectedf   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StringIOsuperr<   writepathlibPath__file__parent	read_textgetvalue)clsrE   output	__class__r   r   r<   j   s   
z!FigureManagerNbAgg.get_javascriptc                 C   s   t | }| | |S r;   )
CommSocketadd_web_socketr:   commr   r   r   r?   w   s   
zFigureManagerNbAgg._create_commc                 C   s.   |  d t| jD ]}|  q
|   d S )Nclose)_send_eventlistr   on_closeclearup_closedrW   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_eventr=   r   r   r   r]      s   z!FigureManagerNbAgg.clearup_closedc                    s    fdd| j D | _ d S )Nc                    s   h | ]
}|j j kr|qS r   )rX   comm_idr`   rc   r   r   ra      s    z1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r:   rc   r   rd   r   remove_comm      zFigureManagerNbAgg.remove_commr;   )r3   r4   r5   r,   
ToolbarClsr9   r>   rA   rB   propertyrD   classmethodr<   r?   r^   r]   re   __classcell__r   r   rS   r   r7   I   s    
r7   c                   @   s   e Zd ZeZdS )FigureCanvasNbAggN)r3   r4   r5   r   
_timer_clsr   r   r   r   rk      s    rk   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 )rU   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contentrc   )
_ext_closere   r]   )close_messager   r:   r   r   	_on_close   s   z&CommSocket.__init__.<locals>._on_close)supports_binaryr   struuiduuid4r   r   r   rX   AttributeErrorRuntimeErroron_msg
on_messagerq   r\   )r:   r   errrt   r   rs   r   r9      s"   zCommSocket.__init__c                 C   s   | j p| jj S r;   )rq   rX   _closedr=   r   r   r   r_      s   zCommSocket.is_openc                 C   s2   |   rz| j  W d S  ty   Y d S w d S r;   )r_   rX   rY   KeyErrorr=   r   r   r   r\      s   zCommSocket.on_closec                 C   s   | j dt|i d S )Nro   )rX   sendjsondumps)r:   rp   r   r   r   	send_json   rf   zCommSocket.send_jsonc                 C   s,   t |d}d|}| jd|i d S )Nasciizdata:image/png;base64,{0}ro   )r   decoder   rX   r   )r:   blobro   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 )Nrp   ro   typeclosingru   value)r   loadsr\   r   r]   ru   handle_json)r:   messager   r   r   r|      s   zCommSocket.on_messageN)
r3   r4   r5   __doc__r9   r_   r\   r   r   r|   r   r   r   r   rU      s    	
rU   c                   @   s:   e Zd ZeZeZedd Zedd Z	edddZ
dS )	_BackendNbAggc                    sB   t |}t||  t r   |j  |d fdd  S )Nrb   c                    s
   t  S r;   )r
   r^   )eventr   r   r   <lambda>   s   
 z?_BackendNbAgg.new_figure_manager_given_figure.<locals>.<lambda>)rk   r7   r   rA   r   r@   mpl_connect)r   r   r   r   r   r   new_figure_manager_given_figure   s   

z-_BackendNbAgg.new_figure_manager_given_figurec                 C   s   |    d S r;   )rA   r   r   r   r   trigger_manager_draw   s   z"_BackendNbAgg.trigger_manager_drawNc                 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   rA   hasattrr   mpl_disconnectr   r"   popr   )blockr
   managersinteractiver   r   r   r   rA      s   
z_BackendNbAgg.showr;   )r3   r4   r5   rk   FigureCanvasr7   FigureManagerstaticmethodr   r   rA   r   r   r   r   r      s    
	
r   )$r   base64r   rG   r   rK   rw   IPython.displayr   r   r   ipykernel.commr   ImportErrorIPython.kernel.commrm   r   r   r   r
   matplotlib.backend_basesr   r   'matplotlib.backends.backend_webagg_corer   r   r   r   r%   r-   r,   r7   rk   rU   exportr   r   r   r   r   <module>   s>    GJ