B
    Mô`/+  ã            
   @   sR  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yddlZW n, ek
r  Z zedƒe‚W d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 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)Ú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   úF/tmp/pip-unpacked-wheel-0c1wfn4v/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Zdd„ ZdS )ÚFigureCanvasWebAggc             C   s
   t ƒ  d S )N)Úshow)r   r   r   r   r   5   s    zFigureCanvasWebAgg.showN)r   r   r   r	   Z
_timer_clsr   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 )NzContent-Typez	image/pngzimages/matplotlib.png)Ú
set_headerÚwriter   ÚmplÚget_data_pathÚ
read_bytes)r   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 )N)r"   ÚsuperÚ__init__)r   Ú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 )Nzws://{req.host}{prefix}/)ÚreqÚprefixzsingle_figure.html)r*   Úws_uriÚfig_idÚ	toolitemsÚcanvas)Úintr   Úget_fig_managerÚformatr&   r"   ÚrenderÚcoreÚNavigationToolbar2WebAggr-   r.   )r   ÚfignumÚmanagerr+   r   r   r   r   J   s    

z&WebAggApplication.SingleFigurePage.get)r   r   r   r$   r   Ú__classcell__r   r   )r(   r   ÚSingleFigurePageE   s   r8   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 )N)r"   r#   r$   )r   r%   r&   r"   r'   )r(   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 )Nzws://{req.host}{prefix}/)r)   r*   zall_figures.html)r*   r+   Zfiguresr-   )r1   r&   r"   r2   Úsortedr   ZfigsÚitemsr3   r4   r-   )r   r+   r   r   r   r   ]   s    
z$WebAggApplication.AllFiguresPage.get)r   r   r   r$   r   r7   r   r   )r(   r   ÚAllFiguresPageX   s   r;   c               @   s   e Zd Zdd„ ZdS )zWebAggApplication.MplJsc             C   s$   |   dd¡ tj ¡ }|  |¡ d S )NzContent-Typezapplication/javascript)r   r3   ÚFigureManagerWebAggZget_javascriptr   )r   Z
js_contentr   r   r   r   h   s    
zWebAggApplication.MplJs.getN)r   r   r   r   r   r   r   r   ÚMplJsg   s   r=   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 )NzContent-TypeÚbinary)r1   )r/   r   r0   r   Ú	mimetypesÚ	types_mapr   r   r.   ÚfigureZsavefigr   Úgetvalue)r   r5   Úfmtr6   Zbuffr   r   r   r   p   s    
zWebAggApplication.Download.getN)r   r   r   r   r   r   r   r   ÚDownloado   s   rD   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/   r5   r   r0   r6   Zadd_web_socketÚhasattrrE   )r   r5   r   r   r   Úopen|   s
    

z WebAggApplication.WebSocket.openc             C   s   | j  | ¡ d S )N)r6   Zremove_web_socket)r   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ÚloadsrJ   r   r0   r5   Zhandle_json)r   Úmessager6   r   r   r   Ú
on_message†   s    
z&WebAggApplication.WebSocket.on_messagec             C   s   |   t |¡¡ d S )N)Úwrite_messagerL   Ú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)r>   zdata:image/png;base64,{0}Úbase64Ú
r!   )rJ   rP   r1   ÚencodeÚreplace)r   ZblobZdata_urir   r   r   Úsend_binary˜   s
    z'WebAggApplication.WebSocket.send_binaryN)	r   r   r   rJ   rG   rH   rO   rS   rX   r   r   r   r   Ú	WebSockety   s   rY   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StaticFileHandlerr3   r<   Úget_static_file_pathr   r   r   r    r8   r;   r=   rY   rD   )r   r"   )r(   r   r   r$       s(    

zWebAggApplication.__init__Nc             C   sÆ   | j r
d S | |d�}|| _dd„ }|d kr8tjd | _n|| _tjd | _xp|| jtjd ƒD ]R}y| || j¡ W n2 tjk
r¦ } z|j	t	j
kr–‚ W d d }~X Y q^X || _P q^W tdƒ‚d| _ d S )	N)r"   c             s   sV   x t td|ƒƒD ]}| | V  qW x.t |d ƒD ]}| t d| d| ¡ V  q0W 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Úaddressrg   ÚlistenÚsocketÚerrorÚerrnoZ
EADDRINUSEÚ
SystemExit)Úclsr"   rg   rm   Zapprj   Ú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 d Q R X d S )Nc                  s$   ˆ  ¡  tdƒ tj ¡  dˆ _d S )NzServer is stoppedF)ÚstopÚprintÚsysÚstdoutÚflushÚstartedr   )rs   r   r   r   Úshutdown  s    
z)WebAggApplication.start.<locals>.shutdownc           
   3   s8   t   t j‡ ‡fdd„¡} z
d V  W d t   t j| ¡ X d S )Nc                s
   ˆ   ˆ¡S )N)Zadd_callback_from_signal)ÚsigÚframe)r   r|   r   r   Ú<lambda>  s    z?WebAggApplication.start.<locals>.catch_sigint.<locals>.<lambda>)ÚsignalÚSIGINT)Zold_handler)r   r|   r   r   Úcatch_sigint
  s    
z-WebAggApplication.start.<locals>.catch_sigintTz"Press Ctrl+C to stop WebAgg server)r{   r   r   r   r   r   rw   rx   ry   rz   r   )rs   r‚   r   )rs   r   r|   r   r   ö   s    	
zWebAggApplication.start)r!   )r!   NN)r   r   r   rk   r{   r   r^   ZRequestHandlerr    r8   r;   r=   rD   Z	websocketZWebSocketHandlerrY   r$   Úclassmethodru   r   r7   r   r   )r(   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)r*   r,   r-   r.   rg   zutf-8)Ztornado.templater   ru   Úwebagg_server_threadÚis_aliver   Únumberr   r3   r<   r_   Ú	read_textÚtemplateÚTemplateÚgenerater"   r4   r-   r.   rg   Údecode)rA   r   r5   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	edd„ ƒZ
dS )Ú_BackendWebAggc             C   s   | j  ¡  d S )N)r.   Z	draw_idle)r6   r   r   r   Útrigger_manager_draw5  s    z#_BackendWebAgg.trigger_manager_drawc              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})rm   rg   r*   zwebagg.open_in_browserr   zTo view figure, visit {0})r   ru   r1   rm   rg   r"   r   rl   Ú
webbrowserrG   rw   r   )Úurlr�   r   r   r   r   9  s    


z_BackendWebAgg.showN)r   r   r   r   ZFigureCanvasr3   r<   ZFigureManagerÚstaticmethodr�   r   r   r   r   r   rŽ   0  s   rŽ   )+Ú__doc__Ú
contextlibr   rq   Úior   rL   r?   Úpathlibr   re   rx   r€   ro   Ú	threadingr   ÚImportErrorÚerrÚRuntimeErrorZtornado.webZtornado.ioloopZtornado.websocketZ
matplotlibr   Zmatplotlib.backend_basesr   Zmatplotlib._pylab_helpersr   r!   r   r3   r	   ÚThreadr
   r„   ZFigureCanvasWebAggCorer   r^   ÚApplicationr   r�   ÚexportrŽ   r   r   r   r   Ú<module>   s>   	 c