U
    ÂmœdQ  ã                   @   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   úZ/home/sam/Atlas/atlas_env/lib/python3.8/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)Ú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 k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   r3   C   s    zFigureManagerNbAgg.__init__c                    sH   ||ƒ‰ | ˆ |ƒ‰t ƒ r(ˆ ¡  ˆ  ¡  ‡ ‡‡fdd„}ˆ  d|¡‰ˆS )Nc                    s   ˆ   ˆ¡ t ˆ¡ d S ©N)Úmpl_disconnectr	   Údestroy)Úevent©r   Zcidr   r   r   r9   O   s    
z6FigureManagerNbAgg.create_with_canvas.<locals>.destroyÚclose_event)r   ÚshowÚ	draw_idleZmpl_connect)ÚclsZcanvas_classr   r   r9   r   r;   r   Úcreate_with_canvasG   s    
z%FigureManagerNbAgg.create_with_canvasc                 C   s   t tt ¡ ƒƒ d S r7   )r   r   r0   Úget_javascript©r4   r   r   r   Ú
display_jsV   s    zFigureManagerNbAgg.display_jsc                 C   sf   | j s|  ¡  |  ¡  n
| j ¡  d| _ t| dƒr@| j | j¡ tƒ sbddl	m
} |j | jd ¡ d S )NTÚ_cidgcfr   r   )r1   rC   Ú_create_commr   r>   Úhasattrr8   rD   r   Úmatplotlib._pylab_helpersr	   r!   Úpopr   )r4   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)r1   r=   rB   r   r   r   Úreshowm   s    zFigureManagerNbAgg.reshowc                 C   s
   t | jƒS r7   )Úboolr   rB   r   r   r   Ú	connectedu   s    zFigureManagerNbAgg.connectedNc                    sV   |d krt  ¡ }n|}tƒ j|d� | t t¡jd j	dd�¡ |d krR| 
¡ S d S )N)Ústreamzweb_backend/js/nbagg_mpl.jszutf-8)Úencoding)ÚioÚStringIOr2   rA   ÚwriteÚpathlibÚPathÚ__file__ÚparentÚ	read_textÚgetvalue)r?   rL   Úoutputr5   r   r   rA   y   s    
ÿþz!FigureManagerNbAgg.get_javascriptc                 C   s   t | ƒ}|  |¡ |S r7   )Ú
CommSocketZadd_web_socket©r4   Úcommr   r   r   rE   †   s    
zFigureManagerNbAgg._create_commc                 C   s.   |   d¡ t| jƒD ]}| ¡  q|  ¡  d S )NÚclose)Z_send_eventÚlistr   Úon_closeÚclearup_closedrY   r   r   r   r9   ‹   s    

zFigureManagerNbAgg.destroyc                 C   s4   dd„ | j D ƒ| _ t| j ƒdkr0td| 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   r<   N)r   r    r   r   Z_processrB   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   )rZ   Úcomm_idr`   ©rb   r   r   ra   ›   s    ÿz1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r4   rb   r   rc   r   Úremove_commš   s    zFigureManagerNbAgg.remove_comm)N)r-   r.   r/   r*   Z_toolbar2_classZ
ToolbarClsr3   Úclassmethodr@   rC   r=   rI   ÚpropertyrK   rA   rE   r9   r^   rd   Ú__classcell__r   r   r5   r   r0   @   s   

r0   c                   @   s   e Zd ZeZdS )ÚFigureCanvasNbAggN)r-   r.   r/   r0   Zmanager_classr   r   r   r   rh   Ÿ   s   rh   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 )rX   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	k
rp } zt
dƒ|‚W 5 d }~X Y nX ˆ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Úcontentrb   )Ú
_ext_closerd   r^   )Zclose_message©r   r4   r   r   Ú	_on_close¿   s    z&CommSocket.__init__.<locals>._on_close)Úsupports_binaryr   ÚstrÚuuidÚuuid4r   r   r   rZ   ÚAttributeErrorÚRuntimeErrorZon_msgÚ
on_messagerm   r]   )r4   r   Úerrro   r   rn   r   r3   ­   s    þzCommSocket.__init__c                 C   s   | j p| jj S r7   )rm   rZ   Ú_closedrB   r   r   r   r_   Æ   s    zCommSocket.is_openc                 C   s0   |   ¡ r,z| j ¡  W n tk
r*   Y nX d S r7   )r_   rZ   r[   ÚKeyErrorrB   r   r   r   r]   É   s
    zCommSocket.on_closec                 C   s   | j  dt |¡i¡ d S )Nrk   )rZ   ÚsendÚjsonÚdumps)r4   rl   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}rk   )rp   rZ   rz   r   Údecoder   )r4   r~   rk   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 )Nrl   rk   ÚtypeÚclosingrp   Úvalue)r{   Úloadsr]   r   r^   rp   Zhandle_json)r4   Úmessager   r   r   rv   à   s    zCommSocket.on_messageN)
r-   r.   r/   Ú__doc__r3   r_   r]   r}   r‚   rv   r   r   r   r   rX   £   s   	

rX   c                   @   s   e Zd ZeZeZdS )Ú_BackendNbAggN)r-   r.   r/   rh   ZFigureCanvasr0   ZFigureManagerr   r   r   r   r‰   ð   s   r‰   )#rˆ   Úbase64r   rN   r{   rQ   rr   Zipykernel.commr   ÚIPython.displayr   r   r   ri   r   rG   r	   Zmatplotlib.backend_basesr
   r   r   Zbackend_webagg_corer   r   r   r   r   r$   r+   r*   r0   rh   rX   Zexportr‰   r   r   r   r   Ú<module>   s6   ù_M