a
    ëÝEbÕ!  ã                   @   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   úa/home/ja/django-apps/lartica_env/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ÚbackÚ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   r2   M   s    zFigureManagerNbAgg.__init__c                 C   s   t tt ¡ ƒƒ d S ©N)r   r   r/   Úget_javascript©r3   r   r   r   Ú
display_jsQ   s    zFigureManagerNbAgg.display_jsc                 C   s,   | j s|  ¡  |  ¡  n
| j ¡  d| _ d S )NT)r0   r9   Ú_create_commr   Ú	draw_idler8   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)r0   r<   r8   r   r   r   Úreshow_   s    zFigureManagerNbAgg.reshowc                 C   s
   t | jƒS r6   )Úboolr   r8   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ÚStringIOr1   r7   ÚwriteÚpathlibÚPathÚ__file__ÚparentÚ	read_textÚgetvalue)Úclsr@   Úoutputr4   r   r   r7   k   s    
ÿþz!FigureManagerNbAgg.get_javascriptc                 C   s   t | ƒ}|  |¡ |S r6   )Ú
CommSocketZadd_web_socket©r3   Úcommr   r   r   r:   x   s    
zFigureManagerNbAgg._create_commc                 C   s.   |   d¡ t| jƒD ]}| ¡  q|  ¡  d S )NÚclose)Z_send_eventÚlistr   Úon_closeÚclearup_closedrN   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_eventr8   r   r   r   rS   „   s    z!FigureManagerNbAgg.clearup_closedc                    s   ‡ fdd„| j D ƒ| _ d S )Nc                    s   h | ]}|j jˆ kr|’qS r   )rO   Úcomm_idrV   ©rY   r   r   rW   �   s   ÿz1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r3   rY   r   rZ   r   Úremove_commŒ   s    zFigureManagerNbAgg.remove_comm)N)r,   r-   r.   r)   Z
ToolbarClsr2   r9   r<   r=   Úpropertyr?   Úclassmethodr7   r:   rT   rS   r[   Ú__classcell__r   r   r4   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 )rM   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ÚcontentrY   )Ú
_ext_closer[   rS   )Zclose_message©r   r3   r   r   Ú	_on_close±   s    z&CommSocket.__init__.<locals>._on_close)Úsupports_binaryr   ÚstrÚuuidÚuuid4r   r   r   rO   ÚAttributeErrorÚRuntimeErrorZon_msgÚ
on_messagerd   rR   )r3   r   Úerrrf   r   re   r   r2   Ÿ   s    þzCommSocket.__init__c                 C   s   | j p| jj S r6   )rd   rO   Ú_closedr8   r   r   r   rU   ¸   s    zCommSocket.is_openc                 C   s.   |   ¡ r*z| j ¡  W n ty(   Y n0 d S r6   )rU   rO   rP   ÚKeyErrorr8   r   r   r   rR   »   s
    zCommSocket.on_closec                 C   s   | j  dt |¡i¡ d S )Nrb   )rO   ÚsendÚjsonÚdumps)r3   rc   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}rb   )rg   rO   rq   r   Údecoder   )r3   ru   rb   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 )Nrc   rb   ÚtypeÚclosingrg   Úvalue)rr   ÚloadsrR   r   rS   rg   Zhandle_json)r3   Úmessager   r   r   rm   Ò   s    zCommSocket.on_messageN)
r,   r-   r.   Ú__doc__r2   rU   rR   rt   ry   rm   r   r   r   r   rM   •   s   	

rM   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 r6   )Úmpl_disconnectr	   rT   )Úevent©r   Zcidr   r   r   rT   ï   s    
z>_BackendNbAgg.new_figure_manager_given_figure.<locals>.destroyrX   )r_   r/   r   r<   r   r;   Zmpl_connect)r   r   rT   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   rB   rr   rE   ri   Ú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_   rM   Zexportr€   r   r   r   r   Ú<module>   s:   ùGM