U
    d»{j-  ã                   @   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 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Ú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   ú`/var/www/html/bwave/python/venv/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   r2   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   r8   O   s    
z6FigureManagerNbAgg.create_with_canvas.<locals>.destroyÚclose_event)r   ÚshowÚ	draw_idleZmpl_connect)ÚclsZcanvas_classr   r   r8   r   r:   r   Úcreate_with_canvasG   s    
z%FigureManagerNbAgg.create_with_canvasc                 C   s   t tt ¡ ƒƒ d S r6   )r   r   r/   Úget_javascript©r3   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   )r0   rB   Ú_create_commr   r=   Úhasattrr7   rC   r   Úmatplotlib._pylab_helpersr	   r    Úpopr   )r3   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)r0   r<   rA   r   r   r   Úreshowm   s    zFigureManagerNbAgg.reshowc                 C   s
   t | jƒS r6   )Úboolr   rA   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ÚStringIOr1   r@   ÚwriteÚpathlibÚPathÚ__file__ÚparentÚ	read_textÚgetvalue)r>   rK   Úoutputr4   r   r   r@   y   s    
ÿþz!FigureManagerNbAgg.get_javascriptc                 C   s   t | ƒ}|  |¡ |S r6   )Ú
CommSocketZadd_web_socket©r3   Úcommr   r   r   rD   †   s    
zFigureManagerNbAgg._create_commc                 C   s.   |   d¡ t| jƒD ]}| ¡  q|  ¡  d S )NÚclose)Z_send_eventÚlistr   Úon_closeÚclearup_closedrX   r   r   r   r8   ‹   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   r;   rA   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   )rY   Úcomm_idr_   ©ra   r   r   r`   ›   s    ÿz1FigureManagerNbAgg.remove_comm.<locals>.<setcomp>)r   )r3   ra   r   rb   r   Úremove_commš   s    zFigureManagerNbAgg.remove_comm)N)r,   r-   r.   r)   Z_toolbar2_classZ
ToolbarClsr2   Úclassmethodr?   rB   r<   rH   ÚpropertyrJ   r@   rD   r8   r]   rc   Ú__classcell__r   r   r4   r   r/   @   s   

r/   c                   @   s   e Zd ZeZdS )ÚFigureCanvasNbAggN)r,   r-   r.   r/   Zmanager_classr   r   r   r   rg   Ÿ   s   rg   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 )rW   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Úcontentra   )Ú
_ext_closerc   r]   )Zclose_message©r   r3   r   r   Ú	_on_close¿   s    z&CommSocket.__init__.<locals>._on_close)Úsupports_binaryr   ÚstrÚuuidÚuuid4r   r   r   rY   ÚAttributeErrorÚRuntimeErrorZon_msgÚ
on_messagerl   r\   )r3   r   Úerrrn   r   rm   r   r2   ­   s    þzCommSocket.__init__c                 C   s   | j p| jj S r6   )rl   rY   Ú_closedrA   r   r   r   r^   Æ   s    zCommSocket.is_openc                 C   s0   |   ¡ r,z| j ¡  W n tk
r*   Y nX d S r6   )r^   rY   rZ   ÚKeyErrorrA   r   r   r   r\   É   s
    zCommSocket.on_closec                 C   s   | j  dt |¡i¡ d S )Nrj   )rY   ÚsendÚjsonÚdumps)r3   rk   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}rj   )ro   rY   ry   r   Údecoder   )r3   r}   rj   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 )Nrk   rj   ÚtypeÚclosingro   Úvalue)rz   Úloadsr\   r   r]   ro   Zhandle_json)r3   Úmessager   r   r   ru   à   s    zCommSocket.on_messageN)
r,   r-   r.   Ú__doc__r2   r^   r\   r|   r�   ru   r   r   r   r   rW   £   s   	

rW   c                   @   s   e Zd ZeZeZdS )Ú_BackendNbAggN)r,   r-   r.   rg   ZFigureCanvasr/   ZFigureManagerr   r   r   r   rˆ   ð   s   rˆ   )"r‡   Úbase64r   rM   rz   rP   rq   Zipykernel.commr   ÚIPython.displayr   r   r   rh   r   rF   r	   Zmatplotlib.backend_basesr
   r   Zbackend_webagg_corer   r   r   r   r   r#   r*   r)   r/   rg   rW   Zexportrˆ   r   r   r   r   Ú<module>   s6   ù_M