3
$ßL]·+  ã               @   s>  d Z ddlmZ ddl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ZddlZddlZyddlZW n ek
rˆ   edƒ‚Y nX 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 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.)ÚrcParams)Ú_Backend)ÚGcfé   )Úbackend_webagg_core)ÚTimerTornadoc               @   s   e Zd Zdd„ ZdS )ÚServerThreadc             C   s   t jjjƒ jƒ  d S )N)ÚtornadoÚioloopÚIOLoopÚinstanceÚstart)Úself© r   úJ/tmp/pip-install-edjban26/matplotlib/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d„ Zdd„ ZdS )ÚFigureCanvasWebAggc             C   s
   t ƒ  d S )N)Úshow)r   r   r   r   r   2   s    zFigureCanvasWebAgg.showc             O   s
   t ||ŽS )N)r
   )r   ÚargsÚkwargsr   r   r   Ú	new_timer7   s    zFigureCanvasWebAgg.new_timerN)r   r   r   r   r   r   r   r   r   r   1   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.   | j ddƒ ttd ddƒ}| j|jƒ ƒ d S )NzContent-Typez	image/pngZdatapathZimageszmatplotlib.png)Ú
set_headerr   r   ÚwriteÚ
read_bytes)r   Z
image_pathr   r   r   ÚgetA   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$   r   )Ú	__class__r   r   r&   G   s    z+WebAggApplication.SingleFigurePage.__init__c             C   sF   t |ƒ}tj|ƒ}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!   K   s    

z&WebAggApplication.SingleFigurePage.get)r   r   r   r&   r!   Ú__classcell__r   r   )r)   r   ÚSingleFigurePageF   s   r9   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&   Z   s    z)WebAggApplication.AllFiguresPage.__init__c             C   s:   dj | j| jd�}| jd| j|ttjjƒ ƒtj	j
d� d S )Nzws://{req.host}{prefix}/)r*   r+   zall_figures.html)r+   r,   Zfiguresr.   )r2   r(   r$   r3   Úsortedr   ZfigsÚitemsr4   r5   r.   )r   r,   r   r   r   r!   ^   s    
z$WebAggApplication.AllFiguresPage.get)r   r   r   r&   r!   r8   r   r   )r)   r   ÚAllFiguresPageY   s   r<   c               @   s   e Zd Zdd„ ZdS )zWebAggApplication.MplJsc             C   s$   | j ddƒ tjjƒ }| j|ƒ d S )NzContent-Typezapplication/javascript)r   r4   ÚFigureManagerWebAggZget_javascriptr   )r   Z
js_contentr   r   r   r!   i   s    
zWebAggApplication.MplJs.getN)r   r   r   r!   r   r   r   r   ÚMplJsh   s   r>   c               @   s   e Zd Zdd„ ZdS )zWebAggApplication.Downloadc          	   C   sf   t |ƒ}tj|ƒ}dddddddddœ}| jd	|j|d
ƒƒ tƒ }|jjj||d� | j	|j
ƒ ƒ d S )Nzapplication/postscriptzapplication/pdfzimage/svg+xmlz	image/pngz
image/jpegz
image/tiffzapplication/emf)ZpsZepsZpdfÚsvgZpngZjpegZtifZemfzContent-TypeÚbinary)r2   )r0   r   r1   r   r!   r   r/   ÚfigureZsavefigr   Úgetvalue)r   r6   Úfmtr7   Ú	mimetypesZbuffr   r   r   r!   q   s    
zWebAggApplication.Download.getN)r   r   r   r!   r   r   r   r   ÚDownloadp   s   rE   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ƒ| _| jj| ƒ t| dƒr8| jdƒ d S )NÚset_nodelayT)r0   r6   r   r1   r7   Zadd_web_socketÚhasattrrF   )r   r6   r   r   r   ÚopenŠ   s
    

z WebAggApplication.WebSocket.openc             C   s   | j j| ƒ d S )N)r7   Zremove_web_socket)r   r   r   r   Úon_close‘   s    z$WebAggApplication.WebSocket.on_closec             C   sD   t j|ƒ}|d dkr"|d | _ntj| jƒ}|d k	r@|j|ƒ d S )NÚtypeÚsupports_binaryÚvalue)ÚjsonÚloadsrK   r   r1   r6   Zhandle_json)r   Úmessager7   r   r   r   Ú
on_message”   s    
z&WebAggApplication.WebSocket.on_messagec             C   s   | j tj|ƒƒ d S )N)Úwrite_messagerM   Údumps)r   Úcontentr   r   r   Ú	send_json£   s    z%WebAggApplication.WebSocket.send_jsonc             C   s<   | j r| j|dd� n"dj|jdƒjddƒƒ}| j|ƒ d S )NT)r@   zdata:image/png;base64,{0}Úbase64Ú
r#   )rK   rQ   r2   ÚencodeÚreplace)r   ZblobZdata_urir   r   r   Úsend_binary¦   s
    z'WebAggApplication.WebSocket.send_binaryN)	r   r   r   rK   rH   rI   rP   rT   rY   r   r   r   r   Ú	WebSocket‡   s   rZ   r#   c          	      s¬   |r$|d dkr|d dks$t dƒ‚tƒ j|d tjjdtjjƒ 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jƒ d� d S )Nr   ú/r   z6url_prefix must start with a "/" and not end with one.z/_static/(.*)Úpathz/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StaticFileHandlerr4   r=   Úget_static_file_pathr"   r9   r<   r>   rZ   rE   )r   r$   )r)   r   r   r&   ®   s"    

zWebAggApplication.__init__Nc             C   sÀ   | j r
d S | |d�}|| _dd„ }|d kr6td | _n|| _td | _xn|| jtd ƒD ]R}y|j|| jƒ W n2 tjk
r  } z|jtj	kr�‚ W Y d d }~X qXX || _P qXW t
dƒ‚d| _ d S )	N)r$   c             s   sV   x t td|ƒƒD ]}| | V  qW x.t |d ƒD ]}| tj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   Úaddressrh   ÚlistenÚsocketÚerrorÚerrnoZ
EADDRINUSEÚ
SystemExit)Úclsr$   rh   rm   Zapprk   Úer   r   r   Ú
initializeÑ   s(    

zWebAggApplication.initializec          	      sl   ˆ j r
d S tjjjƒ ‰‡ ‡fdd„‰t‡‡fdd„ƒ}dˆ _ tdƒ tjj	ƒ  |ƒ � ˆj
ƒ  W d Q R X d S )Nc                  s$   ˆj ƒ  tdƒ tj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 j t j‡ ‡fdd„ƒ} z
d V  W d t j t j| ƒ X d S )Nc                s
   ˆ j ˆƒ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   rl   r{   r   r_   ZRequestHandlerr"   r9   r<   r>   rE   Z	websocketZWebSocketHandlerrZ   r&   Úclassmethodru   r   r8   r   r   )r)   r   r   <   s   '#,r   c             C   sj   dd l }tjƒ  tjƒ s tjƒ  | j}ttj	j
ƒ dƒjƒ }|jj|ƒ}|jtj|tjj| jtjd�jdƒS )Nr   zipython_inline_figure.html)r+   r-   r.   r/   rh   zutf-8)Ztornado.templater   ru   Úwebagg_server_threadÚis_aliver   Únumberr   r4   r=   r`   Z	read_textÚtemplateÚTemplateÚgenerater$   r5   r.   r/   rh   Údecode)rA   r   r6   Ú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 jƒ  d S )N)r/   Z	draw_idle)r7   r   r   r   Útrigger_manager_draw=  s    z#_BackendWebAgg.trigger_manager_drawc              C   sT   t jƒ  djt jt jt jd�} td r:dd l}|j| ƒ nt	dj| ƒƒ t j
ƒ  d S )Nzhttp://{address}:{port}{prefix})rm   rh   r+   zwebagg.open_in_browserr   zTo view figure, visit {0})r   ru   r2   rm   rh   r$   r   Ú
webbrowserrH   rw   r   )Úurlr�   r   r   r   r   A  s    
z_BackendWebAgg.showN)r   r   r   r   ZFigureCanvasr4   r=   ZFigureManagerÚstaticmethodr�   r   r   r   r   r   rŽ   8  s   rŽ   ))Ú__doc__Ú
contextlibr   rq   Úior   rM   Zpathlibr   rf   rx   r€   ro   Ú	threadingr   ÚImportErrorÚRuntimeErrorZtornado.webZtornado.ioloopZtornado.websocketZ
matplotlibr   Zmatplotlib.backend_basesr   Zmatplotlib._pylab_helpersr   r#   r	   r4   r
   ÚThreadr   r„   ZFigureCanvasWebAggCorer   r_   ÚApplicationr   r�   ÚexportrŽ   r   r   r   r   Ú<module>   s<    j