a
    ìÝEb:*  ã                
   @   sX  d Z ddlmZ ddlZddlmZ ddlZddl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ZzddlZW n. ey¢ Z zedƒe‚W Y dZ[n
dZ[0 0 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 d	d
lmZ d	dlmZm Z  G dd„ dej!ƒZ"e"ƒ Z#G dd„ dej$ƒZ%G dd„ dej&j'ƒZ(dd„ Z)ej*G dd„ deƒƒZ+dS )z8
Displays Agg images in the browser, with interactivity
é    )ÚcontextmanagerN)ÚBytesIO)ÚPathz$The WebAgg backend requires Tornado.)Ú_Backend)ÚGcfé   )Úbackend_webagg_core)ÚTimerAsyncioÚTimerTornadoc                   @   s   e Zd Zdd„ ZdS )ÚServerThreadc                 C   s   t jj ¡  ¡  d S ©N)ÚtornadoÚioloopÚIOLoopÚinstanceÚstart©Úself© r   úb/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/matplotlib/backends/backend_webagg.pyÚrun+   s    zServerThread.runN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   *   s   r   c                   @   s   e Zd ZdS )ÚFigureCanvasWebAggN)r   r   r   r   r   r   r   r   2   s   r   c                       s¸   e Zd ZdZdZG dd„ dejjƒZG dd„ dejjƒZ	G dd„ dejjƒZ
G dd	„ d	ejjƒZG d
d„ dejjƒZG dd„ dejjƒZd‡ fdd„	Zeddd„ƒZedd„ ƒZ‡  ZS )ÚWebAggApplicationFc                   @   s   e Zd Zdd„ ZdS )zWebAggApplication.FavIconc                 C   s(   |   dd¡ |  tt ¡ dƒ ¡ ¡ d S )NúContent-Typez	image/pngzimages/matplotlib.png)Ú
set_headerÚwriter   ÚmplÚget_data_pathÚ
read_bytesr   r   r   r   Úget;   s    ÿzWebAggApplication.FavIcon.getN©r   r   r   r"   r   r   r   r   ÚFavIcon:   s   r$   c                       s*   e Zd Zddœ‡ fdd„
Zdd„ Z‡  ZS )z"WebAggApplication.SingleFigurePageÚ ©Ú
url_prefixc                   s    || _ tƒ j||fi |¤Ž d S r   ©r'   ÚsuperÚ__init__©r   ZapplicationÚrequestr'   Úkwargs©Ú	__class__r   r   r*   A   s    z+WebAggApplication.SingleFigurePage.__init__c                 C   sF   t |ƒ}t |¡}dj| j| jd�}| jd| j||tjj	|j
d� d S )Núws://{req.host}{prefix}/©ÚreqÚprefixzsingle_figure.html)r3   Úws_uriÚfig_idÚ	toolitemsÚcanvas)Úintr   Úget_fig_managerÚformatr,   r'   ÚrenderÚcoreÚNavigationToolbar2WebAggr6   r7   )r   ÚfignumÚmanagerr4   r   r   r   r"   E   s    
ÿúz&WebAggApplication.SingleFigurePage.get©r   r   r   r*   r"   Ú__classcell__r   r   r.   r   ÚSingleFigurePage@   s   rB   c                       s*   e Zd Zddœ‡ fdd„
Zdd„ Z‡  ZS )z WebAggApplication.AllFiguresPager%   r&   c                   s    || _ tƒ j||fi |¤Ž d S r   r(   r+   r.   r   r   r*   T   s    z)WebAggApplication.AllFiguresPage.__init__c                 C   s:   dj | j| jd�}| jd| j|ttj ¡ ƒtj	j
d� d S )Nr0   r1   zall_figures.html)r3   r4   Zfiguresr6   )r:   r,   r'   r;   Úsortedr   ZfigsÚitemsr<   r=   r6   )r   r4   r   r   r   r"   X   s    ÿûz$WebAggApplication.AllFiguresPage.getr@   r   r   r.   r   ÚAllFiguresPageS   s   rE   c                   @   s   e Zd Zdd„ ZdS )zWebAggApplication.MplJsc                 C   s$   |   dd¡ tj ¡ }|  |¡ d S )Nr   zapplication/javascript)r   r<   ÚFigureManagerWebAggZget_javascriptr   )r   Z
js_contentr   r   r   r"   c   s    
zWebAggApplication.MplJs.getNr#   r   r   r   r   ÚMplJsb   s   rG   c                   @   s   e Zd Zdd„ ZdS )zWebAggApplication.Downloadc                 C   sR   t |ƒ}t |¡}|  dtj |d¡¡ tƒ }|jj	j
||d� |  | ¡ ¡ d S )Nr   Úbinary)r:   )r8   r   r9   r   Ú	mimetypesÚ	types_mapr"   r   r7   ÚfigureZsavefigr   Úgetvalue)r   r>   Úfmtr?   Zbuffr   r   r   r"   k   s    
ÿzWebAggApplication.Download.getNr#   r   r   r   r   ÚDownloadj   s   rN   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )zWebAggApplication.WebSocketTc                 C   s<   t |ƒ| _t | j¡| _| j | ¡ t| dƒr8|  d¡ d S )NÚset_nodelayT)r8   r>   r   r9   r?   Zadd_web_socketÚhasattrrO   )r   r>   r   r   r   Úopenw   s
    

z WebAggApplication.WebSocket.openc                 C   s   | j  | ¡ d S r   )r?   Zremove_web_socketr   r   r   r   Úon_close~   s    z$WebAggApplication.WebSocket.on_closec                 C   sD   t  |¡}|d dkr"|d | _nt | j¡}|d ur@| |¡ d S )NÚtypeÚsupports_binaryÚvalue)ÚjsonÚloadsrT   r   r9   r>   Zhandle_json)r   Úmessager?   r   r   r   Ú
on_message�   s    
z&WebAggApplication.WebSocket.on_messagec                 C   s   |   t |¡¡ d S r   )Úwrite_messagerV   Údumps)r   Úcontentr   r   r   Ú	send_json�   s    z%WebAggApplication.WebSocket.send_jsonc                 C   s<   | j r| j|dd� n"d | d¡ dd¡¡}|  |¡ d S )NT)rH   zdata:image/png;base64,{0}Úbase64Ú
r%   )rT   rZ   r:   ÚencodeÚreplace)r   ZblobZdata_urir   r   r   Úsend_binary“   s    ÿz'WebAggApplication.WebSocket.send_binaryN)	r   r   r   rT   rQ   rR   rY   r]   rb   r   r   r   r   Ú	WebSockett   s   rc   r%   c              
      sÊ   |r$|d dkr|d dks$J dƒ‚t ƒ j|d tjjdtj ¡ if|d tjjdtt	 
¡ dƒif|d	 | jf|d
 | jd|if|d | jd|if|d | jf|d | jf|d | jfgtj ¡ d� d S )Nr   ú/éÿÿÿÿz6url_prefix must start with a "/" and not end with one.z/_static/(.*)Úpathz/_images/(.*)Zimagesz/favicon.icoz	/([0-9]+)r'   z/?z
/js/mpl.jsz/([0-9]+)/wsz/([0-9]+)/download.([a-z0-9.]+))Ztemplate_path)r)   r*   r   ÚwebZStaticFileHandlerr<   rF   Úget_static_file_pathr   r   r    r$   rB   rE   rG   rc   rN   )r   r'   r.   r   r   r*   ›   s8    ÿþþ
ÿ
ÿÿã ßzWebAggApplication.__init__Nc                 C   sÆ   | j r
d S | |d�}|| _dd„ }|d u r8tjd | _n|| _tjd | _|| jtjd ƒD ]V}z| || j¡ W n4 tjy¦ } z|j	t	j
kr’‚ W Y d }~q\d }~0 0 || _ q¼q\tdƒ‚d| _ d S )	Nr&   c                 s   sN   t td|ƒƒD ]}| | V  qt |d ƒD ]}| t d| d| ¡ V  q*dS )zâ
            Generate a list of n random ports near the given port.

            The first 5 ports will be sequential, and the remaining n-5 will be
            randomly selected in the range [port-2*n, port+2*n].
            é   éþÿÿÿé   N)ÚrangeÚminÚrandomÚrandint)ÚportÚnÚir   r   r   Úrandom_portsÏ   s    z2WebAggApplication.initialize.<locals>.random_portszwebagg.addresszwebagg.portzwebagg.port_retrieszSThe webagg server could not be started because an available port could not be foundT)Úinitializedr'   r   ÚrcParamsÚaddressrp   ÚlistenÚsocketÚerrorÚerrnoÚ
EADDRINUSEÚ
SystemExit)Úclsr'   rp   rv   Zapprs   Úer   r   r   Ú
initializeÃ   s.    
ÿÿzWebAggApplication.initializec                    s€   ˆ j r
d S tjj ¡ ‰‡ ‡fdd„‰t‡‡fdd„ƒ}dˆ _ tdƒ tj 	¡  |ƒ � ˆ 
¡  W d   ƒ n1 sr0    Y  d S )Nc                      s$   ˆ  ¡  tdƒ tj ¡  dˆ _d S )NzServer is stoppedF)ÚstopÚprintÚsysÚstdoutÚflushÚstartedr   )r}   r   r   r   Úshutdownÿ   s    
z)WebAggApplication.start.<locals>.shutdownc               
   3   sF   t   t j‡ ‡fdd„¡} zd V  W t   t j| ¡ nt   t j| ¡ 0 d S )Nc                    s
   ˆ   ˆ¡S r   )Zadd_callback_from_signal)ÚsigÚframe©r   r†   r   r   Ú<lambda>	  ó    z?WebAggApplication.start.<locals>.catch_sigint.<locals>.<lambda>)ÚsignalÚSIGINT)Zold_handlerr‰   r   r   Úcatch_sigint  s    þz-WebAggApplication.start.<locals>.catch_sigintTz"Press Ctrl+C to stop WebAgg server)r…   r   r   r   r   r   r�   r‚   rƒ   r„   r   )r}   rŽ   r   )r}   r   r†   r   r   ñ   s    	

zWebAggApplication.start)r%   )r%   NN)r   r   r   rt   r…   r   rg   ZRequestHandlerr$   rB   rE   rG   rN   Z	websocketZWebSocketHandlerrc   r*   Úclassmethodr   r   rA   r   r   r.   r   r   6   s   
'(-r   c                 C   sj   dd l }t ¡  t ¡ s t ¡  | j}ttj	 
¡ dƒ ¡ }|j |¡}|jtj|tjj| jtjd� d¡S )Nr   zipython_inline_figure.html)r3   r5   r6   r7   rp   zutf-8)Ztornado.templater   r   Úwebagg_server_threadÚis_aliver   Únumberr   r<   rF   rh   Ú	read_textÚtemplateÚTemplateÚgenerater'   r=   r6   r7   rp   Údecode)rK   r   r>   ZtplÚtr   r   r   Úipython_inline_display  s$    
ÿûûr™   c                   @   s"   e Zd ZeZejZedd„ ƒZ	dS )Ú_BackendWebAggc                  C   sd   t  ¡  djt jt jt jd�} tjd rJdd l}| 	| ¡sXt
d | ¡ƒ nt
d | ¡ƒ t  ¡  d S )Nzhttp://{address}:{port}{prefix})rv   rp   r3   zwebagg.open_in_browserr   zTo view figure, visit {0})r   r   r:   rv   rp   r'   r   ru   Ú
webbrowserrQ   r�   r   )Úurlr›   r   r   r   Úshow0  s    ý

z_BackendWebAgg.showN)
r   r   r   r   ZFigureCanvasr<   rF   ZFigureManagerÚstaticmethodr�   r   r   r   r   rš   +  s   rš   ),Ú__doc__Ú
contextlibr   rz   Úior   rV   rI   Úpathlibr   rn   r‚   rŒ   rx   Ú	threadingr   ÚImportErrorÚerrÚRuntimeErrorZtornado.webZtornado.ioloopZtornado.websocketZ
matplotlibr   Zmatplotlib.backend_basesr   Zmatplotlib._pylab_helpersr   r%   r   r<   r	   r
   ÚThreadr   r�   ZFigureCanvasWebAggCorer   rg   ÚApplicationr   r™   Zexportrš   r   r   r   r   Ú<module>   s>     c