U
    d»{jù*  ã                
   @   sh  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k
r  Z zedƒe‚W 5 dZ[X Y nX 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%ƒ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   úa/var/www/html/bwave/python/venv/lib/python3.8/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ejZdS )ÚFigureManagerWebAggN)r   r   r   ÚcoreÚNavigationToolbar2WebAggZ_toolbar2_classr   r   r   r   r   3   s   r   c                   @   s   e Zd ZeZdS )ÚFigureCanvasWebAggN)r   r   r   r   Zmanager_classr   r   r   r   r   7   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|Ž d S r   ©r*   ÚsuperÚ__init__©r   ZapplicationÚrequestr*   Úkwargs©Ú	__class__r   r   r-   F   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)r6   Úws_uriÚfig_idÚ	toolitemsÚcanvas)Úintr   Úget_fig_managerÚformatr/   r*   Úrenderr   r   r9   r:   )r   ÚfignumÚmanagerr7   r   r   r   r%   J   s    
ÿúz&WebAggApplication.SingleFigurePage.get©r   r   r   r-   r%   Ú__classcell__r   r   r1   r   ÚSingleFigurePageE   s   rC   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|Ž d S r   r+   r.   r1   r   r   r-   Y   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 )Nr3   r4   zall_figures.html)r6   r7   Zfiguresr9   )r=   r/   r*   r>   Úsortedr   ZfigsÚitemsr   r   r9   )r   r7   r   r   r   r%   ]   s    ÿûz$WebAggApplication.AllFiguresPage.getrA   r   r   r1   r   ÚAllFiguresPageX   s   rF   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   r   Zget_javascriptr!   )r   Z
js_contentr   r   r   r%   h   s    
zWebAggApplication.MplJs.getNr&   r   r   r   r   ÚMplJsg   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=   )r;   r   r<   r    Ú	mimetypesÚ	types_mapr%   r   r:   ÚfigureZsavefigr!   Úgetvalue)r   r?   Úfmtr@   Zbuffr   r   r   r%   p   s    
 ÿzWebAggApplication.Download.getNr&   r   r   r   r   ÚDownloado   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)r;   r?   r   r<   r@   Zadd_web_socketÚhasattrrO   )r   r?   r   r   r   Úopen|   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 k	r@| |¡ d S )NÚtypeÚsupports_binaryÚvalue)ÚjsonÚloadsrT   r   r<   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   Ú	WebSockety   s   rc   r(   c              
      sÊ   |r$|d dkr|d dks$t 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)ÚAssertionErrorr,   r-   r   ÚwebZStaticFileHandlerr   r   Úget_static_file_pathr   r"   r#   r'   rC   rF   rG   rc   rN   )r   r*   r1   r   r   r-       s8    ÿþþ
ÿ
ÿÿã ßzWebAggApplication.__init__Nc                 C   sÄ   | j r
d S | |d�}|| _dd„ }|d kr8tjd | _n|| _tjd | _|| jtjd ƒD ]T}z| || j¡ W n2 tjk
r¤ } z|j	t	j
kr”‚ W 5 d }~X Y q\X || _ 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Úaddressrq   ÚlistenÚsocketÚerrorÚerrnoZ
EADDRINUSEÚ
SystemExit)Úclsr*   rq   rw   Zapprt   Úer   r   r   Ú
initializeÈ   s.    
ÿÿzWebAggApplication.initializec              	      sl   ˆ j r
d S tjj ¡ ‰‡ ‡fdd„‰t‡‡fdd„ƒ}dˆ _ tdƒ tj 	¡  |ƒ � ˆ 
¡  W 5 Q R X 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   s8   t   t j‡ ‡fdd„¡} z
d V  W 5 t   t j| ¡ X 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   ru   r…   r   rh   ZRequestHandlerr'   rC   rF   rG   rN   Z	websocketZWebSocketHandlerrc   r-   Úclassmethodr   r   rB   r   r   r1   r   r   ;   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)r6   r8   r9   r:   rq   zutf-8)Ztornado.templater   r   Úwebagg_server_threadÚis_aliver   Únumberr   r   r   ri   Ú	read_textÚtemplateÚTemplateÚgenerater*   r   r9   r:   rq   Údecode)rK   r   r?   ZtplÚtr   r   r   Úipython_inline_display  s$    
ÿûûr™   c                   @   s&   e Zd ZeZeZeddœdd„ƒZdS )Ú_BackendWebAggN)Úblockc                 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})rw   rq   r6   zwebagg.open_in_browserr   zTo view figure, visit {0})r   r   r=   rw   rq   r*   r"   rv   Ú
webbrowserrQ   r�   r   )r›   Úurlrœ   r   r   r   Úshow5  s    ý

z_BackendWebAgg.show)	r   r   r   r   ZFigureCanvasr   ZFigureManagerÚstaticmethodrž   r   r   r   r   rš   0  s   rš   )-Ú__doc__Ú
contextlibr   r{   Úior   rV   rI   Úpathlibr   ro   r‚   rŒ   ry   Ú	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�   r   ZFigureCanvasWebAggCorer   rh   ÚApplicationr   r™   Zexportrš   r   r   r   r   Ú<module>   s@    c