
    Be6                       d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZ ddlZddlmZmZmZm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 ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z% ddl&m'Z' ddl(m)Z) e	rddl*m+Z+ ddl,m-Z- dZ.dZ/ed             Z0 G d de           Z1 G d de%e'          Z2 G d de#          Z3 G d de)          Z4 G d  d!e)          Z5dS )"z8
Pane class which render plots from different libraries
    )annotationsN)contextmanager)BytesIO)TYPE_CHECKINGAnyClassVarDictMappingOptional)CustomJS	LayoutDOMModelSpacer)Theme   )remove_root)push)escape)
Layoutable   )PaneBase)PDFPNGSVGImage)	IPyWidget)HTML)Document)Commz`<div style="width:100%;"><div style="position:relative;width:100%;height:0;padding-bottom:60%;">zm<div style="width:100%;height:100%"><div style="position:relative;width:100%;height:100%;padding-bottom:0%;">c              #    K   |j         j        }|                    d           ||v r|                    |          }| ||<   dV  | |v r|                    |           }|||<   t	          ||           |                    |           dS )a  
    Wraps a bokeh callback ensuring that any events triggered by it
    appropriately dispatch events in the notebook. Also temporarily
    replaces the wrapped callback with the real one while the callback
    is executed to ensure the callback can be removed as usual.
    combineN)	callbacks
hold_valueholdindexr   )cbwrappeddoccommr"   r$   r%   s          /lib/python3.11/site-packages/panel/pane/plot.py_wrap_callbackr+   '   s       =#DHHY)((	%	EEE	Y##"	%dOOOHHTNNNNN    c                  F    e Zd ZU dZ ej        dd          Z ej        dee	fd          Z
dZd	ed
<   dddZded<   d( fd	Zd) fdZed*d            Zed             Zed             Zed             Zd Z ej        dd          d             Z	 	 d+d,d'Z xZS )-Bokehz
    The Bokeh pane allows displaying any displayable Bokeh model inside a
    Panel app.

    Reference: https://panel.holoviz.org/reference/panes/Bokeh.html

    :Example:

    >>> Bokeh(some_bokeh_figure)
    Tzx
        Whether to automatically dispatch events inside bokeh on_change
        and on_event callbacks in the notebook.defaultr(   Nz*
        Bokeh theme to apply to the plot.)r0   class_r(   g?ClassVar[float | bool | None]priority)autodispatchtheme"ClassVar[Mapping[str, str | None]]_renamec                      t                      j        |fi | d _         fd|                                D              _        d S )NFc                b    g | ]+\  }}|t           j        v |j        |         j        k    )|,S  )r   paramr0   .0pvselfs      r*   
<listcomp>z"Bokeh.__init__.<locals>.<listcomp>X   sH     
 
 
!QJ$$$djm.C)C)C )C)C)Cr,   )super__init___syncing_propsitems
_overridesr@   objectparams	__class__s   `  r*   rC   zBokeh.__init__U   sa    **6***#
 
 
 
 ,,..
 
 
r,   eventsparam.parameterized.EventreturnNonec                v       j         d |D                t                      j         fd|D               d S )Nc              3  @   K   | ]}|j         t          j        v |V  d S N)namer   r;   )r=   es     r*   	<genexpr>z&Bokeh._param_change.<locals>.<genexpr>^   s1      PPaQVz?O5O5O5O5O5O5OPPr,   c              3  @   K   | ]}|j         j        d gz   v |V  dS )css_classesN)rR   rF   )r=   rS   r@   s     r*   rT   z&Bokeh._param_change.<locals>.<genexpr>_   s9      __aQVtP]?^5^5^5^5^5^5^__r,   )_track_overridesrB   _param_change)r@   rK   rJ   s   ` r*   rX   zBokeh._param_change]   sR    PP6PPPQQ____6___````r,   objr   float | bool | Nonec                ,    t          |t                    S rQ   )
isinstancer   clsrY   s     r*   applieszBokeh.appliesa   s    #y)))r,   c                "    fdS )Nc                z    t                    5   | ||           d d d            d S # 1 swxY w Y   d S rQ   r+   )attroldnewr"   r&   r)   r(   wrapped_callbacks      r*   rf   z:Bokeh._property_callback_wrapper.<locals>.wrapped_callbackg   s    $4c4KK # #4c"""# # # # # # # # # # # # # # # # # #s   044r:   r^   r&   r(   r)   r"   rf   s    ````@r*   _property_callback_wrapperz Bokeh._property_callback_wrappere   s<    	# 	# 	# 	# 	# 	# 	# 	# 	#  r,   c                "    fdS )Nc                v    t                    5   |            d d d            d S # 1 swxY w Y   d S rQ   rb   )eventr"   r&   r)   r(   rf   s    r*   rf   z7Bokeh._event_callback_wrapper.<locals>.wrapped_callbackn   s    $4c4KK  5			                 s   .22r:   rg   s    ````@r*   _event_callback_wrapperzBokeh._event_callback_wrapperl   s<    	 	 	 	 	 	 	 	 	  r,   c                T    |                     dt          i          D ]}|j                                        D ](\  }}|j        |          fd|D             d d <   )|j                                        D ](\  }}|j        |          fd|D             d d <   )d S )Ntypec                @    g | ]}                     |          S r:   )rh   r=   r&   r"   r^   r)   r(   s     r*   rA   z/Bokeh._wrap_bokeh_callbacks.<locals>.<listcomp>x   s=           222sD)LL     r,   c                @    g | ]}                     |          S r:   )rl   rp   s     r*   rA   z/Bokeh._wrap_bokeh_callbacks.<locals>.<listcomp>~   s=           //CyII     r,   )selectr   
_callbacksrE   _event_callbacks)	r^   rootbokeh_modelr(   r)   modelkeycbsr"   s	   `  ``   @r*   _wrap_bokeh_callbackszBokeh._wrap_bokeh_callbackss   s    ''88 	 	E!,2244  S!,S1	             !     	!!! "288::  S!237	             !     	!!!	 	r,   c                <   | j         rd S t          | j                  }|D ]`}|j        |v r;| j        |j                 j        |j        k    r|                    |j                   F|                    |j                   a|| _        | 	                                 d S rQ   )
rD   listrF   rR   r;   r0   re   removeappend_sync_properties)r@   rK   	overridesrS   s       r*   rW   zBokeh._track_overrides   s     	F))	 	) 	)Av""tz!&'9'AQU'J'J  ((((  ((((#r,   rH   )watchc                <     j         d S d _        	  j                             fd j                                                                         D                         fd j        D             }|r  j         j        di | d _        d S # d _        w xY w)NTc                R    i | ]#\  }}|j         vr|t          j        v r|d v ||$S ))rV   rR   )rF   r   r;   r<   s      r*   
<dictcomp>z*Bokeh._sync_properties.<locals>.<dictcomp>   sO       ADO++Z5E0E0E000 1000r,   c                2    i | ]}|t          |          S r:   )getattr)r=   or@   s     r*   r   z*Bokeh._sync_properties.<locals>.<dictcomp>   s2       ()74##  r,   Fr:   )rH   rD   r;   updateproperties_with_valuesrE   rF   )r@   propss   ` r*   r   zBokeh._sync_properties   s    ;F"	(J    !%!C!C!E!E!K!K!M!M     
   -1_  E  ,""++U+++"'D%D''''s   A6B 	Br(   r   ru   Optional[Model]parentr)   Optional[Comm]r   c                   ||                      ||          S | j        t                      }n| j        }|r| j        r|                     ||||           |j        d         }|                    dt          i          D ]-}|j        	                    |j        d         |          |_        .|j
        r||j
        urt          ||           ||f| j        |<   | j                            || j                   |S )Nidrn   )get_rootrH   BkSpacerr4   rz   refrr   r   codereplace	_documentr   _models_designapply_bokeh_theme_to_modelr5   )r@   r(   ru   r   r)   rw   r   jss           r*   
_get_modelzBokeh._get_model   s    <==d+++;JJEEKE 	?D% 	?&&tUC>>>htn,,122 	< 	<Bgooeios;;BGG? 	$s%/99s###"FOS 	//tzBBBr,   rQ   )rK   rL   rM   rN   rY   r   rM   rZ   NNN
r(   r   ru   r   r   r   r)   r   rM   r   )__name__
__module____qualname____doc__r;   Booleanr4   ClassSelectorr   strr5   r3   __annotations__r7   rC   rX   classmethodr_   rh   rl   rz   rW   dependsr   r   __classcell__rJ   s   @r*   r.   r.   <   s        	 	 !5= 43 4 4 4L  EeS\ H- . . .E /2H1111 t3 3G    
 
 
 
 
 
a a a a a a * * * [*     [      [    [
  
  
  U]84(((( ( )((& 6:?C        r,   r.   c                      e Zd ZU dZ ej        ddd          Z ej        dd          Z ej	        d	d	d
gd          Z
 ej        dd          Z ej        ddd          Z ej        ddd          Z ej        dd          ZddddddddZded<   ej        g dz   Zed:d            Zd; fd	Zd  Zed!             Zed"             Zd<d$Zd% Z	 d=d>d+Z	 	 d?d@d4ZdAd8Zd9 Z  xZ!S )B
Matplotliba3  
    The `Matplotlib` pane allows displaying any displayable Matplotlib figure
    inside a Panel app.

    - It will render the plot to PNG at the declared DPI and then embed it.
    - If you find the figure to be clipped on the edges, you can set `tight=True`
    to automatically resize objects to fit within the pane.
    - If you have installed `ipympl` you will also be able to use the
    interactive backend.

    Reference: https://panel.holoviz.org/reference/panes/Matplotlib.html

    :Example:

    >>> Matplotlib(some_matplotlib_figure, dpi=144)
       r   Nz1
        Scales the dpi of the matplotlib figure.)r0   boundsr(   Tz-
        Whether to encode SVG out as base64.r/   pngsvgzI
        The format to render the plot as if the plot is not interactive.)r0   objectsr(   z:
        Whether to optimize output for high-dpi displays.FzC
        Whether to render interactive matplotlib plot with ipympl.)r0   constantr(   Nz^
        The Matplotlib Figure being wrapped, which will be rendered as a
        Bokeh model.)r0   
allow_refsr(   zd
        Automatically adjust the figure size to fit the
        subplots and other artist elements.text)rH   interactivedpitighthigh_dpiformatencoder6   r7   )r   rH   r   r   r   rY   r   rM   rZ   c                    dt           j        vrdS ddlm} t	          ||          }|r|j        t          d          |S )N
matplotlibFr   )Figurez7Matplotlib figure has no canvas and cannot be rendered.)sysmodulesmatplotlib.figurer   r\   canvas
ValueError)r^   rY   r   is_figs       r*   r_   zMatplotlib.applies   s`    s{**5,,,,,,C(( 	4cj( 3 4 4 4r,   c                J     t                      j        |fi | i | _        d S rQ   )rB   rC   	_managersrG   s      r*   rC   zMatplotlib.__init__   s-    **6***r,   c                d  
 dd l }t          |j        dd          }ddlm}m}m} ddlm
 |	                    |           
fd} ||          }|j
                            d            ||d          }	 |            r|j                                         |                    d|           |	S )Nr   backendagg)CanvasFigureManageris_interactive)Gcfc                2                         d           d S )Nr   )destroy)rk   r   s    r*   closerz&Matplotlib._get_widget.<locals>.closer
  s    KKNNNNNr,   close_event)matplotlib.backendsr   backendsipympl.backend_nbaggr   r   r   matplotlib._pylab_helpersr   usepatch	set_alphar   	draw_idlempl_connect)r@   figr   old_backendr   r   r   r   r   managerr   s             @r*   _get_widgetzMatplotlib._get_widget  s    """"j19eDDNNNNNNNNNN111111{###	 	 	 	 	 	A-**> 	#J  """=&111r,   c                X    | j         dk    rt          S | j         dk    rt          S t          S )Nr   r   )r   r   r   r   r@   s    r*   	_img_typezMatplotlib._img_type  s+    ;%J[E!!JJr,   c                    | j         j        S rQ   )r   filetyper   s    r*   r   zMatplotlib.filetype   s    ~&&r,   Dict[str, Any]c                8    | j                             | |          S rQ   )r   _transform_object)r@   rY   s     r*   r   zMatplotlib._transform_object$  s    ~//c:::r,   c                    	 | j                             |          S # t          $ r | j                             | |          cY S w xY wrQ   )r   	_imgshape	TypeError)r@   datas     r*   r   zMatplotlib._imgshape'  sV    	8>++D111 	8 	8 	8>++D$77777	8s    %AAsrcr   width
str | Noneheightc                <    | j                             | |||          S rQ   )r   _format_html)r@   r   r   r   s       r*   r   zMatplotlib._format_html-  s      ~**4eVDDDr,   r(   r   ru   r   r   r)   r   r   c                ~   | j         s| j                            | ||||          S | j                            | j                   |                     | j                  }|                     |          }|d=  | j        |j	        |||fi |}|p|}||f| j
        |j        d         <   || j        |j        d         <   |S )Nr   r   )r   r   r   rH   set_dpir   r   _get_properties_get_ipywidgetr   r   r   r   )r@   r(   ru   r   r)   r   
propertiesrw   s           r*   r   zMatplotlib._get_model2  s      	L>,,T3fdKKKDH%%%""4;//))#..
v##NCt
 
/9
 
 }u(-vTXd^$)0tx~&r,   r   rw   rN   c                   | j         s' |j        di |                     |j                   d S | j        |         }| j        |j        j        ur| j                            | j	                   | j        j
                            d           | j        |j        _        | j                            |j                   |j        j        |j        j        d}|j                            |           |j                                         d S )Nr   )r   r   r:   )r   r   r   documentr   rH   r   figurer   r   r   r   
set_canvas_width_heighthandle_resizer   )r@   r   rw   r   rk   s        r*   _updatezMatplotlib._updateD  s     	EL@@4//??@@@F.%;gn333K)))K''***$(KGN!K""7>222%n3&~57 7EN((///  """""r,   c                v   	 |                     | j                   n"# t          $ r}t          d          |d }~ww xY wt                      }| j        rd}nd }|j                            || j        |                                |	                                | j        |           |
                                S )NzLThe Matplotlib backend is not configured. Try adding `matplotlib.use('agg')`r   )r   	facecolor	edgecolorr   bbox_inches)r   r   	Exceptionr   r   r   print_figurer   get_facecolorget_edgecolorgetvalue)r@   rY   exbr   s        r*   _datazMatplotlib._dataS  s    	tKK!!!! 	t 	t 	tjkkqss	tII: 	!KKK
;''))''))# 	  	
 	
 	
 zz||s    
<7<r   rQ   rY   r   rM   r   )NN)r   r   r   r   r   r   r   r   )r   r   rw   r   rM   rN   )"r   r   r   r   r;   Integerr   r   r   Selectorr   r   r   	ParameterrH   r   r7   r   r   _rerender_paramsr   r_   rC   r   propertyr   r   r   r   r   r   r   r  r   r   s   @r*   r   r      s         " %-I <4 5 5 5C U]4 .0 1 1 1F U^EE5> HL M M MF u}T 0= > > >H  %- CF G G GK U_Td A   F EM% ./ 0 0 0E
 ddDD3 3G    
 + / / /     [       ,   X ' ' X'; ; ; ;8 8 8 HLE E E E E 6:?C    $# # # #      r,   r   c                      e Zd ZU dZ ej        d          Z ej        d          Z ej        dd          Ze	j
        g dz   Z
dd	iZd
ed<   edd            Zd Zd	S )RGGPlotz
    An RGGPlot pane renders an r2py-based ggplot2 figure to png
    and wraps the base64-encoded data in a bokeh Div model.
    i  )r0   r   r   )r0   r   )rH   r   r   r   r   Nr6   r7   rY   r   rM   rZ   c                R    t          |          j        dk    ot          |d          S )NGGPlotr_repr)rn   r   hasattrr]   s     r*   r_   zRGGPlot.appliesz  s%    Cyy!X-H'#x2H2HHr,   c                   ddl m} ddlm} |                    |j        d| j        | j        | j        d          5 } |	                    d          |           d d d            n# 1 swxY w Y   |
                                S )Nr   )robjects)	grdevicesz	cairo-pngsubpixel)rn   r   r   res	antialiasprint)rpy2r  rpy2.robjects.libr  render_to_bytesior   r   r   r   rr  )r@   rY   r  r  r  s        r*   r  zRGGPlot._data~  s    !!!!!!//////((!DKX ) 5 5 	%89HJJw$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% zz||s   A''A+.A+r   )r   r   r   r   r;   r
  r   r   r   r   r  r7   r   r   r_   r  r:   r,   r*   r  r  j  s          
 U]3'''FEM#&&&E
%-I
6
6
6C+.R.R.RR38$-G????I I I [I    r,   r  c                  :    e Zd ZU dZdZded<   edd	            Zd
S )YTz
    YT panes wrap plottable objects from the YT library.
    By default, the height and width are calculated by summing all
    contained plots, but can optionally be specified explicitly to
    provide additional space.
    g      ?r2   r3   rY   boolrM   rZ   c                    t          |dd                              d          ot          |d          ot          |d          S )Nr    zyt.plots_repr_html_r   
startswithr  r]   s     r*   r_   z
YT.applies  sH    \2..99%@@ ,W%%,]++	-r,   N)rY   r"  rM   rZ   )r   r   r   r   r3   r   r   r_   r:   r,   r*   r!  r!    sQ           /2H1111- - - [- - -r,   r!  c                  j    e Zd ZU dZ ej        dg d          ZdZded<   e	dd            Z
ddZdS )Foliumz6
    The Folium pane wraps Folium map components.
    stretch_width)fixedr+  stretch_heightstretch_bothscale_widthscale_height
scale_bothN)r0   r   g333333?r2   r3   rY   r   rM   rZ   c                j    t          |dd                              d          ot          |d          S )Nr   r$  zfolium.r&  r'  r]   s     r*   r_   zFolium.applies  s6    \2..99)DD ,]++	-r,   r   c                    |dn|}t          |d          r2|                                                    t          t                    }t          t          |                    S )Nr$  r&  )rH   )r  r&  r   FOLIUM_BEFOREFOLIUM_AFTERdictr   )r@   rY   r   s      r*   r   zFolium._transform_object  sZ    [rrc4'' 	K##%%--m\JJD6$<<((((r,   Nr   r	  )r   r   r   r   r;   ObjectSelectorsizing_moder3   r   r   r_   r   r:   r,   r*   r*  r*    s           '%& I; I; I; < < <K /2H1111- - - [-) ) ) ) ) )r,   r*  )6r   
__future__r   r   
contextlibr   ior   typingr   r   r   r	   r
   r   r;   bokeh.modelsr   r   r   r   r   bokeh.themesr   r   io.notebookr   utilr   viewabler   baser   imager   r   r   r   	ipywidgetr   markupr   bokeh.documentr   pyviz_commsr   r4  r5  r+   r.   r   r  r!  r*  r:   r,   r*   <module>rH     s    # " " " " " 



 % % % % % %                                                          ! ! ! ! ! !                  !                 !''''''      r~  (A A A A AH A A AHg g g g g	 g g gT    c   <- - - - - - - -") ) ) ) )T ) ) ) ) )r,   