
    h+                        S r SSKJr  SSKrSSKJr  SSKrSSKrSSKJ	r	  SSK
r
SSKrSSKrSSKr SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJr  S	S
KJr  S	SKJrJr  \R@                  " S S9r! " S S\RD                  5      r" " S S\RF                  5      r$ " S S\RJ                  RL                  5      r'S r(\RR                   " S S\5      5       r*g! \ a  r\" S5      \eSrCff = f)z7Displays Agg images in the browser, with interactivity.    )contextmanagerN)BytesIO)Pathz$The WebAgg backend requires Tornado.)_Backend)Gcf   )backend_webagg_core)TimerAsyncioTimerTornadoc                  n    [         R                  R                  R                  5       R	                  5       $ N)tornadoioloopIOLoopinstancestart     T/var/www/html/env/lib/python3.13/site-packages/matplotlib/backends/backend_webagg.py<lambda>r   )   s     7>>((11399;r   )targetc                   D    \ rS rSr\R
                  r\SS.S j5       rSr	g)FigureManagerWebAgg,   N)blockc                h   [         R                  5         SR                  [         R                  [         R                  [         R
                  S9n[        R                  S   (       a)  SS KnUR                  U5      (       d  [        SU 35        O[        SU 35        [         R                  5         g )Nzhttp://{address}:{port}{prefix})addressportprefixzwebagg.open_in_browserr   zTo view figure, visit )WebAggApplication
initializeformatr   r   
url_prefixmplrcParams
webbrowseropenprintr   )clsr   urlr&   s       r   pyplot_showFigureManagerWebAgg.pyplot_show/   s    $$&/66%--"''$// 7 1
 <<01??3''.se45*3%01!r   r   )
__name__
__module____qualname____firstlineno__coreNavigationToolbar2WebAgg_toolbar2_classclassmethodr+   __static_attributes__r   r   r   r   r   ,   s"    33O"& " "r   r   c                       \ rS rSr\rSrg)FigureCanvasWebAggB   r   N)r-   r.   r/   r0   r   manager_classr5   r   r   r   r7   r7   B   s    'Mr   r7   c                     ^  \ rS rSrSrSr " S S\R                  R                  5      r	 " S S\R                  R                  5      r
 " S S\R                  R                  5      r " S	 S
\R                  R                  5      r " S S\R                  R                  5      r " S S\R                  R                  5      rSU 4S jjr\SS j5       r\S 5       rSrU =r$ )r    F   Fc                       \ rS rSrS rSrg)WebAggApplication.FavIconJ   c                     U R                  SS5        U R                  [        [        R                  " 5       S5      R                  5       5        g )NContent-Typez	image/pngzimages/matplotlib.png)
set_headerwriter   r$   get_data_path
read_bytesselfs    r   getWebAggApplication.FavIcon.getK   s9    OONK8JJtC--/355?Z\Cr   r   Nr-   r.   r/   r0   rG   r5   r   r   r   FavIconr=   J   s    	Cr   rJ   c                   6   ^  \ rS rSrSS.U 4S jjrS rSrU =r$ )"WebAggApplication.SingleFigurePageP    r#   c                4   > X0l         [        TU ]  " X40 UD6  g r   r#   super__init__rF   applicationrequestr#   kwargs	__class__s        r   rS   +WebAggApplication.SingleFigurePage.__init__Q       (OG[<V<r   c           	         [        U5      n[        R                  " U5      nSU R                  R                   U R
                   S3nU R                  SU R
                  UU[        R                  R                  UR                  S9  g )Nws:///zsingle_figure.html)r   ws_urifig_id	toolitemscanvas)intr   get_fig_managerrV   hostr#   renderr1   r2   r`   ra   )rF   fignummanagerr^   s       r   rG   &WebAggApplication.SingleFigurePage.getU   st    [F))&1GT\\..//@BFKK$77AA~~  'r   r-   r.   r/   r0   rS   rG   r5   __classcell__rX   s   @r   SingleFigurePagerL   P   s    ?A 	= 	=	' 	'r   rl   c                   6   ^  \ rS rSrSS.U 4S jjrS rSrU =r$ ) WebAggApplication.AllFiguresPageb   rN   rO   c                4   > X0l         [        TU ]  " X40 UD6  g r   rQ   rT   s        r   rS   )WebAggApplication.AllFiguresPage.__init__c   rZ   r   c           	         SU R                   R                   U R                   S3nU R                  SU R                  U[	        [
        R                  R                  5       5      [        R                  R                  S9  g )Nr\   r]   zall_figures.html)r   r^   figuresr`   )rV   rd   r#   re   sortedr   figsitemsr1   r2   r`   )rF   r^   s     r   rG   $WebAggApplication.AllFiguresPage.getg   se    T\\..//@BFKK"sxx~~/077AA  Cr   ri   rk   s   @r   AllFiguresPagern   b   s    ?A 	= 	=	C 	Cr   rx   c                       \ rS rSrS rSrg)WebAggApplication.MplJsp   c                     U R                  SS5        [        R                  R                  5       nU R	                  U5        g )Nr@   zapplication/javascript)rA   r1   r   get_javascriptrB   )rF   
js_contents     r   rG   WebAggApplication.MplJs.getq   s1    OON,DE11@@BJJJz"r   r   NrI   r   r   r   MplJsrz   p   s    	#r   r   c                       \ rS rSrS rSrg)WebAggApplication.Downloadx   c                 >   [        U5      n[        R                  " U5      nU R                  S[        R
                  R                  US5      5        [        5       nUR                  R                  R                  XBS9  U R                  UR                  5       5        g )Nr@   binary)r"   )rb   r   rc   rA   	mimetypes	types_maprG   r   ra   figuresavefigrB   getvalue)rF   rf   fmtrg   buffs        r   rG   WebAggApplication.Download.gety   st    [F))&1GOO	 3 3 7 7X FH9DNN!!))$);JJt}}'r   r   NrI   r   r   r   Downloadr   x   s    	(r   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
WebAggApplication.WebSocket   Tc                     [        U5      U l        [        R                  " U R                  5      U l        U R                  R                  U 5        [        U S5      (       a  U R                  S5        g g )Nset_nodelayT)rb   rf   r   rc   rg   add_web_sockethasattrr   )rF   rf   s     r   r'    WebAggApplication.WebSocket.open   sU    f+DK..t{{;DLLL''-t]++  & ,r   c                 :    U R                   R                  U 5        g r   )rg   remove_web_socketrE   s    r   on_close$WebAggApplication.WebSocket.on_close   s    LL**40r   c                     [         R                  " U5      nUS   S:X  a  US   U l        g [        R                  " U R
                  5      nUb  UR                  U5        g g )Ntypesupports_binaryvalue)jsonloadsr   r   rc   rf   handle_json)rF   messagerg   s      r   
on_message&WebAggApplication.WebSocket.on_message   s[    jj)G v"33'.w'7$--dkk: &''0 'r   c                 N    U R                  [        R                  " U5      5        g r   )write_messager   dumps)rF   contents     r   	send_json%WebAggApplication.WebSocket.send_json   s    tzz'23r   c                     U R                   (       a  U R                  USS9  g SR                  UR                  S5      R	                  SS5      5      nU R                  U5        g )NT)r   zdata:image/png;base64,{}base64
rN   )r   r   r"   encodereplace)rF   blobdata_uris      r   send_binary'WebAggApplication.WebSocket.send_binary   sU    ##""4"55<<KK)11$;=""8,r   )rf   rg   r   N)r-   r.   r/   r0   r   r'   r   r   r   r   r5   r   r   r   	WebSocketr      s     	'	1	1	4	-r   r   c                 \  > U(       a  US   S:X  a	  US   S:w  d   S5       e[         TU ]  US-   [        R                  R                  S[
        R                  R                  5       04US-   [        R                  R                  S[        [        R                  " 5       S5      04US	-   U R                  4US
-   U R                  SU04US-   U R                  SU04US-   U R                  4US-   U R                  4US-   U R                   4/[
        R                  R                  5       S9  g )Nr   r]   z6url_prefix must start with a "/" and not end with one.z/_static/(.*)pathz/_images/(.*)imagesz/favicon.icoz	/([0-9]+)r#   z/?z
/js/mpl.jsz/([0-9]+)/wsz/([0-9]+)/download.([a-z0-9.]+))template_path)rR   rS   r   webStaticFileHandlerr1   r   get_static_file_pathr   r$   rC   rJ   rl   rx   r   r   r   )rF   r#   rX   s     r   rS   WebAggApplication.__init__   sX   a=C'JrNc,A IHIA 	 ....$22GGIJL
 ....$s002H=>@
 o-t||< l*D,A,A
+- e#T%8%8
+- m+TZZ8 o-t~~> @@ ;@ 22GGIC 	 !	Kr   c                    U R                   (       a  g U " US9nXl        S nUc  [        R                  S   U l        OX0l        [        R                  S   U l        U" U R
                  [        R                  S   5       H%  n UR                  X R                  5        X l          O   [        S5      eSU l         g ! [         a*  nUR                  [        R                  :w  a  e  S nAMk  S nAff = f)NrO   c              3      #    [        [        SU5      5       H	  nX-   v   M     [        US-
  5       H%  nU [        R                  " SU-  SU-  5      -   v   M'     g7f)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)r   nis      r   random_ports2WebAggApplication.initialize.<locals>.random_ports   sS      3q!9%h &1q5\V^^BFAE::: "s   AAzwebagg.addresszwebagg.portzwebagg.port_retrieszSThe webagg server could not be started because an available port could not be foundT)initializedr#   r$   r%   r   r   listenOSErrorerrno
EADDRINUSE
SystemExit)r)   r#   r   r   appr   es          r   r!   WebAggApplication.initialize   s    ?? Z(#
	; ?,,'78CK!K<<. !$.C!DFD

4-
  F *+ +   77e... /s   B<<
C0C++C0c                   ^ ^^ SS K n UR                  5         ST l        T R                  (       a  g  [        R
                  R                  R                  5       mU U4S jm[        UU4S j5       nST l        [        S5        [        R                  R                  5         U" 5          TR                  5         S S S 5        g ! [         a     Nf = f! , (       d  f       g = f)Nr   Tc                     > TR                  5         [        S5        [        R                  R	                  5         ST l        g )NzServer is stoppedF)stopr(   sysstdoutflushstarted)r)   r   s   r   shutdown)WebAggApplication.start.<locals>.shutdown  s,    KKM%&JJCKr   c               3   
  >#    [         R                   " [         R                  UU4S j5      n  S v   [         R                   " [         R                  U 5        g ! [         R                   " [         R                  U 5        f = f7f)Nc                 &   > TR                  T5      $ r   )add_callback_from_signal)sigframer   r   s     r   r   ?WebAggApplication.start.<locals>.catch_sigint.<locals>.<lambda>  s    6#B#B8#Lr   )signalSIGINT)old_handlerr   r   s    r   catch_sigint-WebAggApplication.start.<locals>.catch_sigint  sK      --LNK:fmm[9fmm[9s   +BA &B'B  Bz"Press Ctrl+C to stop WebAgg server)asyncioget_running_loopr   RuntimeErrorr   r   r   r   r   r(   r   r   r   r   )r)   r   r   r   r   s   `  @@r   r   WebAggApplication.start   s    	$$& CK;;	 &&//1	  
	: 
	: 23

^LLN ^M  		L ^s   C )C
CC
C!r   )rN   )rN   NN)r-   r.   r/   r0   r   r   r   r   RequestHandlerrJ   rl   rx   r   r   	websocketWebSocketHandlerr   rS   r4   r!   r   r5   rj   rk   s   @r   r    r    F   s    KGC'++,, C'7;;55 '$C33 C#** #(7;;-- (%-G%%66 %-N&KP + +Z + +r   r    c                 P   SS K n[        R                  5         SS Kn UR	                  5         U R                  n[        [        R                  R                  5       S5      R                  5       nUR                  R!                  U5      nUR#                  [        R$                  U[        R&                  R(                  U R*                  [        R,                  S9R/                  S5      $ ! [
         a0    [        R                  5       (       d  [        R                  5          Nf = f)Nr   zipython_inline_figure.html)r   r_   r`   ra   r   zutf-8)tornado.templater    r!   r   r   r   webagg_server_threadis_aliver   numberr   r1   r   r   	read_texttemplateTemplategenerater#   r2   r`   ra   r   decode)r   r   r   rf   tplts         r   ipython_inline_displayr   .  s      ")  "
 ]]F
t''<<>+--6Y[ !!#&A:: ++//99}}##  %
 &,VG_5  )#,,.. &&()s   C+ +7D%$D%c                       \ rS rSr\r\rSrg)_BackendWebAggiE  r   N)	r-   r.   r/   r0   r7   FigureCanvasr   FigureManagerr5   r   r   r   r   r   E  s    %L'Mr   r   )+__doc__
contextlibr   r   ior   r   r   pathlibr   r   r   r   	threadingr   ImportErrorerrr   tornado.webtornado.iolooptornado.websocket
matplotlibr$   matplotlib.backend_basesr   matplotlib._pylab_helpersr   rN   r	   r1   r
   r   Threadr   r   FigureCanvasWebAggCorer7   r   Applicationr    r   exportr   r   r   r   <module>r     s    = &       
  H     - ) )  !'';= "$22 ",(44 (e// eP5. 
(X ( ([	  H
=
>CGHs   C C,	C''C,