
    ;g`                      U 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Zddl	Z	ddl
Z
ddlZddlZddlmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZm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* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZCmDZD ddlEmFZFmGZGmHZHmIZImJZJ ddlKmLZL ddlMmNZNmOZOmPZPmQZQmRZR erddlSZTddlUmVZV ddlWmXZX ddl%mYZYmZZZ ddl[m\Z\m]Z] ddl^m_Z_ ddl`maZa ddlbmcZd ddlemfZfmgZgmhZh eTj                  e\z  Zjdekd <   e!j                  eTj                  z  eTj                  z  e_z  ee   z  Zodekd!<    ej                  d"      Zq ere-j                  d#z         Zs ed$es      Ztd%ekd&<    G d' d(eQ      Zu G d) d*eueR      Zv G d+ d,ev      Zw G d- d.ew      Zx G d/ d0e,      Zy G d1 d2ey      Zz G d3 d4ev      Z{ G d5 d6e{ez7      ZBd8Z|y)9z
Declares Syncable and Reactive classes which provides baseclasses
for Panel components which sync their state with one or more bokeh
models rendered on the frontend.
    )annotationsN)Counterdefaultdict
namedtuple)CallableMappingSequence)	lru_cachepartial)pformat)TYPE_CHECKINGAnyClassVar	TypeAlias)UnsetValueError)	DataModel)ImportedStyleSheet)Version)ParameterizedMetaclassWatcher_syncingiscoroutinefunctionresolve_refresolve_value   )holdunlocked)push)CDN_DISTloading_csspatch_stylesheetprocess_raw_cssresolve_stylesheet)
set_curdocstate)DOMEventReactiveHTMLReactiveHTMLParser)HTML_SANITIZERclasspropertyedit_readonlyescapeupdating)import_available)ChildChildren
Layoutable
RenderableViewable)Document)Event)Model
ModelEvent)DataDictPatches)ExtensionArray)Comm)Panel)CallbackJSLinkTargetLinkr   TDataTDataColumnzpanel.reactive)targetlinkstransformedbidirectional_watcherr   tupleLinkWatcherc                      e Zd ZU dZdZded<   dZded<   g Zded<   g Zded	<   i Z	d
ed<   i Z
ded<   i Zd
ed<   i Zd
ed<   g Zded<   g Zded<   dZ fdZe ed      d               Zed5d       Zd6dZd7dZd7dZed8d       Zed8d       Zd9dZd:dZ	 d;	 	 	 	 	 	 	 	 	 	 	 d<dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=d Zd>d!Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d?d"Z 	 	 	 	 	 	 	 	 	 	 d@d#Z!	 	 	 	 	 	 	 	 	 	 	 	 	 	 dAd$Z"d;dB fd%Z#	 	 	 	 	 	 dCd&Z$d' Z%d( Z&d>d)Z'dDd*Z(dEd+Z)dFd,Z*dFd-Z+dFd.Z,dGd/Z-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dHd0Z.dEd1Z/dId2Z0dEd3Z1	 	 	 	 	 	 	 	 	 	 	 	 	 	 dJd4Z2 xZ3S )KSyncableaa  
    Syncable is an extension of the Renderable object which can not
    only render to a bokeh model but also sync the parameters on the
    object with the properties on the model.

    In order to bi-directionally link parameters with bokeh model
    instances the _link_params and _link_props methods define
    callbacks triggered when either the parameter or bokeh property
    values change. Since there may not be a 1-to-1 mapping between
    parameter and the model property the _process_property_change and
    _process_param_change may be overridden to apply any necessary
    transformations.
    i N  zClassVar[int]_timeout2   	_debounceClassVar[list[str]]_priority_changes_manual_params"ClassVar[Mapping[str, str | None]]_renameClassVar[Mapping[str, str]]_js_transforms_source_transforms_target_transforms_stylesheets_busy__ignoreTc                R   d | _         t        |   di | d| _        i | _        g | _        | j                          i | _        i | _        d| _	        | j                  rJ| j                  j                  | j                  j                  | j                  | j                               y y )NF )_themersuper__init__	_updating_events_links_link_params	_changing_in_process__events_watching_stylesheetsrO   _internal_callbacksappendparamwatch_update_manual)selfparams	__class__s     .lib/python3.12/site-packages/panel/reactive.pyr\   zSyncable.__init__   s    "6"     $&  &+" $$++

  !4!4d6I6IJ     N)maxsizec                    i }| j                   d d d   D ].  }t        |t              s|j                  |j                         0 |S )N)__mro__
issubclassrI   updaterQ   )clsrenamesclss      rl   _property_mappingzSyncable._property_mapping   sA     KK"%D$)dll+ & rm   c                @     t         fd j                  D              S )Nc              3     K   | ]N  }|t         j                  vr:j                  j                  ||      j                  j                  ||       P y wN)r3   rf   rw   get).0pri   s     rl   	<genexpr>z.Syncable._linked_properties.<locals>.<genexpr>   sO      
6@&4+A+A+E+Ea+K+W ""&&q!,js   AA)rF   rf   ri   s   `rl   _linked_propertieszSyncable._linked_properties   s!     
6:jj
 
 	
rm   c                @    | j                  | j                               S rz   )_process_param_change_init_params)ri   docs     rl   _get_propertieszSyncable._get_properties   s    ))$*;*;*=>>rm   c                    | j                   j                         D ci c]  \  }}||
 }}}|j                         D ci c]  \  }}|j                  ||      | c}}S c c}}w c c}}w )a3  
        Transform bokeh model property changes into parameter updates.
        Should be overridden to provide appropriate mapping between
        parameter value and bokeh model change. By default uses the
        _rename class level attribute to map between parameter and
        property names.
        )rw   itemsr{   )ri   msgkvinverteds        rl   _process_property_changez!Syncable._process_property_change   si     &*%;%;%A%A%CD%CTQAqD%CD25))+>+$!QQ"A%+>> E>s   A"A(c           	        |j                         D ci c]O  \  }}| j                  j                  |d      .|| j                  vr | j                  j                  |      xs ||Q }}}d|v r| j                  |d   |d<   d|v r| j                  |d   |d<   d|v rPddlm} t        |j                  |j                  |j                        t         d	g}|t        |j                        z  }||j                  z  }|| j                  D cg c]  }t        | |d
       c}z  }||d   z  }g }|D ]  }	t!        |	t"              r|	j%                  d      d   j'                  d      s|	j)                  d      r\t*        j,                  r/t*        j                  j                  t*        j,                  i       }
ni }
|	|
v r|
|	   }	nt/        |	      x|
|	<   }	|j1                  |	        ||d<   |S c c}}w c c}w )a3  
        Transform parameter changes into bokeh model property updates.
        Should be overridden to provide appropriate mapping between
        parameter value and bokeh model change. By default uses the
        _rename class level attribute to map between parameter and
        property names.
        Fwidth	min_widthheight
min_heightstylesheetsr   configzcss/loading.cssrV   ?r   z.csshttpurl)r   rw   r{   rO   sizing_moder   r    loading_spinnerloading_colorloading_max_heightr   r"   raw_css	css_filesrV   r#   
isinstancestrsplitendswith
startswithr%   curdocr   re   )ri   r   r   r   
propertiesr   r   css_filewrapped
stylesheetcaches              rl   r   zSyncable._process_param_change   s    ?Biik
>Ida%%))!U3?T((( ""&&q).Q1k 	 

 j T%5%5%=&0&9J{#z!d&6&6&>'1(';J|$J&&&&&(<(<f>W>W?+-K ?6>>::K6+++K $ 1 1 1H #4>B 1 K :m44KG)
j#.J4D4DS4I!4L4U4UV\4]akavavw}a~|| % 2 2 6 6u||R H "!U*%*:%6
9KPZ9[[j)Jz* * )0J}%E
 s   AG2G8c                    | j                   D cg c]A  }| j                  j                  |d       | j                  j                  |d      |dvr|C c}S c c}w )zT
        Parameters that can be linked in JavaScript via source transforms.
        F)designr   )_synced_paramsrQ   r{   rT   )ri   r}   s     rl   _linkable_paramszSyncable._linkable_params   sg     **
*!dll.>.>q%.H.T''++Au5A.. *
 	
 
s
   !A$Ac                l    g d}| j                   D cg c]  }|| j                  |z   vs| c}S c c}w )z
        Parameters which are synced with properties using transforms
        applied in the _process_param_change method.
        )default_layoutloading
background)rf   rO   )ri   ignoredr}   s      rl   r   zSyncable._synced_params   s7     >::N:a$2E2Eg2M)M:NNNs   11c                    | j                   j                         j                         D ci c]  \  }}|| j                  v r||| c}}S c c}}w rz   )rf   valuesr   r   )ri   r   r   s      rl   r   zSyncable._init_params   sW    !ZZ..0668
8TQD'''AM qD8
 	
 
s   Ac                    | j                   }|rB| j                  j                  | j                  |      }| j                  j                  |       y y rz   )r   rf   rg   _param_changerd   re   )ri   rj   watchers      rl   r`   zSyncable._link_params  sE    $$jj&&t'9'96BG$$++G4 rm   c                   ddl m } |j                  d   }|j                  ry |D ]  }t        |t              r|\  }	}|}
d|v r9|j                  d      ^ }}|D ]  }t        |
|      }
 dj                  |      }nd }|r*|
j                  |t        | j                  ||||             |
j                  |t        | j                  |||              y )Nr   r   id.)r   refembedr   rF   r   getattrjoin	on_changer   _comm_change_server_change)ri   modelr   r   rootcommr   r   r}   _mpartsspsubpaths                 rl   _link_propszSyncable._link_props  s     	#hhtn<<A!U#1AaxGGCL	B2A  ((5/Awt'8'8#sD'RSAwt':':CgNO rm   c                     y)z~
        Method for handling any manual update events, i.e. events triggered
        by changes in the manual params.
        NrY   )ri   eventsr   r   r   parentr   s          rl   _manual_updatezSyncable._manual_update$      rm   c           
     :   | j                   j                         j                         D ]  \  }\  }}|t        j                  vs|t        j
                  v r.t        j                  |   \  }}}}|st        j                  |      rHt               5  | j                  ||||||       d d d        |sd|j                  vst        ||       t        | j                  ||||||      }	|j                  r|j                  |	        |	         y # 1 sw Y   kxY wNembedded)_modelscopyr   r%   _views_fake_roots
_unblockedr   r   tagsr   r   session_contextadd_next_tick_callback)
ri   r   r   r   r   viewabler   r   r   cbs
             rl   rh   zSyncable._update_manual-  s    $(LL$5$5$7$=$=$? C%%,,&#1B1B*B(-S(9%HdCu'',Z''sD&$O  Jdii7dOT00&%dFTXY&&..r2D %@
  Zs   DD	c                    || j                   |<   	 | j                  ||||||       | j                   j                  |d        y # | j                   j                  |d        w xY wrz   )rb   _update_modelpop)ri   r   r   r   r   r   r   curdoc_eventss           rl   _scheduled_update_modelz Syncable._scheduled_update_model>  s^     )6  %	4vsD%dC$$((d3D$$((d3s   A A"c           
        |t         j                  vs|t         j                  v ryt         j                  |   \  }}}}|s!|j                  rt        j                  |      rFt               5  | j                  ||||||       d d d        |rd|j                  vrt        ||       y| j                  j                  |i       }	t        | j                  |||||||	      }
|j                  |
       y# 1 sw Y   pxY w)NTr   F)r%   r   r   r   r   r   r   r   r   rb   r   r   r   r   )ri   r   r   r   r   r   r   r   r   r   r   s              rl   _apply_updatezSyncable._apply_updateJ  s     ell"cU->->&>$)LL$5!$Ts**e.>.>s.C""63eS$G 
$))3S$ 4488bAM55vsD%QTVZ\ijB&&r* s   +C11C:c                   |j                   d   }g x| j                  |<   }| j                  j                  |i       }	|j	                         j                         D ]z  \  }
}|
|	v r||	|
   u r||
= |
| j                  v r| j                  |
= 1	 t        ||
      }|j                  |
      j                  j                  ||      rj|j                  |
       | 	  |j                  di | | j                  j                  |g       D 
cg c]  }
|
|vr|

 }}
|r|| j                  |<   y || j                  v r| j                  |= y y # t        $ r |j                  |
       Y 	w xY wc c}
w # | j                  j                  |g       D 
cg c]  }
|
|vr|

 nc c}
w }}
|r|| j                  |<   w || j                  v r| j                  |= w w xY w)Nr   rY   )r   ra   rb   r{   r   r   r^   r   r   re   lookuppropertymatchesrs   )ri   r   r   r   r   r   r   r   attrsr   attrvalue	model_valchangings                 rl   r   zSyncable._update_model]  s    hhtn&((se0044S"=88:++-KD%}$-2E)E I% LL&#E40	 <<%..66y%HT") .,
	(ELL3 "&!3!3C!<!<u$ !<   &.s#&NN3' ' # T"!%!3!3C!<!<u$ !<H  &.s#&NN3' 's0   EE- 	E(E%$E%-GF7Gc                   t         |   |       |y |j                  d   }|| j                  v r-| j                  j	                  |d       \  }}i |_        i |_        | j                  sr| j                  rf| j                  j                          | j                  t        j                  v r)t        j                  j                  | j                         d| _        | j                  j	                  |d      \  }}|r	 |j                          |r	 |j                          y y # t        $ r Y  w xY w# t        $ r Y y w xY w)Nr   F)NN)r[   _cleanupr   r   r   
_callbacks_event_callbacksrc   setr%   _watch_eventsremove_commsclose	Exception)ri   r   r   r   r   r   client_commrk   s          rl   r   zSyncable._cleanup  s   <hhtn$,,||''T2HE1!E%'E"|| : :&&**,))U-@-@@##**4+E+EF).D& KKOOC>k

 !!#   
  s$   >D# D2 #	D/.D/2	D>=D>c               v    |D ci c]  }|j                   |j                   }}| j                  |      S c c}w rz   )namenewr   )ri   r   r   eventchangess        rl   _update_propertieszSyncable._update_properties  s;     7==fU5::uyy(f=))'22 >s   6c                   ddl m } | j                  D cg c]  }t        |t        j                        s|! }}| j
                  s|j                  r|rt        d      sy t        j                         x| _        }t        j                  j                  |       t        j                  | j                         y c c}w )Nr   r   
watchfiles)r   rV   r   pathlibPurePathrc   
autoreloadr.   asyncior5   r%   r   re   execute_watch_stylesheets)ri   r   stspathsr   s        rl   _setup_autoreloadzSyncable._setup_autoreload  s    " $ 1 1W 1ZWEUEU5V 1W&&v/@/@UO_`lOm-4]]_<"U""5)d--. Xs
   B;B;c                  K   dd l }t        j                  t        j                  t        |                   j                  }g }| j                  D ]b  }t        |t        j                        s|j                         j                         s||z  }|j                         sR|j                  |       d  |j                  |d| j                  i2 3 d {   }| j                  j!                  d       %7  6 y w)Nr   
stop_eventr   )r   r   Pathinspectgetfiletyper   rV   r   r  absoluteis_filere   awatchrc   rf   trigger)ri   r   base_dirr  r  r   s         rl   r  zSyncable._watch_stylesheets  s     <<T
 ;<CC$$C#w//0||~--/"S.C;;=LL% % )z((%WD<V<VW 	.!JJ}-	.Ws6   A+C:.3C:"/C:C8C6C8C:6C88C:c                   |D ci c]  }|j                   | }}d}| j                  j                         j                         D ]  \  }\  }} | j                  |d|j
                  i}|s y || j                  ||||      z  }|t        j                  vrSt        j                  |   d   }	|sl|	| j                  v s{| j                  |	=  y c c}w )NFr      )
r   r   r   r   r   documentr   r%   r   rb   )
ri   r   r   named_eventsappliedr   r   r   r   r   s
             rl   r   zSyncable._param_change  s    7=>ve

E)v>#||00288:OC%000&MennMJt)),
E3OOG%,,&,,s#A&C3$":"::,,S1  ; ?s   Cc           	     (     j                  d|       t         fd|D              r1t        t              5  t        xj                  dz  c_        d d d        |r| j
                  t        j                  <    j                  |      }	 t               5  |j                         D ci c]  \  }}d|vs|| }}}t         t        |            5    j                  j                  di | d d d        d d d        |j                         D ]{  \  }}d|vr|j                  d      ^ }} }|D ]  }	t        ||	      } t        |      5  t        ||g      5   |j                  j                  di ||i d d d        d d d        } 	  j                  d|       t         fd|D              r2t        t              5  t        xj                  dz  c_        d d d        y y # 1 sw Y   xY wc c}}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        $ re t!        |      dkD  rdt#        |       d}
n t!        |      dk(  rdt#        |       d}
nd}
t$        j'                  d	 j(                  d
|
         w xY w# 1 sw Y   y xY w#  j                  d|       t         fd|D              r>t        t              5  t        xj                  dz  c_        d d d        w # 1 sw Y   w xY ww xY w)Nzreceived events %sc              3  @   K   | ]  }|j                   vs|  y wrz   rW   r|   eri   s     rl   r~   z+Syncable._process_events.<locals>.<genexpr>  s     @&QAT-?-?$?q&   r   r   zchanging properties z 
zchanging property 
z!Callback failed for object named  zfinished processing events %sc              3  @   K   | ]  }|j                   vs|  y wrz   r  r  s     rl   r~   z+Syncable._process_events.<locals>.<genexpr>  s     Df1C1C(C1fr  rY   )_loganyr+   r%   _busy_counterrb   r   r   r   r   listrf   rs   r   r   r   lenr   log	exceptionr   )ri   r   rj   r   r   self_paramsr   r}   objr   msg_ends   `          rl   _process_eventszSyncable._process_events  s   		&/@&@@u%##q(# &5;D$$U\\2..v6	-t$06O1#Q,q!tOdD$56%DJJ%%44 7 % 1a<ggcl!!B!#r*C ""3'!#s+(		((2Aq62 , (' '( II5v>DfDD"5)''1,' *) E? &% P66 %$ ,+ ('  	6{Q00AEV!.wv.?sCMM=dii]!G9UV	 *) II5v>DfDD"5)''1,' *)) Es   G(
H. H)G56G5;HG;0H8AH. H!H<H!
H. J(G25H;H	 HHH. HH!!H+	&H. .A.JJ+ J(+6L!L;	LL	Lc                L   | j                  d|       t        t              5  t        xj                  dz  c_        d d d        	 t	        |      5  | j                  |       d d d        | j                  d|       t        t              5  t        xj                  dz  c_        d d d        y # 1 sw Y   sxY w# 1 sw Y   YxY w# 1 sw Y   y xY w# | j                  d|       t        t              5  t        xj                  dz  c_        d d d        w # 1 sw Y   w xY wxY w)Nzreceived bokeh event %sr   z"finished processing bokeh event %s)r"  r+   r%   r$  r$   _process_eventri   r   r   s      rl   _process_bokeh_eventzSyncable._process_bokeh_event  s    		+U35!1$ "	)C##E* ! II:EBu%##q(# &% "! ! &% II:EBu%##q(# &%%sS   B.C B:"C C.B7:C?C C"D#4D	D#D D#c                  K   t         j                  rUt         j                  j                  | j                  |      }|j	                  t        t         j                  |             y t        |      5  	 | j                  |       d d d        y # t        $ r}t        j                  |       Y d }~,d }~ww xY w# 1 sw Y   y xY wwNr   )
r%   _thread_poolsubmit_change_eventadd_done_callbackr   _handle_future_exceptionr$   r   _handle_exception)ri   r   futurer  s       rl   _change_coroutinezSyncable._change_coroutine  s     ''..t/A/A3GF$$WU-K-KQT%UVC/&&s+ ! ! /++A../ !sB   A1C3B:5B	C	B7B2-B:2B77B::C?Cc                T  K   t         j                  rVt         j                  j                  | j                  ||      }|j	                  t        t         j                  |             y 	 | j                  ||       y # t        $ r}t        j                  |       Y d }~y d }~ww xY wwr2  	r%   r4  r5  r0  r7  r   r8  r   r9  ri   r   r   r:  r  s        rl   _event_coroutinezSyncable._event_coroutine  s     ''..t/H/H#uUF$$WU-K-KQT%UV+))#u5 +''**+s0   A'B(*A= <B(=	B%B B( B%%B(c                    | j                   }i | _         t        |      5  | j                  |       d d d        y # 1 sw Y   y xY wrz   )r^   r$   r,  )ri   r   r   s      rl   r6  zSyncable._change_event  s1    _  ( __s	   :Ac                j    t        ||      5  | j                  |       d d d        y # 1 sw Y   y xY w)N)r   )r   r6  )ri   r   r   s      rl   _schedule_changezSyncable._schedule_change  s&    #D!s# "!!s   )2c                   |r| d| }|| j                   j                  |g       v r| j                   |   j                  |       y | j                  j	                  ||i       t
        j                  rVt
        j                  j                  | j                  ||      }|j                  t        t
        j                  |             y 	 | j                  ||       y # t        $ r}	t        j                  |	       Y d }	~	y d }	~	ww xY w)Nr   r3  )ra   r{   r   r^   rs   r%   r4  r5  rB  r7  r   r8  r   r9  )
ri   r   r   r   r   r   oldr   r:  r  s
             rl   r   zSyncable._comm_change  s     Yav&D4>>%%c2..NN3&&t,T3K(''..t/D/Dc4PF$$WU-K-KQT%UV+%%c40 +''**+s   C 	D'DDc                L   t         j                  rVt         j                  j                  | j                  ||      }|j	                  t        t         j                  |             y 	 | j                  ||       y # t        $ r}t        j                  |       Y d }~y d }~ww xY wr2  r=  r>  s        rl   _comm_eventzSyncable._comm_event/  s}    ''..t/H/H#uUF$$WU-K-KQT%UV+))#u5 +''**+s   (A; ;	B#BB#c               ~    |D ]8  }|r| j                   n| j                  }|j                  |t        ||             : y rz   )rF  _server_eventon_eventr   )ri   r   r   r   event_names
event_namemethods          rl   _register_eventszSyncable._register_events9  s6    %J)-T%%43E3EFNN:wvs';< &rm   c                    |j                   r<t        j                  |      s'|j                  t	        | j
                  ||             y | j                  ||       y rz   )r   r%   r   r   r   r?  rF  r/  s      rl   rH  zSyncable._server_event>  sJ    u'7'7'<&&--sE: S%(rm   c                2   |r| d| }|| j                   j                  |g       v r| j                   |   j                  |       y t        | j                        }| j                  j                  ||i       |ry |j                  rSt        | j                  |      }|| j                  v r|j                  |       y |j                  || j                         y 	 | j                  |       y # t        $ r}	t        j                   |	       Y d }	~	y d }	~	ww xY w)Nr   )ra   r{   r   boolr^   rs   r   r   r;  rN   r   add_timeout_callbackrL   r6  r   r%   r9  )
ri   r   r   r   r   rD  r   
processingr   r  s
             rl   r   zSyncable._server_changeF  s     Yav&D4>>%%c2..NN3&&t,$,,'
T3K(//5Bt---**2.((T^^<+""3' +''**+s   C. .	D7DDreturnztuple[str, ...]r   zDocument | NonerT  dict[str, Any])r   rV  rT  rV  rT  	list[str]rT  rV  )rT  Nonerz   )r   zModel | DataModelr   z)Sequence[str] | Sequence[tuple[str, str]]r   r4   r   r6   r   Comm | NonerT  rZ  )r   %tuple[param.parameterized.Event, ...]r   r6   r   r4   r   r6   r   Model | Noner   r[  rT  rZ  r   param.parameterized.EventrT  rZ  )r   $dict[str, param.parameterized.Event]r   rV  r   r6   r   r6   r   r4   r   r[  r   rV  rT  rZ  )
r   r`  r   rV  r   r6   r   r   rT  rP  r   r`  r   rV  r   r6   r   r6   r   r4   r   r[  rT  rZ  r   r]  rT  rZ  r   r_  r   r4   rT  rV  r   rV  rT  rZ  )r   r4   r   r5   rT  rZ  )r   r4   rT  rZ  )r   r4   r   r[  rT  rZ  )r   r4   r   r   r   r[  r   
str | Noner   r   rD  r   r   r   rT  rZ  )
rJ  r   r   r6   r   r4   r   r[  rT  rZ  )r   r4   r   r   r   re  r   r   rD  r   r   r   rT  rZ  )4__name__
__module____qualname____doc__rJ   __annotations__rL   rN   rO   rQ   rS   rT   rU   rV   rW   _Syncable__abstractr\   r*   r
   rw   r   r   r   r   r   r   r   r   r`   r   r   rh   r   r   r   r   r   r  r  r   r,  r0  r;  r?  r6  rB  r   rF  rM  rH  r   __classcell__rk   s   @rl   rI   rI   O   s2    $Hm#  "I}! .0*/ +-N', 35G/4 35N/4 >@:?=?:? )+L%* *,M&+JD t   
 
?	?*X 
 
 O O
5 9=P&P4]PP"P*5P 
P2;DIPX)1< 
"
4:
4AO
4
4!
4(0
48C
4 &
4 
	
4:AO 
&'(:'(AO'('(!'((0'(8C'( 
'(R43037?3	3/.2$-L
)	/+)$++"%+-8+CM+++"%+ 
+(+=
)++"%+0:+BE+++ 
+rm   rI   c                       e Zd ZU dZdZded<   dddZded<   d	Zd fd
	Zd Z	d Z
ddZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ	 	 d	 	 	 	 	 	 	 	 	 ddZg d	fddZi fddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZ xZS ) Reactivea8  
    Reactive is a Viewable object that also supports syncing between
    the objects parameters and the underlying bokeh model either via
    the defined pyviz_comms.Comm type or using bokeh server.

    In addition it defines various methods which make it easy to link
    the parameters to other objects.
    rY   zClassVar[tuple[str, ...]]_ignored_refsN)r   r   rP   rQ   Tc                    | j                   j                  d      j                         D ]6  \  }}|| j                  j                  vst        |t              r0d|_        8 ||| _        t        |      r*t         j                  j                  | j                         n$|j                  t        | j                               t        | j                        }|r#t        j                   | j"                  g|ddi t%        | L  di | y )NexistingTrg   rY   )rf   objectsr   _param__privateexplicit_no_refsr   r/   
allow_refs_refsr   parameterizedasync_executor_async_refsrs   r   r   bind
_sync_refsr[   r\   )ri   refsrj   r   pobjrk   s        rl   r\   zReactive.__init__u  s    **,,Z8>>@JD$D00AAAtU+"& A DJ"4(##2243C3CDmDJJ78tzz*D

4??>T>>"6"rm   c                d    t        | j                        }| j                  j                  |       y rz   )r   rw  rf   rs   )ri   r   resolveds      rl   r|  zReactive._sync_refs  s"     ,

(#rm   c                  K   t        | j                        }t        j                  | j                        r'|2 3 d {   }| j                  j                  |       %| j                  j                  | d {          y 7 D6 y 7 wrz   )r   rw  r  isasyncgenfunctionrf   rs   )ri   r   r  vals       rl   rz  zReactive._async_refs  sc      ,%%djj1% 'c

!!#&JJHn-'X -s2   7BBBB7B8B9	BBBc                   | j                   r| j                   j                  | |      ni d f\  }}| j                         j                         D ]@  \  }}|dv r||v r||   |z   x||<   }||vs| j                  |   j
                  |us<|||<   B | j                  |      }d|vr|S |r4t        j                  j                  |i       xt        j                  |<   }ni }|rd|j                  v r|j                  d   }nt        }g }	|d   D ]S  }
t        |
t              r0t        |
j                        }||v r||   }
n|
||<   t!        |
|       |	j#                  |
       U |	|d<   |S )N)r   r   r   dist_url)_designrj   r   r   rf   defaultr   r%   rV   r{   _template_variablesr   r   r   r   r   r!   re   )ri   r   rj   r   r   r   r   r   r  r   r   r   s               rl   r   zReactive._get_properties  sw   6:llDLL''c2T
	%%'--/DAq++V &q	A-q	A&DJJqM$9$9$Bq		 0
 //7

*.3.@.@.D.DS".MMEs#eE:!8!88..z:HH$]3J*&89*..)%<!&sJ!+E#J X6z* 4 %0
=!rm   c                   | j                   r| j                   j                  | |      ni d f\  }}|D ci c]  }|j                  |j                   }}d|v rd|v r|d   |d   z   |d<   | j	                  |      S c c}w )Nr   )r  rj   r   r   r   )ri   r   r   rj   r   r   r   s          rl   r   zReactive._update_properties  s    6:llDLL''c2T
	6<=fU5::uyy(f=G#(?%+M%:W]=S%SGM"))'22 >s   A;c                    d|v rM|rd|j                   v r|j                   d   }nt        }|d   D ]  }t        |t              st	        ||       ! t
        	|   ||||||       y )Nr   r  )r  r   r   r   r!   r[   r   )
ri   r   r   r   r   r   r   r  r   rk   s
            rl   r   zReactive._update_model  so     CzS%<%<<22:>#!-0
j*<=$Z: 1 	fc4TBrm   c                    rrt        d      sst        d      r|rt        d      g fd}rt              n
t              } j                  j                  ||      }d}|rRg j	                         D 	
ci c]  \  }	}
|
|	
 c}
}	 fd}j                  j                  |t                    }t        |      }dt        j                  v r5t        |      t        t        j                        k  r||j                  fz  }t        |du|fz    } j                  j                  |       |S c c}
}	w )a>  
        Links the parameters on this `Reactive` object to attributes on the
        target `Parameterized` object.

        Supports two modes, either specify a
        mapping between the source and target object parameters as keywords or
        provide a dictionary of callbacks which maps from the source
        parameter to a callback which is triggered when the parameter
        changes.

        Parameters
        ----------
        target: param.Parameterized
          The target object of the link.
        callbacks: dict | None
          Maps from a parameter in the source object to a callback.
        bidirectional: bool
          Whether to link source and target bi-directionally
        links: dict
          Maps between parameters on this object to the parameters
          on the supplied object.
        zVEither supply a set of parameters to link as keywords or a set of callbacks, not both.FDeclare parameters to link or a set of callbacks, neither was defined.zrBidirectional linking not supported for explicit callbacks. You must define separate callbacks for each direction.c                    | D ]  }|j                   v rj                  |j                          	 r |j                      |       n$t        |j                      |j                         j	                  j                  |j                                 y # j	                  j                  |j                                w xY wrz   )r   re   setattrr   r   index)r   r   r]   	callbacksrC   rB   s     rl   link_cbzReactive.link.<locals>.link_cb  s    ::*H  ,? -	%**-fe<ejj(9599EMM)//%**"=>   MM)//%**"=>s   =B,CNc                 (   | D ]l  }|j                   v rj                  |j                          	 t        |j                      |j                         j	                  |j                          n y # j	                  |j                          w xY wrz   )r   re   r  r   r   )r   r   _reverse_updatingreverse_linksri   s     rl   reverse_linkz#Reactive.link.<locals>.reverse_link  st    #Ezz%66%,,UZZ8=mEJJ&?K)00< $ *00<s   $A44B
precedence)
ValueErrorr%  rf   rg   r   rF   r   _fieldsr&  r  rG   r_   re   )ri   rB   r  bidirectionalrC   r  rj   r   rE   r   r   r  	link_argslinkr  r]   r  s   ``` `         @@@rl   r  zReactive.link  sP   4 Y ) * * y ? @ @= F G G 	
	? %.i4;ZZgv. $ ".3kkm<mdaQTm<M= %+LL$6$6|T-EX$Y!"I	 7??*s9~GOO@T/T"--))IYy7LNc'ddf4 	% =s   E	c                l   ddl m}m} ddlm} ddlm} |r|}n$|r| j                  }nt        | j                        }|D 	cg c]  }	|	t        j                  vs|	 }
}	 || j                  f|
|dd|}|D 	cg c]  }	|	t        j                  v s|	 }}	d|vr0| j                  j                  dd      |s|j                  d	d        || j                  f||d
d|}|r|
D ]9  }	|j                  |	   }|j                  | |	d       t        ||      s3d|_        ; |D ]<  }	|j                  |	   }|j                  | |	|	dk7         t        ||      s6d|_        > |
r%|r# ||j                   d	   |j                   d	         S |
r|j                   d	   S |j                   d	   S c c}	w c c}	w )a  
        Creates a set of widgets which allow manipulating the parameters
        on this instance. By default all parameters which support
        linking are exposed, but an explicit list of parameters can
        be provided.

        Parameters
        ----------
        parameters: list(str)
           An explicit list of parameters to return controls for.
        jslink: bool
           Whether to use jslinks instead of Python based links.
           This does not allow using all types of parameters.
        kwargs: dict
           Additional kwargs to pass to the Param pane(s) used to
           generate the controls widgets.

        Returns
        -------
        A layout of the controls
        r   )Tabs	WidgetBox)Param)LiteralInputControls)
parametersr   r   r   Fr   LayoutT)r   r  jsonr   )layoutr  r  rf   r  widgetsr  r   r%  r3   rw   r{   insert_widgetsjslinkr   
serializer)ri   r  r  kwargsr  r  r  r  linkabler}   rj   controlslayout_paramsstylewidgets                  rl   r  zReactive.controls  s   , 	, )!H,,HDJJ'H%AX(..)@!XA 4y(4,24$,DHqX^^0CHD&4+A+A+E+Efe+T+`is  F+djj /]9#/'-/!**1-d!4@fl3(.F%	 
 #*d!1	>Jfl3(.F%	 # m*ELLO<<??1%%||A1 B Es   F,F,;F1F1c                $    ddl m}  || ||      S )ay  
        Allows defining a JS callback to be triggered when a property
        changes on the source object. The keyword arguments define the
        properties that trigger a callback and the JS code that gets
        executed.

        Parameters
        ----------
        args: dict
          A mapping of objects to make available to the JS callback
        callbacks: dict
          A mapping between properties on the source model and the code
          to execute when that property changes

        Returns
        -------
        callback: Callback
          The Callback which can be used to disable the callback.
        r   )r=   )codeargs)rC   r=   )ri   r  r  r=   s       rl   
jscallbackzReactive.jscallbackT  s    ( 	$9488rm   c                    |r|rt        d      |s|st        d      |i }ddlm}m}m} |xs |}	 || |	       t        |t              r|
 || ||	        || |||||      S )a  
        Links properties on the this Reactive object to those on the
        target Reactive object in JS code.

        Supports two modes, either specify a
        mapping between the source and target model properties as
        keywords or provide a dictionary of JS code snippets which
        maps from the source parameter to a JS code snippet which is
        executed when the property changes.

        Parameters
        ----------
        target: panel.viewable.Viewable | bokeh.model.Model | holoviews.core.dimension.Dimensioned
          The target to link the value to.
        code: dict
          Custom code which will be executed when the widget value
          changes.
        args: dict
          A mapping of objects to make available to the JS callback
        bidirectional: boolean
          Whether to link source and target bi-directionally
        links: dict
          A mapping between properties on the source model and the
          target model property to link it to.

        Returns
        -------
        link: GenericLink
          The GenericLink which can be used unlink the widget and
          the target model.
        z^Either supply a set of properties to link as keywords or a set of JS code callbacks, not both.r  r   )r?   assert_source_syncableassert_target_syncable)r   r  r  r  )r  rC   r?   r  r  r   rI   )
ri   rB   r  r  r  rC   r?   r  r  mappings
             rl   r  zReactive.jslinkk  s    N T 4 5 5 t ? @ @<DOO-%tW-fh'DL"49D&UD"/1 	1rm   c                R   | j                   j                         j                         D ]  \  }\  }}|t        j                  vs|t        j
                  v r. |dd|i|}t        j                  |   \  }}}	}
|
s!t        j                  |	      s|	j                  sMt               5  |	j                  j                  |       ddd       |
sd|j                  vst        |	|
       t        |	j                  j                  |      }|	j                  |        y# 1 sw Y   \xY w)aY  
        Send an event to the frontend

        Parameters
        ----------
        Event: Bokeh.Event
            The event to send to the frontend
        event_kwargs: dict
            Additional keyword arguments to pass to the event
            This will create the following event:
            Event(model=model, **event_kwargs)
        r   Nr   rY   )r   r   r   r%   r   r   r   r   r   r  
send_eventr   r   r   r   )ri   r5   event_kwargsr   r   r   r   	_viewabler   r   r   r   s               rl   _send_eventzReactive._send_event  s      $||00288:OC%%,,&#1B1B*B666E).c):&ItS$u'',C4G4GZMM,,U3  Jdii7dOS]]55u=**2.  ;  Zs   &DD&	rz   rU  rc  ra  )NF)
rB   zparam.Parameterizedr  z dict[str, str | Callable] | Noner  rP  rC   r   rT  r   )r  rX  r  rP  rT  	BasePanel)r  rV  r  r   rT  r=   )NNF)rB   r>   r  zdict[str, str] | Noner  zdict | Noner  rP  rC   r   rT  r?   )r5   r7   r  r   )rf  rg  rh  ri  rp  rj  rQ   _Reactive__abstractr\   r|  rz  r   r   r   r  r  r  r  r  rl  rm  s   @rl   ro  ro  b  s9    /1M+0 43G/  J# $.>3C:CAOCC!C(0C8CC 
C& X\#J)J6VJJ.1J 
JX 02$ 9v /1 94 '+ #7171 $71 	71
 71 71 
71r/rm   ro  c                  N    e Zd ZU dZ ej
                  g ed      Zg Zde	d<   ddiZ
de	d	<   d
Z fdZddZddZddZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd	 	 	 	 	 d d       Zd!dZed"d#d       Zd$dZed%d       Zd fdZ	 d&	 	 	 	 	 	 	 d'dZd(dZ xZS ))SyncableDatazy
    A baseclass for components which sync one or more data parameters
    with the frontend via a ColumnDataSource.
    z1
        The currently selected rows in the data.)r  	item_typer   rM   _data_params	selectionNrP   rQ   Tc                   t        |   di | d | _        d | _        | j                  j                  | j                  | j                        g}| j                  r?|j                  | j                  j                  | j                  | j                               |j                  | j                  j                  | j                  d             | xj                  |z  c_        | j                          | j                          y )Nr  rY   )r[   r\   _data
_processedrf   rg   	_validater  re   _update_cds_update_selectedrd   )ri   rj   r  rk   s      rl   r\   zSyncableData.__init__  s    "6"
ZZ%%dnnd6G6GHI	

  !1!143D3DE 	))$*?*?MN  I- rm   c                     y)zI
        Allows implementing validation for the data parameters.
        NrY   )ri   r   s     rl   r  zSyncableData._validate  r   rm   c                    t               )ao  
        Implemented by subclasses converting data parameter(s) into
        a ColumnDataSource compatible data dictionary.

        Returns
        -------
        processed: object
          Raw data after pre-processing (e.g. after filtering)
        data: dict
          Dictionary of columns used to instantiate and update the
          ColumnDataSource
        )NotImplementedErrorr   s    rl   	_get_datazSyncableData._get_data  s     "##rm   c                x    t        | | j                  d         }|||<   | j                  || j                  |<   yy)a+  
        Implemented by subclasses converting changes in columns to
        changes in the data parameter.

        Parameters
        ----------
        column: str
          The name of the column to update.
        array: numpy.ndarray
          The array data to update the column with.
        r   N)r   r  r  )ri   columnarraydatas       rl   _update_columnzSyncableData._update_column  sA     tT..q12V??&&+DOOF# 'rm   c                Z     | j                   j                  di | j                  d   |i y )Nr   rY   )rf   rs   r  )ri   r  s     rl   _update_datazSyncableData._update_data  s)    

9T..q1489rm   c                    |D ]W  }|j                   dk(  r| j                  rt        | d|j                  z         s9 t	        | d|j                  z         |       Y y )N	triggered_update_)r  r]   hasattrr   r   )ri   r   r   r   r   r   r   r   s           rl   r   zSyncableData._manual_update  sP     Ezz[(T^^zEJJ676j5::56u=	 rm   c                :   | j                         \  | _        | _        d| j                  i}|D ci c]  }|j                  | }}| j                  j                         j                         D ]&  \  }\  }}| j                  |||j                  |       ( y c c}w Nr  )	r  r  r  r   r   r   r   r   source)ri   r   r   r   r  r   r   r   s           rl   r  zSyncableData._update_cds  s    &*nn&6#tzz"7=>ve

E)v><<,,.446KC!Q|S!((C@ 7 ?s   B)indicesc                   || j                   n|}d|i}|D ci c]  }|j                  | }}| j                  j                         j	                         D ]0  \  }\  }}| j                  |||j                  j                  |       2 y c c}w )Nr  )r  r   r   r   r   r   r  selected)	ri   r  r   r   r   r  r   r   r   s	            rl   r  zSyncableData._update_selected  s     %,O$..'"7=>ve

E)v><<,,.446KC!Q|S!((2C2CSI 7 ?s   Bc                    dg| j                   |<   	 |j                  j                  ||       | j                   |= y # | j                   |= w xY wr  )ra   r  stream)ri   r   r   r  rollovers        rl   _apply_streamzSyncableData._apply_stream"  sD    %hs	$LL1s#s#s	   < Ac                b   | j                         \  | _        }| j                  j                         j	                         D ]  \  }\  }}|t
        j                  vs|t
        j                  v r.t
        j                  |   \  }}}}	|	s!|j                  rt        j                  |      rNt               5  |j                  j                  ||       d d d        |	sd|j                  vst        ||	       t        | j                   ||||      }
|j#                  |
        y # 1 sw Y   UxY wr   )r  r  r   r   r   r%   r   r   r   r   r   r  r  r   r   r   r  r   )ri   r  r  r   r   r   r   r   r   r   r   s              rl   _streamzSyncableData._stream)  s    !^^-<<,,.446KC!Q%,,&#1B1B*B(-S(9%HdC3..%2B2B32GZHHOOFH5  Jdii7dOT//aJ**2. 7
  Zs   4D%%D.	c                    dg| j                   |<   	 |j                  j                  |       | j                   |= y # | j                   |= w xY wr  )ra   r  patch)ri   r   r   r  s       rl   _apply_patchzSyncableData._apply_patch9  sB    %hs	$LLu%s#s#s	   ; A
c                .   | j                   j                         j                         D ]  \  }\  }}|t        j                  vs|t        j
                  v r.t        j                  |   \  }}}}|s!|j                  rt        j                  |      rMt               5  |j                  j                  |       d d d        |sd|j                  vst        ||       t        | j                  |||      }	|j                  |	        y # 1 sw Y   TxY wr   )r   r   r   r%   r   r   r   r   r   r  r  r   r   r   r  r   )
ri   r  r   r   r   r   r   r   r   r   s
             rl   _patchzSyncableData._patch@  s    <<,,.446KC!Q%,,&#1B1B*B(-S(9%HdC3..%2B2B32GZHHNN5)  Jdii7dOT..Q>**2. 7
  Zs   DD	c                    g }|D ]K  }|j                   | j                  d   k(  r|j                  dk(  r| j                  r;|j	                  |       M t        |   |  y)z2
        Skip events triggered internally
        r   r  N)r   r  r  r]   re   r[   rh   )ri   r   processed_eventsr  rk   s       rl   rh   zSyncableData._update_manualO  s^     Avv**1--!&&K2GDNN##A&  	 01rm   c                z   dt         j                  v rddl}nd}|rKt        |j                        r4t        | j
                  t              r!| j                  j                         |       y|rN| j
                  j                  j                         dz   }j                  d      xj                  |z  c_	         |j                  | j
                  g      }||j                  | d }t        j                  |       5  | j!                  |       ddd       	 d| _        | j                  j%                  | j&                  d          d| _        | j)                  |       y|rt        |j*                        rt        | j
                  t              rD| j                  j                         j-                         D ci c]	  \  }}||g c}}|       y| j
                  j                  j                         dz   }| j
                  j.                  |<   t        j                  |       5  | j!                  | j
                         ddd       | j)                  | j
                  j                  dd |       yt        t              rt        | j
                  t              rt1        fd	| j2                  D              st5        d
      j-                         D ]C  \  }	}
t7        j8                  | j2                  |	   |
g      }||| d }| j;                  |	|       E | j)                  |       y	  |j                        | j                         yt5        d      # 1 sw Y   [xY w# d| _        w xY wc c}}w # 1 sw Y   ?xY w# t4        $ r  |j*                        Y fw xY w)as  
        Streams (appends) the `stream_value` provided to the existing
        value in an efficient manner.

        Parameters
        ----------
        stream_value: (pd.DataFrame | pd.Series | Dict)
          The new value(s) to append to the existing value.
        rollover: (int | None, default=None)
           A maximum column size, above which data from the start of
           the column begins to be discarded. If None, then columns
           will continue to grow unbounded.
        reset_index (bool, default=True):
          If True and the stream_value is a DataFrame, then its index
          is reset. Helps to keep the index unique and named `index`.

        Raises
        ------
        ValueError: Raised if the stream_value is not a supported type.

        Examples
        --------

        Stream a Series to a DataFrame
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> stream_value = pd.Series({"x": 4, "y": "d"})
        >>> obj.stream(stream_value)
        >>> obj.value.to_dict("list")
        {'x': [1, 2, 4], 'y': ['a', 'b', 'd']}

        Stream a Dataframe to a Dataframe
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> stream_value = pd.DataFrame({"x": [3, 4], "y": ["c", "d"]})
        >>> obj.stream(stream_value)
        >>> obj.value.to_dict("list")
        {'x': [1, 2, 3, 4], 'y': ['a', 'b', 'c', 'd']}

        Stream a Dictionary row to a DataFrame
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> tabulator = DataComponent(value)
        >>> stream_value = {"x": 4, "y": "d"}
        >>> obj.stream(stream_value)
        >>> obj.value.to_dict("list")
        {'x': [1, 2, 4], 'y': ['a', 'b', 'd']}

        Stream a Dictionary of Columns to a Dataframe
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> stream_value = {"x": [3, 4], "y": ["c", "d"]}
        >>> obj.stream(stream_value)
        >>> obj.value.to_dict("list")
        {'x': [1, 2, 3, 4], 'y': ['a', 'b', 'c', 'd']}
        pandasr   Nr   T)dropFrp   c              3  &   K   | ]  }|v  
 y wrz   rY   )r|   colstream_values     rl   r~   z&SyncableData.stream.<locals>.<genexpr>  s     E*33,.*s   z)Stream update must append to all columns.z=The stream value provided is not a DataFrame, Series or Dict!)sysmodulesr  r   	DataFramer  dictr  to_dictr  maxreset_indexconcatilocrf   discard_eventsr  r]   r  r  r  Seriesr   locallr  r  npconcatenater  )ri   r  r  r  pdvalue_index_startcombinedr   r   r  r  concatenateds    `          rl   r  zSyncableData.streamZ  s	   v s{{"B*\2<<8$//40L002H=$(OO$9$9$=$=$?!$C!+77T7B""&77" ryy$//<!@AH##==(4%%d+!!(+ ,'!%

""4#4#4Q#78!&LLx0J|RYY7$//400D0D0F0L0L0NO0N1QV0NOQYZ $ 5 5 9 9 ;a ?5ADOO 12%%d+!!$//2 ,LL--bc2H=d+$//40E$**EE$%PQQ"."4"4"6JC#%>>4::c?E2J#KL+'3XIJ'?''\:	 #7
 \84;#/2<<#=L L)\]]E ,+ "' P ,+  " ;#,299\#:L;s<   M0 /M= 	N	
6NN 0M:=	NNN:9N:c           	        | j                   t        |t              r| j                  |       ydt        j
                  v rddl}nd}t        | | j                  d         }i }|rtt        ||j                        r^|j                  D ]=  }g ||<   |j                  D ]'  }||   j                  ||j                  ||f   f       ) ? | j                  |       y|rt        ||j                        rd|v rM|j!                         D ci c]   \  }}t#        |      t%        |d         |fg" }}}|j'                  d       nCt#        |j(                        |j!                         D 	cg c]  \  }}	t%        |      |	f c}	}i}| j                  |       yt        |t              r|j!                         D ]K  \  }}|D ]A  \  }}
|r2t        | j                   |j                        r|
|j                  ||f<   :|
||   |<   C M d| _        	 | j                  |       d| _        yt-        dt/        |      j0                   d      c c}}w c c}	}w # d| _        w xY w)	a  
        Efficiently patches (updates) the existing value with the `patch_value`.

        Parameters
        ----------
        patch_value: (pd.DataFrame | pd.Series | Dict)
          The value(s) to patch the existing value with.

        Raises
        ------
        ValueError: Raised if the patch_value is not a supported type.

        Examples
        --------

        Patch a DataFrame with a Dictionary row.
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> patch_value = {"x": [(0, 3)]}
        >>> obj.patch(patch_value)
        >>> obj.value.to_dict("list")
        {'x': [3, 2], 'y': ['a', 'b']}

        Patch a Dataframe with a Dictionary of Columns.
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> patch_value = {"x": [(slice(2), (3,4))], "y": [(1,'d')]}
        >>> obj.patch(patch_value)
        >>> obj.value.to_dict("list")
        {'x': [3, 4], 'y': ['a', 'd']}

        Patch a DataFrame with a Series. Please note the index is used in the update.
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> patch_value = pd.Series({"index": 1, "x": 4, "y": "d"})
        >>> obj.patch(patch_value)
        >>> obj.value.to_dict("list")
        {'x': [1, 4], 'y': ['a', 'd']}

        Patch a Dataframe with a Dataframe. Please note the index is used in the update.
        >>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
        >>> obj = DataComponent(value)
        >>> patch_value = pd.DataFrame({"x": [3, 4], "y": ["c", "d"]})
        >>> obj.patch(patch_value)
        >>> obj.value.to_dict("list")
        {'x': [3, 4], 'y': ['c', 'd']}
        Nr  r   r  TFz$Patching with a patch_value of type z> is not supported. Please provide a DataFrame, Series or Dict.)r  r   r  r  r  r  r  r   r  r  columnsr  re   r  r  r  r   r   intr   r   r]   r  r  rf  )ri   patch_valuer  r  patch_value_dictr  r  r   r   r   r  s              rl   r  zSyncableData.patch  sW   ` ??"jd&CKK$s{{"BtT..q12$&*[",,7%--+- ((..E$V,33UKOOESYM<Z4[\ / . JJ'(J{BII6+% !, 1 1 3$ 31 Fc+g"67;<< 3 ! $ !$$W- ((),7,=,=,?,,?LE5 UU+,?,$  JJ'(T*#))+1%&LE5j",,G-2*).Q	 &' , "DN'K(!&6tK7H7Q7Q6R SP P 5$," "'s   %I)II 	Ir^  )rT  ztuple[TData, DataDict])r  r   r  rA   rT  rZ  )r  r@   rT  rZ  )r   r\  r   r6   r   r4   r   r6   r   r]  r   r;   rT  rZ  )r   r_  r  zlist[int] | NonerT  rZ  )
r   r   r   r6   r  r8   r  
int | NonerT  rZ  rz   )r  r8   r  r  rT  rZ  )r   r   r   r6   r  r9   rT  rZ  )r  r9   rT  rZ  )NT)r  z#pd.DataFrame | pd.Series | DataDictr  r  r  rP  rT  rZ  )r	  zpd.DataFrame | pd.Series | dictrT  rZ  )rf  rg  rh  ri  rf   Listr  r  r  rj  rQ   _SyncableData__abstractr\   r  r  r  r  r   r-   r  r  r  r  r  r  rh   r  r  rl  rm  s   @rl   r  r    s[   
 

2 ;4 5I )+L%*3>2EG/EJ
$,":>;>DI>>">,8>@D> 
> A A NRJ0J;KJ	J J$ / /$ / /	2 :>l^?l^l^26l^ 
l^\_rm   r  c                  L     e Zd ZdZdZddZ	 	 	 	 	 	 ddZd	dZd
 fdZ xZ	S )ReactiveDataz
    An extension of SyncableData which bi-directionally syncs a data
    parameter between frontend and backend using a ColumnDataSource.
    Tc                    || _         y rz   )r  )ri   r  s     rl   _update_selectionzReactiveData._update_selection2  s	     rm   c                >   t        |dt        j                  d            }d }|j                  dk(  r|j                  j                  dv rt        |dd       }|rCdd l} |j
                   |j                  |d            j                  j                  |      }nw|d	z  j                  |      }na|j                  dk(  rt        d
 |D              r?t        d |D              s,g }|D ]z  }t        |t        j                        r|j                         }n<t        |t        j                        s"t        j                  j                  |dz        }|j                  |       | |}ndt         j"                  v rdd l}|}	t%        |j&                        t%        d      k\  r2ddlm}
 t        ||
      r |D cg c]  }|dk(  r|j,                  n| }	} |j
                  |	      j                  |      j.                  }n|j                  |      }||S |S c c}w )NdtypeOMiftzr   ms)unitg    .Ac              3  P   K   | ]  }t        |t        j                           y wrz   r   dtdate)r|   ovs     rl   r~   z/ReactiveData._convert_column.<locals>.<genexpr>K  s     AjJr277+j   $&c              3  P   K   | ]  }t        |t        j                           y wrz   r  )r|   ivs     rl   r~   z/ReactiveData._convert_column.<locals>.<genexpr>L  s     A&B
2rww/&r  i  r  z1.1.0)BaseMaskedDtypez<NA>)r   r   r  kindr  r  to_datetimer  tz_localizeastyper  r   datetimer  fromtimestampre   r  r  r   __version__pandas.core.arrays.maskedr"  na_valuer   )ri   r   
old_valuesr  	convertedr  r  
new_valuesr!  
tmp_valuesr"  r   s               rl   _convert_columnzReactiveData._convert_column5  s    
GRXXc];(,	::||  D(UD$/'
 "		."..d"CDKKO  "(# 5 5e <IZZ3AjAAA&AA
 B!"bkk2WWY'BGG4WW222d7;%%b) ! '	$TZJr~~&''*::Ee_5FL"FL!v+1<f  " "		*-44U;BBIe,I"*v9	9"s   ;Hc                   | j                   ry | j                         \  }}|j                         }t        |d      rt	        |j
                        }nt	        |      }d}|j                         D ]  \  }}| j                  j                  ||      }|| j                  v s||vr6t        |t              r0t        |j                         d       }|D 	
cg c]  \  }	}
|
	 }}	}
| j                  t        j                  |      ||         }d }t        |d      r6t        |t        j                         r	 t        j"                  ||   |d      }|	 ||   |k(  j'                         }|r| j)                  ||       d} |sy d| _         t+        | | j,                  d         }	 || j.                  u rCt1        | dg      5  t3        j4                  |       5  || _        d d d        || _        d d d        n| j2                  j7                  d       d| _         || j.                  ur| j9                          y y c c}
}	w # t$        $ r Y w xY w# t$        $ r d}Y w xY w# 1 sw Y   xY w# 1 sw Y   bxY w# d| _         w xY w)	Nr  Fc                    t        | d         S )Nr   )r  )its    rl   <lambda>z,ReactiveData._process_data.<locals>.<lambda>t  s    c"Q%jrm   )keyT)	equal_nanr   r   )r]   r  r   r  r%  r  r   _renamed_colsr{   indexesr   r  sortedr0  r   asarrayndarrayarray_equalr   r  r  r   r  r   r   rf   r  r  r  )ri   r  old_rawold_datar  updatedr  r   sorted_valuesr   r   r-  isequals                rl   _process_datazReactiveData._process_datac  sL   >> NN,,,.7I&7??+G7mG::<KC$$((c2Cdll"c&8&$' &v||~;P Q(561!6,,RZZ-?NIGw	*z)RZZ/P nnWS\9PTUG $&s|y8==?G ##C3- (2  4!2!21!56		#4::%dWI.--d3%,
 4!)DJ /.
 

""7+"DN 4::% &K 7 ! 
 ! $#G$  43 /. #DNsf   H<7II)I: I.I""I.1$I: 	IIII"I+	'I..I73I: :	Jc                    d|v r | j                  |j                  d             d|v r/d| _        	 | j                  |j                  d             d| _        t        |   |       y # d| _        w xY w)Nr  r  TF)rB  r   r]   r  r[   r,  )ri   r   rk   s     rl   r,  zReactiveData._process_events  sl    Vvzz&12!DN'&&vzz)'<=!&' "'s    A) )	A2)r  z	list[int]rT  rZ  )r   z
np.ndarrayr,  rA   rT  rA   )r  z0Mapping[str, list | dict[int, Any] | np.ndarray]rT  rZ  rd  )
rf  rg  rh  ri  _ReactiveData__abstractr  r0  rB  r,  rl  rm  s   @rl   r  r  *  sB    
 J!,: ,:.9,:	,:\8t	( 	(rm   r  c                  >    e Zd ZU  e       Zded<    e       Zded<   y)ReactiveMetaBasezClassVar[set[str]]_loaded_extensionszClassVar[Counter]_name_counterN)rf  rg  rh  r   rG  rj  r   rH  rY   rm   rl   rF  rF    s    -0U*2'.yM$0rm   rF  c                  (    e Zd ZU dZdZded<   ddZy)ReactiveHTMLMetaclassz
    Parses the ReactiveHTML._template of the class and initializes
    variables, callbacks and the data model to sync the parameters and
    HTML attributes.
    zscript\([\"|'](.*)[\"|']\)ClassVar[str]_script_regexc                   ddl m}m} | j                  | _        t        j                  | |||       | j                  }| j                  j                         D ]9  \  }}|| j                  vrt        | d|d      |dvs*t        | d|d       t        |       | _        | j                  j                  | j                         | j                  j                   rt        | d      | j                  j"                  rt        | d      i | _        g | _        | j                  j(                  j                         D ]i  \  }}	|	D ]]  \  }
}}|D ]P  }|| j                  v sd	|v rt+        j,                  | j.                  |      rt+        j0                  | j.                  |      d
   }|| j2                  vrt        | d|d      || j$                  vrg | j$                  |<   | j$                  |   j5                  |
|f       t7        | |      r]|| j$                  vrg | j$                  |<   | j$                  |   j5                  |
|f       | j&                  j5                  ||
|f        t9        j:                  |t=        |             }t        | d| d| d	       ` l t?        t@        j                        }i }| j                  jB                  jE                         D ]  }| j                  |   }| j                  jG                  |      dk(  rt        jH                  ||<   EtK        |      |vstM        |t        jN                  t        jP                  t        jR                  f      svtM        |t        jT                        stM        |jV                  tJ              stY        |jV                  t        jZ                        rtY        |jV                  t@              s|j5                  |        |j]                  d       t^        j`                  |xx   dz  cc<   | t^        j`                  |    } || |||      | _1        y )Nr   )PARAM_MAPPINGconstruct_data_modelz._child_config for zc does not match any parameters. Ensure the name of each child config matches one of the parameters.)r   templateliteralz child parameter declares unknown type {child_type!r}. The '_child_config' mode must be one of 'model', 'template' or 'literal'.zD._template contains for loop without closing {% endfor %} statement.z._template contains tags which were never closed. Ensure all tags in your template have a matching closing tag, e.g. if there is a tag <div>, ensure there is a matching </div> tag.r   r   z5._template inline callback references unknown script zE, ensure the referenced script is declaredin the _scripts dictionary.z3._template references unknown parameter or method 'z*', similar parameters and methods include rQ  r   )r   ignoretypes)2io.datamodelrN  rO  ri  __original_doc__r   r\   rf  _child_configr   rf   r  r(   _parserfeed	_template	_open_for_node_stack_node_callbacks_inline_callbacksr   rematchrL  findall_scriptsre   r  difflibget_close_matchesdirr%  ro  childrenr   r{   Stringr  r   r  DictTupleClassSelectorclass_rr   Parameterizedr   rJ  rH  _data_model)mcsr   basesdict_rN  rO  cls_name
child_typenoder   r   r  rY  r}   r   r   rS  childcparam
model_names                       rl   r\   zReactiveHTMLMetaclass.__init__  s   E"{{''T5%@<< !$ 1 1 7 7 9D*399$ j 3D8 <B B 
 #CC j 3D8 </ /  !: )-' ;;  *bc 
 ;;""* 9 9  BD ";;,,224KD%16-z9#ACII~ xx 1 115!zz#*;*;Q?Bs||3",#+* -==AH E>!>#   s':'::8:C//5++D188$C as':'::8:C//5++D188$C--44dD!_E")";";As3x"H('j )4453 733:)1> - $ 27 5< x~~&[[))002EYYu%F  $$U+y8$||ev,m3Vejj%**ekk%JKfe&9&9:fmmT2"6==%2E2EFv}}h7 u% 3 	v 	++D1Q61v3AA$GHI
.j
rm   N)r   r   rn  ztuple[type, ...]ro  Mapping[str, Any])rf  rg  rh  ri  rL  rj  r\   rY   rm   rl   rJ  rJ    s     $AM=@a
rm   rJ  c                  r     e Zd ZU dZded<   dZded<   dZded<   dZded<   edd       Z	 fd	Z
dd
Z xZS )ReactiveCustomBaseNzClassVar[str | None]_extension_namezClassVar[list[str] | None]__css____javascript____javascript_modules__c                    | j                   du xsN | j                   t        j                  v xr0 t        j                  du xs | j                   t        j                  v S )z8
        Whether the component has been loaded.
        N)ry  rF  rG  r%   _extensions)rt   s    rl   _loadedzReactiveCustomBase._loaded%  s\     4' X  $4$G$GG W4'UC,?,?5CTCT,T	
rm   c                4   t         |   |      }d|v ryt        | dg       xs g }t        j                  r5|D cg c]*  }|j                  d      r|nt        j                   d| , }}|D cg c]  }t        |       c}|d   z   |d<   |S c c}w c c}w Nr   rz  r   /r   r[   r   r   r%   rel_pathr   r   ri   rj   propscssssrk   s        rl   r   z(ReactiveCustomBase._process_param_change0      -f5F"$	2.4"C~~ "! --/B7Gq5MM!  
 69$58r"r*S$m$$%E-  $   /B/Bc                p   |sy | j                   j                  |j                  d   g       }g }i }|j                         D ]a  \  }}|j	                  |      j
                  }	t        ||      }
	 t        |	j                  |
|            }|rL|||<   |j                  |       c || j                   |j                  d   <   	  |j                  di | 
r|| j                   |j                  d   <   y | j                   |j                  d   = y # t        $ r` |	j                  D ]   \  }}|j                  |      s ||      } n 	 t        |	j                  |
|            }n# t        $ r d}Y nw xY wY w xY w# 
r|| j                   |j                  d   <   w | j                   |j                  d   = w xY w)Nr   FrY   )ra   r{   r   r   r   r   r   rP  r   r   alternativesis_validre   rs   )ri   r   r   r   prev_changingr   rD   r   r   proprD  r   tp	converters                 rl   _set_on_modelz ReactiveCustomBase._set_on_model>  s   **488D>2>99;KD%<<%..D%&C
$t||C78 $)D!%! '" *2txx~&	3ELL';'1>txx~.NN488D>2'  $%)%6%6MB	{{5) )% 0 &7$"4<<U#;<G  $#G$$  1>txx~.NN488D>2sH   /DE: ,E7;E7E#"E7#E1.E70E11E76E7:;F5)rT  rP  r   rv  r   r6   r   r6   rT  rZ  )rf  rg  rh  ry  rj  rz  r{  r|  classmethodr  r   r  rl  rm  s   @rl   rx  rx    sL    ,0O)0*.G'.15N.59=6=
 
3rm   rx  c                      e Zd ZU dZi Zded<   i Zded<   dZded<   i Zd	ed
<   dZ	ded<   dZ
 fdZdd  fdZed! fd       Zed        Z	 	 	 	 	 	 	 	 	 	 	 	 d"dZ fdZd#dZd$dZ	 	 	 	 	 	 	 	 	 	 d%dZd&dZed'd       Zed        Z	 	 d(	 	 	 	 	 	 	 	 	 d)dZd*dZd+ fdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,dZd-dZ xZS ).r'   a  
    The ReactiveHTML class enables you to create custom Panel components using HTML, CSS and/ or
    Javascript and without the complexities of Javascript build tools.

    A `ReactiveHTML` subclass provides bi-directional syncing of its parameters with arbitrary HTML
    elements, attributes and properties. The key part of the subclass is the `_template`
    variable. This is the HTML template that gets rendered and declares how to link parameters on the
    subclass to HTML.

    >>>    import panel as pn
    >>>    import param
    >>>    class CustomComponent(pn.reactive.ReactiveHTML):
    ...
    ...        index = param.Integer(default=0)
    ...
    ...        _template = '<img id="slideshow" src="https://picsum.photos/800/300?image=${index}" onclick="${_img_click}"></img>'
    ...
    ...        def _img_click(self, event):
    ...            self.index += 1
    >>>    CustomComponent(width=500, height=200).servable()

    HTML templates
    ~~~~~~~~~~~~~~

    A ReactiveHTML component is declared by providing an HTML template
    on the `_template` attribute on the class. Parameters are synced by
    inserting them as template variables of the form `${parameter_name}`,
    e.g.:

        <div class="${parameter_1}">${parameter_2}</div>

    will interpolate the parameter1 parameter on the class.
    In addition to providing attributes we can also provide children to an HTML
    tag. By default any parameter referenced as a child will be
    treated as a Panel components to be rendered into the containing
    HTML. This makes it possible to use ReactiveHTML to lay out other
    components.

    Children
    ~~~~~~~~

    As mentioned above parameters may be referenced as children of a
    DOM node and will, by default, be treated as Panel components to
    insert on the DOM node. However by declaring a `_child_config` we
    can control how the DOM nodes are treated. The `_child_config` is
    indexed by parameter name and may declare one of three rendering
    modes:

      - model (default): Create child and render child as a Panel
        component into it.
      - literal: Create child and set child as its innerHTML.
      - template: Set child as innerHTML of the container.

    If the type is 'template' the parameter will be inserted as is and
    the DOM node's innerHTML will be synced with the child parameter.

    DOM Events
    ~~~~~~~~~~

    In certain cases it is necessary to explicitly declare event
    listeners on the DOM node to ensure that changes in their
    properties are synced when an event is fired. To make this possible
    the HTML element in question must be given a unique id, e.g.:

        <input id="input_el"></input>

    Now we can use this name to declare set of `_dom_events` to
    subscribe to. The following will subscribe to change DOM events
    on the input element:

       {'input_el': ['change']}

    Once subscribed the class may also define a method following the
    `_{node}_{event}` naming convention which will fire when the DOM
    event triggers, e.g. we could define a `_input_el_change` method.
    Any such callback will be given a DOMEvent object as the first and
    only argument. The DOMEvent contains information about the event
    on the .data attribute and declares the type of event on the .type
    attribute.

    Inline callbacks
    ~~~~~~~~~~~~~~~~

    Instead of declaring explicit DOM events Python callbacks can also
    be declared inline, e.g.:

        <input id="input_el" onchange="${_input_change}"></input>

    will look for an `_input_change` method on the ReactiveHTML
    component and call it when the event is fired.

    Additionally we can invoke pure JS scripts defined on the class, e.g.:

        <input id="input_el" onchange="${script('some_script')}"></input>

    This will invoke the following script if it is defined on the class:

        _scripts = {
            'some_script': 'console.log(model, data, input_el, view)'
       }

    Scripts
    ~~~~~~~

    In addition to declaring callbacks in Python it is also possible
    to declare Javascript callbacks to execute when any synced
    attribute changes. Let us say we have declared an input element
    with a synced value parameter:

        <input id="input_el" value="${value}"></input>

    We can now declare a set of `_scripts`, which will fire whenever
    the value updates:

        _scripts = {
            'value': 'console.log(model, data, input_el)'
       }

    The Javascript is provided multiple objects in its namespace
    including:

      * data :  The data model holds the current values of the synced
                parameters, e.g. data.value will reflect the current
                value of the `input_el` node.
      * model:  The ReactiveHTML model which holds layout information
                and information about the children and events.
      * state:  An empty state dictionary which scripts can use to
                store state for the lifetime of the view.
      * view:   The Bokeh View class responsible for rendering the
                component. This provides access to method like
                `invalidate_layout` and `run_script` which allows
                invoking other scripts.
      * <node>: All named DOM nodes in the HTML template, e.g. the
                `input_el` node in the example above.
    rR   rV  z!ClassVar[Mapping[str, list[str]]]_dom_events rK  rY  z'ClassVar[Mapping[str, str | list[str]]]ra  zNdata\.([^[^\d\W]\w*)[ ]*[\+,\-,\*,\\,%,\*\*,<<,>>,>>>,&,\^,|,\&\&,\|\|,\?\?]*=_script_assignmentTc                   ddl m} t        |       }| j                  j                  j                         D ]O  }| j                  j                  |d      }||vs|dk7  r*||   }t        |t              rUg }|D ]H  }t        |t              r|\  }	}|j                  |	 ||      f       2|j                   ||             J |||<   nHt        |t              r+i }|j                         D ]  \  }
} ||      ||
<    |||<   n ||      x||<   }|j                  |   }	 || j                  j                   vrt#        |      }t        |t$              rt        |t              s!t        |t&              s|j)                  |       R t1        | d  di | i | _        i | _        t9        d       | _        y # t*        $ r7}t-        |j.                   d|dt        |      j.                   d      |d }~ww xY w)	Nr   panelr   z._template declares z8 parameter as a child, however the parameter is of type z. Either ensure that the parameter can accept a Panel component, use literal template using the Jinja2 templating syntax (i.e. {{ <param> }}) or declare the child as a literal in the _child_config.c                      t        t              S rz   )r   r%  rY   rm   rl   r4  z'ReactiveHTML.__init__.<locals>.<lambda>"  s	    K4Erm   rY   )paner  r  rW  re  r   rV  r{   r   r%  rF   re   r  r   rf   rt  ru  r   r0   r/   r  r   RuntimeErrorrf  r[   r\   _attrs_panesr   r   )ri   rj   r  rt   children_parammodechild_valuere  r  r   r5  child_paramr  rk   s                rl   r\   zReactiveHTML.__init__  s   4j"ll33::<N%%)).'BDV+tw 0K+t,'D!$.%)
d uT{(;< d4 ( *2~&K.!,!2!2!4IC$)$KHSM "5)1~&49+4FF~&))N3K!)=)=)N)NN,X6H#K:z(TX?Y#K7))(37 =J 	"6" +,E F  "||n$88J KNK(112 3?? s   #A$F99	G92G44G9c                    | j                   j                         D ]  \  }}|D ]  }|j                  |         t        | 	  |       y rz   )r  r   r   r[   )ri   r   _childpanesr  rk   s        rl   r   zReactiveHTML._cleanup$  sC    ![[..0MFEd#  1 	rm   c                    t         |   D cg c]/  }|| j                  j                  j	                         vs*|dvr|1 c}S c c}w )Nr   )r[   r   rW  re  r   )ri   r}   rk   s     rl   r   zReactiveHTML._linkable_params*  sQ     w/"/!1DLL<Q<Q<X<X<Z3Zi  /" 	" "s
   +AAc                    i S rz   rY   r   s    rl   _child_nameszReactiveHTML._child_names0  s    	rm   c                    |S rz   rY   )ri   r   r   r   r   re  s         rl   _process_childrenzReactiveHTML._process_children4  s	     rm   c                4   t         |   |      }d|v ryt        | dg       xs g }t        j                  r5|D cg c]*  }|j                  d      r|nt        j                   d| , }}|D cg c]  }t        |       c}|d   z   |d<   |S c c}w c c}w r  r  r  s        rl   r   z"ReactiveHTML._process_param_change:  r  r  c                   t        t        j                        }| j                  j                  j                         D ]2  }| j                  j                  |      dk7  s"|j                  |       4 t        t        j                        D ci c]   }t        | |      |dk7  r|t        | |      " }}i g }}| j                  j                         j                         D ]  \  }}| j                  |   }	||v r|dk7  s=|	j                  xs ddk  s*t        |t              rt        |	t        j                        s\t        |t               rt#        j$                  |      }|||<   t        |	t        j&                        s|j                  |        | j)                         \  }
}| _        |j-                  | j*                  | j.                   | j0                  di | j3                  |      | j5                         |t7        t9        j:                  |
            || j                  j<                  D cg c]  \  }}|	 c}}i d	       | j>                  j                         D ]X  \  }}t        |t               s|g}|D cg c].  }t7        t9        j:                  |      jA                               0 c}|d   |<   Z |S c c}w c c}}w c c}w )NrQ  r   r   )	r   r  r  r   event_paramshtmlnodesloopedscriptsr  rY   )!r%  ro  rf   rW  re  r   rV  r{   re   r1   r   r   r  r   r3   ri  r   r)   cleanr5   _get_templater  rs   r\  rl  r   _get_eventsr,   textwrapdedentr  ra  strip)ri   r   rs  r}   rj   data_paramsr  r   r   r~  r  r  rr  r   r  r  scripts                    rl   r   zReactiveHTML._init_paramsH  st   x~~&\\**113E!!%%e,	9u% 4 +/z/?/?*@
*@QtQ+V a  *@ 	 
 %'\JJ%%'--/DAq::a=Dg!v+//&Q!+Ax(D%BUBU1V!S!"((+KN$,##A& 0 $(#5#5#7 eT[[[--$D$$Ot'A'A+'NO&&((8??401+/<<+>+>?+>at+>?

 
	 !% 3 3 5GWgt,")FM*FMFxv.4467g*F9g& !6 I
6 @*s   %K6K	3Kc                @   i }| j                   j                         D ]/  \  }}t        |t              r|D ci c]  }|d c}||<   +|||<   1 | j                  j                         D ]-  \  }}|j                  |i       x||<   }|D ]  }||vsd||<    / |S c c}w )NTF)r  r   r   r%  r   r{   )ri   r   rr  
dom_eventsr  evsnode_eventss          rl   r  zReactiveHTML._get_eventss  s     $ 0 0 6 6 8D**d+1;<A4<t)t	 !9
 ..446ID#)/D")==F4L;K'%*KN  7
   =s   
Bc           
        ddl m} |j                  }| j                  j                  D ci c]  }|g  }}i }	i }
| j                  j                  j	                         D ]  \  }}| j
                  j                  |d      }|dk(  rd |	|<   -t        | |      }t        |t              r$|j	                         D ]  \  }} ||      ||<    n8t        |t              rt        |      D ]  \  }} ||      ||<    n	 ||      g}||	|<   t        |t              rt        |j                               }||
|<    | j                  j	                         D ]5  \  }}|D ]+  }||
j                  |      xs g vs|j                  |       - 7 |	j	                         D ]0  \  }}| j                  j                  |   }t        |t              rt        |j                               }| j
                  j                  |d      }|dk(  s||||<   r|| j                  v r~| j                  |   }|D ]i  }||v r;|j                  d   |j                   v r |j                   |j                  d      \  }}n|j#                  ||||      }||   j%                  |       k ||v r
||   ||<   |D cg c]  }|j#                  ||||       c}||<   3 |
| _        | j'                  |||||      S c c}w c c}w )Nr   r  r   rQ  r   )r  r  re  rW  r   rV  r{   r   r   r  r%  	enumerater   r  r   r   r   
_get_modelre   r  )ri   r   r   r   r   r  
old_modelsr   
new_models	new_panesinternal_panesr  r  r  r5  r   ir  	old_panesold_panechild_panesrs  r   s                          rl   _get_childrenzReactiveHTML._get_children  s    	 ^^
GK||G\G\-]G\VfbjG\
-]LN	;=&*ll&;&;&A&A&C"FN%%)).'BDy $(	&!D.1E%&"'++-JC!&uE#J #0E4((/GAt$T{E!H  0 u %If%&U\\^,-2N>*# 'D& *.):):)<%NI%N$6$6~$F$L"M%%d+ & *=
 $-??#4FK!\\226:N+t,";#5#5#78%%)).'BDy K$7%3
6"4;;. KK7	'Dy(TXXd^t||-K#'<<#?q $T5$ Gv&--e4 ( :%%/%7
6" !,& + OOCud; +&
6") $50 %%%c4jIIk .^`&s   
K++K0c           
        | j                   }| j                  j                  j                         D ];  \  }}| j                  j                  |   D ]  }|j                  d|z  d|z        } = | j                  j                  D ]?  \  }}d|z  }d| d|v r|j                  d| d|      }*|j                  d| d|      }A t        j                  |      }| j                  | j                  t        d}	| j                  j                         j                         D ].  \  }
}||	|
<   |
| j                  v s| j                  |
   |	|
 d	<   0 	 |j                  |	      }t'        | j(                  d      }|j+                  |       t-        |j.                        D ]4  }|j                  d| dd| d      j                  d| dd| d      }6 i }|j0                  j                         D ]]  \  }}|D ]S  \  }}}g }|D ]&  }|| j                  v sd|v s|j3                  |       ( ||vrg ||<   ||   j3                  |||f       U _ | j                  j4                  j                         D ]l  \  }}||f| j                  j                  v r7t7        t9        | |            D ]  \  }}|j                  d| d| dd      }  W|j                  d| dd      }n ||j.                  |fS # t        $ rB}t!        t#        |       j$                   d
t#        |      j$                   d| d|       |d }~ww xY w)Nz${%s}z${%s[{{ loop.index0 }}]}zid="%s-{{ loop.index0 }}"zid=""zid='')rf   ri  r   _namesz+ could not render template, errored with:

z: z.
Full template:

F)rP  z-${id}'z-${id}"r   z${[z]}r  })rY  rW  loop_mapr   loop_var_mapreplacer  jinja2Templaterf   rU  r   r   r  renderr   r  r  rf  r(   rk   rX  r%  r  r   re   re  r  r   )ri   template_string
parent_varr*  vardom_noder   replacementrP  context	parameterr   r  r  parserr   p_attrsrr  r   r   r  tmplparam_attrsr}   r   
child_namer  s                              rl   r  zReactiveHTML._get_template  sv    ..#||44::<OJ||00<"1"9"9cM#=#C#E =  =  <<..KHa5@KhZq!_4"1"9"98*A&#5 #2"9"98*A&#5 / ???3 JJ43H3HPRS $

 1 1 3 9 9 ;Iu!&GID---040A0A)0L9+V,- !<	??7+D $DNNUCD &D4vQ4vY)?@4vQ4vY)?@  ' @B!<<--/KD%,1(z4 #ADJJ#(#**1- $ w&$&GDM$$dK%>? -2 0 #',,"7"7"="="?FJ
#t||':'::%gdJ&?@DAq<<#j\1#S(A2FD A ||c*R$8"= #@ V\\7**M  	:&&' (..21g.>.>-?r! E%%4$57 		s   K7 7	M =L==Mc           
     <   | j                   j                         D cg c]  }|D ]  \  }}}|D ]  }|   }}}}}| j                  j                         D ]W  }t        |t              s|g}|D ]=  }t        j                  | j                  |      D ]  }||vs|j                  |        ? Y | j                  j                  j                         D ]0  }|| j                  j                         v s |j                  |       2 t        |      S c c}}}}w rz   )r  r   ra  r   r%  r^  r`  r  re   rW  re  rl  r   rF   )	ri   pssr   psr}   linked_propertiesr  r  r  s	            rl   r   zReactiveHTML._linked_properties  s    *.++*<*<*>_*>3s81b!\^WXQ\^QsQ*>_}}++-Ggt,")!D$;$;VDA 11)003 E " . #ll33::<N!1!1!<!<!>>!((8 = &'' `s   D
c                &   t        |t              r(|j                         D ]  }| j                  ||        yt        |t              r|D ]  }| j                  ||        yt        |t
              r|j                  j                  d|        yy)z
        Ensure all DataModels have reference to the root model to ensure
        that they can be cleaned up correctly.
        __ref:N)r   r  r   _patch_datamodel_refr%  r   r   re   )rt   r  r   r   s       rl   r  z!ReactiveHTML._patch_datamodel_ref  sy     eT"\\^((C0 $t$((C0 y)JJsen- *rm   c                   t        d	i | j                  |      }|rO| j                         s?| j                  j	                  t        |       j                   d| j                   d       no|m| j                         s]|j                  d   t        j                  v r>| j                  j	                  t        |       j                   d| j                   d       | j                  r)t        j                  j                  | j                         |s|}|j                  d   }|j                  }|j                         D ]1  }t!        |t"              s|j$                  j'                  d|        3 | j)                  |j                         |       |j+                  | j-                  ||||             | j/                  d|||       | j1                  || j2                  |||       ||f| j4                  |j                  d   <   |S )
Nz was not imported on instantiation and may not render in a notebook. Restart the notebook kernel and ensure you load it as part of the extension using:

pn.extension('z')
r   z was not imported on instantiation may not render in the served application. Ensure you add the following to the top of your application:

pn.extension('r  )re  	dom_event)r   r   r   rY   )_BkReactiveHTMLr   r  rf   warningr  rf  ry  r   r%   r   rF  rG  addr  properties_with_valuesr   r   r   re   r  rs   r  rM  r   r   r   )	ri   r   r   r   r   r   r   
data_modelr   s	            rl   r  zReactiveHTML._get_model%  s     <$"6"6s";<JJ:&&' (& '+&:&:%;5B dlln$5<<9WJJ:&&' (& '+&:&:%;5B //33D4H4HIDhhtn %


224A!Y'sen- 5 	!!*"C"C"EsKd00dE4HIkCdKT%<%<c4N(-vTXXd^$rm   c           
        t        |t              sy t        | d|j                   d|j                  d    d       }| ||       |j                  d   }| j
                  j                  di       }| j
                  j                  |j                  i       }d }| j                  D 	ci c]6  \  }}}	||j                  k(  s ||j                  |      r|t        | |	      g8 }
}}}	|j                  |g       |j                  dg       z   |j                  |g       z   |j                  dg       z   |
j                  |g       z   }|D ]
  } ||        y c c}	}}w )Nr   r  *c                X    t        j                  t        j                  dd|      |       S )Nz\{\{.*loop.index.*\}\}z\\d+)r^  r`  sub)rr  patterns     rl   r_  z*ReactiveHTML._process_event.<locals>.matchT  s"    ::bff%>QSWXXrm   )r   r&   r   rr  r  r   r{   r]  )ri   r   r   
event_typestar_cbsnode_cbsr_  rr  r   r}   
inline_cbs	event_cbss               rl   r.  zReactiveHTML._process_eventJ  s[   %*TQuzzl!EJJv,>+?@$G>uIZZ'
((,,S"5((,,UZZ<	Y EIDZDZ HDZ=4q+uUZZ/F WT1-..DZ
 H LLR(8<<R+@@LLR()+3<<R+@ANN:r*+ 	
 BuI Hs   *;Ec                    t         |   |||       t        |t              r.| j	                  |j                         |j                  d          y y )Nr   )r[   r  r   r   r  r  r   )ri   r   r   r   rk   s       rl   r  zReactiveHTML._set_on_modela  sD    c4/eY'%%e&B&B&DdhhtnU (rm   c           	     J   | j                   j                  j                         }i i i }
}	}t        |j	                               D ]_  \  }}||v r_|||<   | j
                  j                  |      dk(  rt        j                  |      |
|<   G||j                  j                         v sd||
|<   j|t        t        j                        dgz   v r||	|<   || j                  v rX| j                  |   j                  xs ddk  s7t        |t              r(t        | j                  |   t        j                         st        |t              rt#        d |D              rddlm} t)        |j                  |      }t        |t              rt|D ci c]  }t)        |d|      | }}g }|D ]P  }|j*                  d    d	t-        |       x}|v r|j/                  ||          9|j/                   |||             R |}||
|<   t        |t        j0                        rYddlm} t)        |j                  |      }|j2                  |j*                  d    d	t-        |       k(  r|}n	 ||      }||
|<   1t        |t4              rt        j                  |      |
|<   [||
|<   b |ry| j                   j6                  rN| j9                         \  }}| _        | j:                  |	d
<   ||	d<   t=        t?        j@                  |            |	d<   | jC                  ||||      }nd }|||	d<   | jE                  |	||       | jE                  |
||j                         |
D ci c]  }||jF                  v s|d }}|r| jE                  |||j                         y y c c}w c c}w )NrQ  r   r   c              3  P   K   | ]  }t        |t        j                           y wrz   )r   rf   rk  )r|   vss     rl   r~   z-ReactiveHTML._update_model.<locals>.<genexpr>|  s!     ,][\UWZE<O<O-P[\r  r   )create_linked_datamodelr   r   -r   r  r  re  F)$rW  re  r   r%  r   rV  r{   r)   r  r  r   ro  rf   r  r   r3   ri  r  rT  r   r   r   r   re   rk  r   r   r  r  r  r,   r  r  r  r  r  )ri   r   r   r   r   r   r   child_paramsnew_children	model_msgdata_msgr  r   r   rD  or  valsr  vnamer  r  re  r}   resets                            rl   r   zReactiveHTML._update_modelf  sV    ||,,335-/RiCIIK(GD!|#%)T"%%))$/9<%3%9%9!%<HTNUZZ2244%&HTNhnn-xj88"#	$#jj&116Q!;8,Z

4@PRWReRe5f At$,][\,])]Aejj$/c4(ADEAwq&!4a7GED&*hhtn%5Qr"vh#??EGK KK7 KK(?D(IJ	  
 A!"Au223Aejj$/88$((4.!12a5'::A/D9A!"As#!/!5!5a!8!"Q )R ||""+/+=+=+?(eT[%)[[	'"%*	'"$*8??4+@$A	&!))#tUDAHH$,Ij!9dE28T5::6#+G8aqE4F4F/FE8GudEJJ7 G FD Hs   NN 3N c                ~   || j                   j                  vr+|dk7  r&t        d| d| j                   j                   d      | j                  |   |   j	                  |       | j                         }| j                  j                         j                         D ]  \  }\  }}| j                  i d|i||         y)aj  
        Registers a callback to be executed when the specified DOM
        event is triggered on the named node. Note that the named node
        must be declared in the HTML. To create a named node you must
        give it an id of the form `id="name"`, where `name` will
        be the node identifier.

        Parameters
        ----------
        node: str
          Named node in the HTML identifiable via id of the form `id="name"`.
        event: str
          Name of the DOM event to add an event listener to.
        callback: callable
          A callable which will be given the DOMEvent object.
        r  zNamed node 'z&' not found. Available nodes include: r   r   N)
rW  r  r  r   re   r  r   r   r   r   )ri   rr  r   callbackr   r   r   r   s           rl   rI  zReactiveHTML.on_event  s    " t||)))dck|D6 2//3||/A/A.B!E F Fd#E*11(;!!##||00288:OC%rHf#5ucB  ;rm   rz   rb  rW  )r   r4   r   r6   r   r6   r   r[  re  dict[str, list[Model]]rT  r  rY  )rT  zdict[str, dict[str, bool]])
r   r4   r   r6   r   r6   r   r[  rT  r  )rT  zEtuple[str, list[str], Mapping[str, list[tuple[str, list[str], str]]]]rS  )NNN)
r   r4   r   r]  r   r]  r   r[  rT  r6   )r   r5   rT  rZ  r  ra  )rr  r   r   r   r  r   rT  rZ  )rf  rg  rh  ri  rV  rj  r  rY  ra  r  _ReactiveHTML__abstractr\   r   r   r   r  r  r   r   r  r  r  r   r  r  r  r.  r  r   rI  rl  rm  s   @rl   r'   r'   `  s   FP 24M.357K27!I}!8:H5: 	Z   J+GZ " "
  #(16>I( 
 )V:J:J#(:J16:J>I:J	:JxH+T ( ( . . 379=###/##+6# 
#J.V
>8:>8AO>8>8!>8(0>88C>8 
>8@Crm   r'   )	metaclass)ro  r'   r  )}ri  
__future__r   r  r'  r  rb  r  loggingr   r^  r  r  collectionsr   r   r   collections.abcr   r   r	   	functoolsr
   r   pprintr   typingr   r   r   r   r  numpyr   rf   bokeh.core.property.descriptorsr   bokeh.modelr   bokeh.modelsr   packaging.versionr   param.parameterizedr   r   r   r   r   r   io.documentr   r   io.notebookr   io.resourcesr   r    r!   r"   r#   io.stater$   r%   models.reactive_htmlr&   r'   r  r(   utilr)   r*   r+   r,   r-   util.checksr.   r   r/   r0   r1   r2   r3   r  r  bokeh.documentr4   bokeh.eventsr5   r6   r7   bokeh.models.sourcesr8   r9   pandas.api.extensionsr:   pyviz_commsr;   layout.baser<   r  rC   r=   r>   r?   r  r@   rj  r;  r  IndexrA   	getLoggerr'  rF   r  rG   rI   ro  r  r  rF  rJ  rx  __all__rY   rm   rl   <module>r,     s  
 #       	 
  8 8 7 7 (      ; ! + % 
 (   (   *  '"-64 /33||h.E9.ZZ"))3bhh>ORZ[^R__K_g()
 [[
\	73U 3P+z P+f[/x [/|
g8 gT|(< |(~1- 1j
, j
Z?3 ?3F]	C%1F ]	C~rm   