
    G8c-                     D   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 ZddddddddZ G d de      Z G d de      Z G d de      Z G d d      Zej@                   G d de             Z!y)z,Interactive figures in the IPython notebook.    )	b64encodeN)Comm)display
JavascriptHTML)is_interactiveGcf)_BackendNavigationToolbar2   )FigureCanvasWebAggCoreFigureManagerWebAggNavigationToolbar2WebAgg)TimerTornadoTimerAsyncioc                     t        j                         D  cg c]^  } dj                  | j                  j                  j                         xs dj                  | j                        | j                        ` }} t               s+|j                  dt        t         j                                dj                  |      S c c} w )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.
    z{fig} - {socket}z	Figure {})figsocketzFigures pending show: 
)r
   get_all_fig_managersformatcanvasfigure	get_labelnumweb_socketsr   appendlenfigsjoin)managerresults     C/usr/lib/python3/dist-packages/matplotlib/backends/backend_nbagg.pyconnection_infor%      s     //1
 	 	!!&&002 4&&w{{3&& 	" 	(F  .s388}o>?99Vs   A#C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forwardzoom_to_rectmovedownloadNc                       e Zd Zej                  dz   D  cg c]  \  }}}}|t
        v r||t
        |   |f c}}}}}} Zyc c}}}}}} w )NavigationIPy))DownloadzDownload plotr+   r+   N)__name__
__module____qualname__r   	toolitems_FONT_AWESOME_CLASSES).0texttooltip_text
image_filename_of_methodr3   s   000000r$   r-   r-   5   sZ     (11MN9 9CT<^ "77 '
3^E 9I 9s   !Ar-   c                        e Zd ZexZZ fdZed        Zd Z	d Z
d Zed        Zed fd	       Zd Zd	 Zd
 Zd Z xZS )FigureManagerNbAggc                 4    d| _         t        | 	  ||       y )NF)_shownsuper__init__)selfr   r   	__class__s      r$   r>   zFigureManagerNbAgg.__init__C   s    %    c                      ||       | |      t               r j                          j                          fd}j                  d|      S )Nc                 R    j                         t        j                         y N)mpl_disconnectr
   destroy)eventr   cidr"   s    r$   rF   z6FigureManagerNbAgg.create_with_canvas.<locals>.destroyO   s    !!#&KK rA   close_event)r   show	draw_idlempl_connect)clscanvas_classr   r   rF   r   rH   r"   s        @@@r$   create_with_canvasz%FigureManagerNbAgg.create_with_canvasG   sR    f%fc"LLN	!   8rA   c                 P    t        t        t        j                                      y rD   )r   r   r:   get_javascriptr?   s    r$   
display_jszFigureManagerNbAgg.display_jsV   s     	
-<<>?@rA   c                 p   | j                   s!| j                          | j                          n| j                  j	                          d| _         t        | d      r%| j                  j                  | j                         t               s-ddl	m
} |j                  j                  | j                  d        y y )NT_cidgcfr   r	   )r<   rS   _create_commr   rK   hasattrrE   rU   r   matplotlib._pylab_helpersr
   r    popr   )r?   r
   s     r$   rJ   zFigureManagerNbAgg.show\   s|    {{OOKK!!#
 4#KK&&t||45HHLL4(  rA   c                 2    d| _         | j                          y)zJ
        A special method to re-show the figure in the notebook.

        FN)r<   rJ   rR   s    r$   reshowzFigureManagerNbAgg.reshowm   s    
 		rA   c                 ,    t        | j                        S rD   )boolr   rR   s    r$   	connectedzFigureManagerNbAgg.connectedu   s    D$$%%rA   c                    |t        j                         }n|}t        |   |       |j	                  t        j                  t              j                  dz  j                  d             ||j                         S y )N)streamzweb_backend/js/nbagg_mpl.jszutf-8)encoding)ioStringIOr=   rQ   writepathlibPath__file__parent	read_textgetvalue)rM   r`   outputr@   s      r$   rQ   z!FigureManagerNbAgg.get_javascripty   sr    >[[]FFf-gll8,3356ii1	3 >??$$ rA   c                 >    t        |       }| j                  |       |S rD   )
CommSocketadd_web_socketr?   comms     r$   rV   zFigureManagerNbAgg._create_comm   s    $D!rA   c                     | j                  d       t        | j                        D ]  }|j                           | j	                          y )Nclose)_send_eventlistr   on_closeclearup_closedro   s     r$   rF   zFigureManagerNbAgg.destroy   s?    !))* 	DMMO	rA   c                     | j                   D ch c]  }|j                         r| c}| _         t        | j                         dk(  r| j                  j	                          yyc c}w )zClear up any closed Comms.r   N)r   is_openr   r   rI   )r?   r   s     r$   rv   z!FigureManagerNbAgg.clearup_closed   s[    151A1A 1v%~~/ # 1 t A%KK##% &1s   A$c                 ~    | j                   D ch c]  }|j                  j                  |k7  r| c}| _         y c c}w rD   )r   rp   comm_id)r?   rz   r   s      r$   remove_commzFigureManagerNbAgg.remove_comm   s9    151A1A ?v%{{22g= # ? ?s   ":rD   )r/   r0   r1   r-   _toolbar2_class
ToolbarClsr>   classmethodrO   rS   rJ   r[   propertyr^   rQ   rV   rF   rv   r{   __classcell__)r@   s   @r$   r:   r:   @   sr    #00Oj&  A)" & & 
% 
%
&?rA   r:   c                       e Zd ZeZy)FigureCanvasNbAggN)r/   r0   r1   r:   manager_class rA   r$   r   r      s    &MrA   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	rm   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                     d  _          _        t        t        j                                _        t        t        d j                  z               	 t        dd j                  i       _         j                  j                   j                          j                  d _         fd} j                  j                  |       y # t        $ r}t        d      |d }~ww xY w)Nz<div id=%r></div>
matplotlibid)datazTUnable to create an IPython notebook Comm instance. Are you in the IPython notebook?Fc                 b    d_         j                  | d   d          j                          y )NTcontentrz   )
_ext_closer{   rv   )close_messager"   r?   s    r$   	_on_closez&CommSocket.__init__.<locals>._on_close   s.    "DOi 8 CD""$rA   )supports_binaryr"   struuiduuid4r   r   r   rp   AttributeErrorRuntimeErroron_msg
on_messager   ru   )r?   r"   errr   s   ``  r$   r>   zCommSocket.__init__   s    #

%	 	(499456	5\tyy0ABDI
 			),,	%
 			9%  	5  + ,145	5s   C 	C'C""C'c                 L    | j                   xs | j                  j                   S rD   )r   rp   _closedrR   s    r$   rx   zCommSocket.is_open   s    OO8tyy'8'899rA   c                 z    | j                         r	 | j                  j                          y y # t        $ r Y y w xY wrD   )rx   rp   rr   KeyErrorrR   s    r$   ru   zCommSocket.on_close   s9     <<>		!   s   . 	::c                 d    | j                   j                  dt        j                  |      i       y )Nr   )rp   sendjsondumps)r?   r   s     r$   	send_jsonzCommSocket.send_json   s     		

7 345rA   c                     | j                   r!| j                  j                  ddi|g       y t        |      j	                  d      }dj                  |      }| j                  j                  d|i       y )Nblobz	image/png)buffersasciizdata:image/png;base64,{0}r   )r   rp   r   r   decoder   )r?   r   r   data_uris       r$   send_binaryzCommSocket.send_binary   sa    IINNFK04&NA T?))'2D299$?HIINNFH-.rA   c                     t        j                  |d   d         }|d   dk(  r+| j                          | j                  j	                          y |d   dk(  r|d   | _        y | j                  j                  |       y )Nr   r   typeclosingr   value)r   loadsru   r"   rv   r   handle_json)r?   messages     r$   r   zCommSocket.on_message   sm     **WY/786?i'MMOLL'')V_ 11#*7#3D LL$$W-rA   N)
r/   r0   r1   __doc__r>   rx   ru   r   r   r   r   rA   r$   rm   rm      s%    &2:6/.rA   rm   c                       e Zd ZeZeZy)_BackendNbAggN)r/   r0   r1   r   FigureCanvasr:   FigureManagerr   rA   r$   r   r      s    $L&MrA   r   )"r   base64r   rb   r   re   r   ipykernel.commr   IPython.displayr   r   r   r   r   rX   r
   matplotlib.backend_basesr   r   backend_webagg_corer   r   r   r   r   r%   r3   r-   r:   r   rm   exportr   r   rA   r$   <module>r      s    2
  	     5 5 % ) AK K & "$ 
 9, 9\?, \?~'. 'J. J.Z 
'H ' 'rA   