
    ;gm                    V   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
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mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddl m!Z"m#Z$m%Z& ddl'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@ er,d dlAmBZB d dlCmDZD d dlEmFZFmGZG d dlHmIZI eJeKeLeKeMeKdf   z     f   ZN G d de<e;      ZO G d de2      ZP G d d e0eP!      Z% G d" d#e%      ZQ G d$ d%e%      Z# G d& d'e#      Z!y)(    )annotationsN)defaultdict)CallableMapping)partial)TYPE_CHECKINGAnyClassVarLiteral)ParameterizedMetaclass   config)construct_data_model)component_resource_path)state)AnyWidgetComponentReactComponentReactiveESM)	DataEventESMEvent)DOMEvent)PaneBase)ReactiveReactiveCustomBaseReactiveHTMLReactiveMetaBase)camel_to_kebabclassproperty)import_available)ChildChildren
LayoutableViewableVieweris_viewable_param)
WidgetBase)Document)Event)Model	UIElement)Comm.c                  r     e Zd ZdZ fdZd Zdd	 fdZd Z	 	 d
	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 d fdZ	 xZ
S )PyComponentaJ  
    The `PyComponent` combines the convenience of `Viewer` components
    that allow creating custom components by declaring a `__panel__`
    method with the ability of controlling layout and styling
    related parameters directly on the class. Internally the
    `PyComponent` will forward layout parameters to the underlying
    object, which is created lazily on render.

    Reference: https://panel.holoviz.org/reference/custom_components/PyComponent.html

    :Example:

    .. code-block:: python

        import panel as pn
        import param

        pn.extension()

        class CounterButton(pn.custom.PyComponent, pn.widgets.WidgetBase):

            value = param.Integer(default=0)

            def __panel__(self):
                return pn.widgets.Button(
                    name=self._button_name, on_click=self._on_click
                )

            def _on_click(self, event):
                self.value += 1

            @param.depends("value")
            def _button_name(self):
                return f"count is {self.value}"

        CounterButton().servable()
    c                    t        |   di | d | _        i | _        | j                  j                  | j                  t        j                  D cg c]
  }|dk7  s	| c}       y c c}w )Nname )super__init___view___changing__paramwatch_sync__viewr#   )selfparamsp	__class__s      ,lib/python3.12/site-packages/panel/custom.pyr3   zPyComponent.__init__\   s]    "6"

))z7G7G+W7G!1PV;A7G+WX+Ws   
A(
A(
c                   |r| j                   y |d   j                  | u r| j                   n| }|D ci c]V  }|j                  | j                  vs%| j                  |j                     |j                  ur|j                  |j                  X }}|sy 	 | j                  j                  |       t        j                  j                  |t        |            5  |j                  j                  |       d d d        |D ]  }|| j                  v s| j                  |=   y c c}w # 1 sw Y   2xY w# |D ]  }|| j                  v s| j                  |=   w xY w)Nr   )
r4   objr0   r5   newupdater6   parameterized_syncinglist)r9   eventstargeter:   r;   s         r=   r8   zPyComponent._sync__viewb   s5   -!'$!6D#)
#)aQVV4;K;K-K'quu4 FFAEEM6 	 
 	,##F+$$--fd6lC##F+ D (((((+ 
 DC (((((+ s1   AD AD1 D%4D1 %D.*D1 1EEc                    | j                   y t        | 	  |       |r5|j                  d   | j                  v r| j                  |j                  d   = | j                   j                  |       y Nid)r4   r2   _cleanupref_models)r9   rootr<   s     r=   rK   zPyComponent._cleanupu   sY    <<DHHTNdll2TXXd^,d#    c                   ddl m} ddlm} t	        | j
                  d      r || j
                  d      }n || j                               }|j                  j                         }i i }}t        j                  D ]D  }|dk7  r(|j                  |   j                  ||   k7  r	||   ||<   0|dk7  s6t        | |      ||<   F |j                  j                  | j                  t        j                  D cg c]
  }|dk7  s	| c}       | j                  j                  |       t        j                  j                  |t        |            5  |j                  j                  |       d d d        |S c c}w # 1 sw Y   |S xY w)Nr   )panel)ParamMethod_dinfoT)lazyr0   )panerQ   r6   rR   hasattr	__panel__valuesr#   defaultgetattrr7   r8   rA   rB   rC   rD   )r9   rQ   rR   viewr:   	overridessyncr;   s           r=   _create__viewzPyComponent._create__view}   s7   &4>>8,t~~D9D)*D""$b4	!!AF{tzz!}44q	A%ay	!f!$*Q	 "
 	

))z7G7G+W7G!1PV;A7G+WX

)$  ))$T
;JJd# <	 ,X;s   5
E5
 E5
E::Fc                    | j                   | j                         | _         | j                   j                  ||||      }||n|}||f| j                  |j                  d   <   |S rI   )r4   r^   
_get_modelrM   rL   )r9   docrN   parentcommmodels         r=   r`   zPyComponent._get_model   sb     <<--/DL''T64@u$(-vTXXd^$rO   c                Z    t         |   |      | j                  j                  |      z   S N)r2   selectr4   )r9   selectorr<   s     r=   rg   zPyComponent.select   s(     w~h'$,,*=*=h*GGGrO   rf   rN   Model | NonereturnNoneNNN
ra   r(   rN   rj   rb   rj   rc   Comm | Nonerk   r*   )rh   z(type | Callable[[Viewable], bool] | Nonerk   zlist[Viewable])__name__
__module____qualname____doc__r3   r8   rK   r^   r`   rg   __classcell__r<   s   @r=   r.   r.   5   sz    $LY,&$, 379=		#/		+6	 
	 DHH@H	H HrO   r.   c                      e Zd ZddZy)ReactiveESMMetaclassc           	        | j                   | _        t        j                  | |||       t        j
                  |xx   dz  cc<   | t        j
                  |    }t        j                  D cg c]5  }t        t        | j                  |   j                        t              r4|7 }}t        | ||dt        j                  i      | _        y c c}w )Nr   esm_constants)r0   ignoreextras)rs   __original_doc__r   r3   r   _name_counterr   r6   
issubclasstypeownerrw   r   Dict_data_model)mcsr0   basesdict_
model_namer;   ignoreds          r=   r3   zReactiveESMMetaclass.__init__   s    "{{''T5%@ 	&&t,1,v.<<TBCD
&nnonJtCIIaLDVDV?WYm4n1no.j/5::9V
 ps   .5C$CN)r0   strr   ztuple[type, ...]r   zMapping[str, Any])rp   rq   rr   r3   r1   rO   r=   rw   rw      s    

rO   rw   c                      e Zd ZU dZeZdZded<   i Zded<   dZ	ded	<   i Z
d
ed<   i Zded<   dZ fdZed        Zed$d       Zed%d&d       Zed'd(d       Zd)d* fdZd Zd Zed+d       Zd, fdZed        Z	 	 	 	 	 	 	 	 	 	 	 	 d-dZd.dZ fdZ	 	 d/	 	 	 	 	 	 	 	 	 d0dZd1dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2dZ d3d Z!d3d!Z"d4d"Z#d5d#Z$ xZ%S )6r   a  
    The `ReactiveESM` classes allow you to create custom Panel
    components using HTML, CSS and/ or Javascript and without the
    complexities of Javascript build tools.

    A `ReactiveESM` subclass provides bi-directional syncing of its
    parameters with arbitrary HTML elements, attributes and
    properties. The key part of the subclass is the `_esm`
    variable. Use this to define a `render` function as shown in the
    example below.

    :Example:

    .. code-block:: python

        import panel as pn
        import param

        pn.extension()

        class CounterButton(pn.custom.ReactiveESM):

            value = param.Integer()

            _esm = """
            export function render({ model }) {
                let btn = document.createElement("button");
                btn.innerHTML = `count is ${model.value}`;
                btn.addEventListener("click", () => {
                    model.value += 1
                });
                model.on('value', () => {
                    btn.innerHTML = `count is ${model.value}`;
                })
                return btn
            }
            """

        CounterButton().servable()
    Nz"ClassVar[str | os.PathLike | None]_bundlezClassVar[dict[str, Any]]
_constants zClassVar[str | os.PathLike]_esmzClassVar[ExportSpec]
_exports__z<ClassVar[dict[Literal['imports', 'scopes'], dict[str, str]]]
_importmapTc                h    t        |   di | d| _        t        t              | _        g | _        y )NFr1   )r2   r3   _watching_esmr   rD   _event__callbacks_msg__callbacks)r9   r:   r<   s     r=   r3   zReactiveESM.__init__   s0    "6""!,T!2!rO   c                >   t        | d      rt        j                  | j                        S 	 t        j                  t	        j
                  |             j                  S # t        t        t        f$ r) t        | j                  t        j                        sY y Y y w xY w)N__path__)rV   pathlibPathr   inspectgetfilerb   OSError	TypeError
ValueError
isinstancer   PurePathclss    r=   _module_pathzReactiveESM._module_path   st    3
#<<--	<< 45<<<J/ 	ckk7+;+;< =	s   1A 8BBc                n   t         j                  r| j                  ry | j                  }|y | j                  r| j
                  D ]/  }t        |t              s| j                  |j                  k(  s.|} 1 | j                  }| j                  }t        |t        j                        r|S |r(|j                  d      r||z  }|j                         r|S t        d| j                   d| j                   d      || j                   dz  }|j                         r|S | j                  }|j!                  d      }t#        t%        t'        |                  D ]z  }dj)                  |d |dz          }		 t+        j,                  |	      }
t3        |
dd       }|s@t5        j6                  |      j8                  }||	 dz  }|j                         sx|c S  |t:        j<                  v r9t:        j<                  |   }||j                   dz  }|j                         r|S d S y # t.        t0        f$ r Y w xY w)	N.jszCould not resolve z
._bundle: zo. Ensure you provide either a string with a relative or absolute path or a Path object to a .js file extension.z
.bundle.js.r   __file__)r   
autoreloadr   r   r   __mro__r~   r   r   osPathLikeendswithis_filer   rp   rq   splitreversedrangelenjoin	importlibimport_moduleModuleNotFoundErrorImportErrorrZ   r   r   rb   sysmodules)r   mod_pathsclsbundlebundle_pathpathmoduler   i	submodulemodmod_filesubmodule_path
module_objs                 r=   _bundle_pathzReactiveESM._bundle_path   s   ##;;dK0S[[DLL5PC $ ''H[[F&"++.FOOE2&/&&(&&$S\\N*S[[M JA A  S\\N*55<<>K ,,s#%G-.A!A#/I--i8 sJ5H$\\(3::N!yk$<<D||~ / S[[ V,J!4!4 5Z@@D<<>43t3 (5 s   4H""H43H4c                   |s| j                   s| j                  }|r|S | j                   }t        |t        j                        r|S |r|j                  d      sy 	 t        | d      r| j                  }n2t        j                  t        j                  |             j                  }||z  }|j                         r|S 	 y # t        t        t         f$ r Y y w xY w)N)r   z.jsxz.tsz.tsxr   )r   r   r   r   r   r   rV   r   r   r   r   r   rb   r   r   r   r   )r   compiledr   esmr   esm_paths         r=   	_esm_pathzReactiveESM._esm_path5  s    388**K""hhc2;;'JCLL)GH		sJ'<<"<<(<=DD#~H! "  J/ 		s   A!C   CCc                h   | j                  |du       }|r|| j                  k(  r| j                  t        j                  v r|r| }| j
                  dd  D ]-  }t        |t              s||j                  |du       k(  s,|}/ t        |d|      }t        j                  r{t        j                  t        |j                         j                        j!                  d            j#                         }|d| z  }n|j%                  d      }n| j&                  }t)        j*                  |      }|S )NTr   r   r   utf-8?)encoding)r   r   rq   r   r   r   r~   r   r   r   r   hashlibsha256r   statst_mtimeencode	hexdigest	read_textr   textwrapdedent)r   r   serverr   base_clsr   r   modifieds           r=   _render_esmzReactiveESM._render_esmL  s	   ==(d*:=;3+++#++0MRXKKOD%dK8 4>>8t;K>#LL#'	 ,
 .hQ$$&~~c(--/2J2J.K.R.RSZ.[\ffhHQxj>)C(('(:((Cooc"
rO   c                Z   |r|j                   d   }|| j                  v r|| j                  |   \  }}|j                  D ][  }t        | |      }t	        |t
              r|g}t	        |t              s3|D ]$  }t	        |t
              s|j                  |       & ] t        |   |       | j                  st| j                  rg| j                  j                          | j                  t        j                  v r)t        j                  j                  | j                         d| _	        y y y )NrJ   F)rL   rM   childrenrZ   r   r$   rD   rK   r2   r   setr   _watch_eventsremove)r9   rN   rL   rd   _childr   r<   s          r=   rK   zReactiveESM._cleanupb  s    ((4.Cdll"<<,q"^^E&tU3H!(H5$,:!(D1%-E)%: %t 4 &. , 	|| 2 2""$!!U%8%88##**4+=+=>!&D	 !3|rO   c                   K   dd l }| j                  d      }|j                  || j                        2 3 d {   }| j	                          7 6 y w)Nr   Fr   )
stop_event)
watchfilesr   awatchr   _update_esm)r9   r   r   r   s       r=   
_watch_esmzReactiveESM._watch_esmv  sN     ~~u~-!(($:L:L(M 	!	Ms%   4AAAAAAAc                   | j                   j                         j                         D ]  \  }\  }}|t        j                  vrt        j                  |   d   }|j
                  xr |j
                  j                  }| j                  t        j                   |      }||j                  k(  r| j                  i d|i||        y )N   r   r   )rM   copyitemsr   _viewssession_contextserver_contextr   r   r   r   _apply_update)r9   rL   rd   r   ra   
is_sessionr   s          r=   r   zReactiveESM._update_esm|  s    #||00288:OC%%,,&,,s#A&C,,S1D1D1S1SJ""v'8'8#8"LCeiirE3<<  ;rO   c                V    t        d | j                  j                         D              S )Nc              3  ,   K   | ]  }|d k7  s	|  yw)js_property_callbacksNr1   ).0r;   s     r=   	<genexpr>z1ReactiveESM._linked_properties.<locals>.<genexpr>  s     ^ =1F]A]Q =s   
)tupler   
properties)r9   s    r=   _linked_propertieszReactiveESM._linked_properties  s"    ^ 0 0 ; ; =^^^rO   c                   t         |   |      }t        |       }i }t        j                  D cg c]v  }t        |j                  |   j                  t              rK|t        j                  v r;|dk7  r6t        t        j                  |         t        |j                  |         u r|x }}| j                  j                         j                         D ]o  \  }}| j                  |   }t        |      }	||v r|dk7  s|j                  xs ddk  s|	r|	r||v r|j                  |       V||v r|j                  |       |||<   q | j                  }
| j                         }d}|
rt!        |xr$ |j"                  xr |j"                  j$                        }|
| j'                  t(        j*                         k(  r!|j,                  t.        j0                  v r|rd}n>t3        j4                  t7        |
      j9                  d            j;                         }nd }| j=                  |      }| j>                  |d<   |jA                  |tC        |jD                         | jF                  d
i |j                         D ci c]  \  }}||vs|| c}}t(        j*                  xs tI        | dd      | jK                  t(        j*                   |      ||jD                  d	       |S c c}w c c}}w )Nr0   r   Furlr   ry   _debugr   )r   
class_namedatadevr   	importmapr0   r1   )&r2   _get_propertiesr   r   r6   r~   r   r   r$   rX   r   r&   
precedencepopr   _process_importmapboolr   r   r   r   r   rq   r   r   r   r   r   r   r   _process_param_changer   rA   r   rp   r   rZ   r   )r9   ra   propsr   data_paramsr;   r   kvis_viewabler   r   r   bundle_hash
data_propsr<   s                  r=   r  zReactiveESM._get_properties  s   ',4j
  ~~
%!ciil00+>(.. Q&[T(..:K5LPTUXU^U^_`UaPb5b ~ 	 

 JJ%%'--/DAq

1A+A.KWf1<<3D12Ik1:IIaLEz		!KN 0 ''++-	
c`c&9&9`c>Q>Q>`>`aJdnn1B1B-BCCZ]ZeZeHejt#%nnS-=-D-DW-MNXXZK//<
&*oo
?#!(6$D$$a9I9I9K'`9KAqX_O_19K'`a$$Fh(F##(9(9$9*#M"LL
 	 K
> (as   A;K&;K+K+c                    | j                   S rf   )r   r   s    r=   r  zReactiveESM._process_importmap  s    ~~rO   c           	     <   |y |j                   d   }t        |t              rB|D cg c]6  }||j                  v r|j                  |   d   n|j	                  ||||      8 c}S ||j                  v r|j                  |   d   S |j	                  ||||      S c c}w )NrJ   r   )rL   r   rD   rM   r`   )r9   r   ra   rN   rb   rc   rL   svs           r=   _get_child_modelzReactiveESM._get_child_model  s     =hhtneT"  B '*RZZ&7

3"R]]3PTV\^b=cc  EMM!==%a((T6488s   ;Bc                    i }| j                   j                         j                         D ]8  \  }}| j                   |   }	t        |	      s!| j	                  |||||      ||<   : |S rf   )r6   rX   r   r&   r  )
r9   
data_modelra   rN   rb   rc   r   r	  r
  r;   s
             r=   _get_childrenzReactiveESM._get_children  sf    JJ%%'--/DAq

1A$Q'//3fdKHQK	 0
 rO   c                j   ddl m } |j                  st        | dd      rt        d      sy t        |           | j                  d      rg| j                  sZt        j                         x| _        }t        j                  j                  |       t        j                  | j                         y y y )Nr   r   r   Fr   r   )r   r   rZ   r    r2   _setup_autoreloadr   r   asyncior)   r   r   appendexecuter   )r9   r   eventr<   s      r=   r  zReactiveESM._setup_autoreload  s    """gdHe&DJZ[gJh!#NNEN*43E3E)08D&&u-MM$//* 4F*rO   c                   | j                  |      } | j                  di |}|xs |}| j                  |j                  ||||      } |j                  j                  di | t        |      |_        ||f| j                  |j                  d   <   | j                  |d   | j                  |||       | j                  dd|||       | j                          |S )NrJ   r   	dom_event
data_event)rd   ra   rc   r1   )r  _bokeh_modelr  r   rA   rD   r   rM   rL   _link_propsr   _register_eventsr  )r9   ra   rN   rb   rc   r  rd   r   s           r=   r`   zReactiveESM._get_model  s     $$S)!!!*E*}u%%ejj#tUDI

%H%h(-vTXXd^$v(?(?dDQk<u#TXY rO   c                   t        |t              rh| j                  D ]#  }t        j                  t        ||      d       % t        j                  t        | j                  |j                        d       y t        |t              sy t        | d|j                         r t        | d|j                         |       | j                  j                  |j                  g       D ]#  }t        j                  t        ||      d       % y )NF)schedule_handle_)r   r   r   r   r  r   _handle_msgr   r   rV   noderZ   r   get)r9   r  cbs      r=   _process_eventzReactiveESM._process_event  s    eY'**gb%05A +MM'$"2"2EJJ?%PE8,48EJJ<012GDHUZZL1259((,,UZZ<BMM'"e,u= =rO   c                   i i }}t        |j                               D ]?  \  }	}
|	t        t        j                        ddgz   v r|
||	<   ,|	j                  v r;|
||	<   A |j                         D ]  \  }}|j                  vr|j
                  }t        |j                  t               r|j                  n|j                  g}|D ].  }|||u st        |t               r||v r|j                  |       0  t        fd|D              r>| j                  j                  |||      }|j                  |       t        |      |d<   | j                  ||       | j                  ||j                         y )Nr   r   c              3  :   K   | ]  }|j                   v   y wrf   )r   )r   rG   rd   s     r=   r   z,ReactiveESM._update_model.<locals>.<genexpr>  s     3FqqENN"Fs   r   )rD   r   r   r6   r   r@   r   oldrK   anyr  r   rA   _set_on_model)r9   rE   msgrN   rd   ra   rc   	model_msgdata_msgpropr
  r0   r  r@   old_objectsr+  r   s       `            r=   _update_modelzReactiveESM._update_model  sQ     "28	CIIK(GD!tHNN+UK,@@@"#	$'!" ) "<<>KD%5>>)))C'1%))T'B%))K";#*C1F3RU:T" # * 3F33))%**c4MHOOH%$(NIj!9dE28T5::6rO   c                     y)z
        Message handler for messages sent from the frontend using the
        `model.send_msg` API.

        Parameters
        ----------
        data: any
            Data received from the frontend.
        Nr1   r9   r   s     r=   r$  zReactiveESM._handle_msg  s    rO   c                2    | j                  t        |       y)z
        Sends data to the frontend which can be observed on the frontend
        with the `model.on_msg("msg:custom", callback)` API.

        Parameters
        ----------
        data: any
            Data to send to the frontend.
        )r   N)_send_eventr   r5  s     r=   	_send_msgzReactiveESM._send_msg#  s     	-rO   c                :    | j                   j                  |       y)aB  
        Registers a callback to be executed when a message event
        containing arbitrary data is received.

        Parameters
        ----------
        event: str
          Name of the DOM event to add an event listener to.
        callback: callable
          A callable which will be given the msg data.
        N)r   r  )r9   callbacks     r=   on_msgzReactiveESM.on_msg/  s     	##H-rO   c                @    | j                   |   j                  |       y)a8  
        Registers a callback to be executed when the specified DOM
        event is triggered.

        Parameters
        ----------
        event: str
          Name of the DOM event to add an event listener to.
        callback: callable
          A callable which will be given the DOMEvent object.
        N)r   r  )r9   r  r:  s      r=   on_eventzReactiveESM.on_event=  s     	u%,,X6rO   )rk   os.PathLike | None)T)r   r  rk   r>  TFr   zbool | Literal['compiling']r   r  rf   ri   )rk   ztuple[str, ...])ra   zDocument | Nonerk   dict[str, Any])r   r$   ra   r(   rN   r*   rb   r*   rc   ro   rk   z"list[UIElement] | UIElement | None)rk   z-dict[str, list[UIElement] | UIElement | None]rm   rn   )r  r)   rk   rl   )rE   z$dict[str, param.parameterized.Event]r.  rA  rN   r*   rd   r*   ra   r(   rc   ro   rk   rl   )r   r	   rk   rl   )r:  r   rk   rl   )r  r   r:  r   rk   rl   )&rp   rq   rr   rs   _BkReactiveESMr  r   __annotations__r   r   r   r   _ReactiveESM__abstractr3   r   r   r   classmethodr   r   rK   r   r   propertyr   r  r  r  r  r  r`   r(  r3  r$  r8  r;  r=  rt   ru   s   @r=   r   r      s   'R "L26G/6+-J(-(*D
%* (*J$)NPJKPJ"   3 3j  ,  *'(	= _ _,\  99$,9499CH9P[9	+9+ 379=#/+6 
 >7:7AO77!7(078C7 
78	
..7rO   r   )	metaclassc                      e Zd ZdZdZy)JSComponenta  
    The `JSComponent` allows you to create custom Panel components
    using Javascript and CSS without the complexities of
    Javascript build tools.

    A `JSComponent` subclass provides bi-directional syncing of its
    parameters with arbitrary HTML elements, attributes and
    properties. The key part of the subclass is the `_esm`
    variable. Use this to define a `render` function as shown in the
    example below.

    Reference: https://panel.holoviz.org/reference/custom_components/JSComponent.html

    :Example:

    .. code-block:: python

        import panel as pn
        import param

        pn.extension()

        class CounterButton(pn.custom.JSComponent):

            value = param.Integer()

            _esm = """
            export function render({ model }) {
                let btn = document.createElement("button");
                btn.innerHTML = `count is ${model.value}`;
                btn.addEventListener("click", () => {
                    model.value += 1
                });
                model.on('value', () => {
                    btn.innerHTML = `count is ${model.value}`;
                })
                return btn
            }
            """

        CounterButton().servable()
    TN)rp   rq   rr   rs   _JSComponent__abstractr1   rO   r=   rI  rI  L  s    )V JrO   rI  c                  ^     e Zd ZdZdZeZdZedd       Z	e
dd	 fd       Ze
d        Z xZS )
r   a  
    The `ReactComponent` allows you to create custom Panel components
    using React without the complexities of Javascript build tools.

    A `ReactComponent` subclass provides bi-directional syncing of its
    parameters with arbitrary HTML elements, attributes and
    properties. The key part of the subclass is the `_esm`
    variable. Use this to define a `render` function as shown in the
    example below.

    Reference: https://panel.holoviz.org/reference/custom_components/ReactComponent.html

    :Example:

    .. code-block:: python

        import panel as pn
        import param

        class CounterButton(pn.custom.ReactComponent):

            value = param.Integer()

            _esm = """
            export function render({model}) {
            const [value, setValue] = model.useState("value");
            return (
                <button onClick={e => setValue(value+1)}>
                count is {value}
                </button>
            )
            }
            """

        CounterButton().servable()
    Tz18.3.1c                    | j                   j                  di       }dgdgd}t        d |j                         D              r|j	                  dgdgd       |S )	Nimportsz*React)
createRoot)reactzreact-dom/clientc              3  $   K   | ]  }d |v  
 ywz@muiNr1   r   r
  s     r=   r   z,ReactComponent._exports__.<locals>.<genexpr>       5$4qv{$4   createCache)CacheProvider)@emotion/cache@emotion/react)r   r&  r,  rX   rA   )r   rM  exportss      r=   r   zReactComponent._exports__  sd    ..$$Y3Z!0 1;
 5GNN$455NN#0/#5"6  rO   c                >    t         |   ||      }|dk(  rd|z   }|S )N)r   r   	compilingzimport * as React from "react"
)r2   r   )r   r   r   r   r<   s       r=   r   zReactComponent._render_esm  s.    g!8F!C{"4s:C
rO   c           
     R   | j                   j                  di       }| j                  }t        j                  rd\  }}ndx}}d| | d| | dd| d| dd| d	| | d
d}d| d| d}t        d |j                         D              r3|dz  }|j                  d| dd| d| d| d| dd| d| dd       |j                         D ]6  \  }}d|vr(d|v r$|j                  d      r|d d  d| d}n| d| }|||<   8 || j                   j                  di       dS )NrM  )z?devz&devr   zhttps://esm.sh/react@/zhttps://esm.sh/react-dom@z?deps=react@z&external=reactz&deps=react@z&external=react/)rO  zreact/z	react-domz
react-dom/zdeps=react@z,react-dom@z&external=react,react-domc              3  $   K   | ]  }d |v  
 ywrQ  r1   rR  s     r=   r   z4ReactComponent._process_importmap.<locals>.<genexpr>  rS  rT  z,react-is,@emotion/reactzhttps://esm.sh/react-is@z)https://esm.sh/@emotion/cache?deps=react@z)https://esm.sh/@emotion/react?deps=react@z&external=react,react-isz*https://esm.sh/@emotion/styled?deps=react@)zreact-isrW  rX  z@emotion/styledr   zesm.shz&path=/scopes)rM  r`  )
r   r&  _react_versionr   r   r,  rX   rA   r   r   )	r   rM  v_react
pkg_suffixpath_suffiximports_with_depssuffixr	  r
  s	            r=   r  z!ReactComponent._process_importmap  s   ..$$Y3$$&4#J'))J,WIj\B-gY{m1E4WI\'Rab5gYl7)T_S``pq	
 wi{7);TU5GNN$45500F$$6wiO$MgYVabiaj"k$MgYVabiaj  kC  #D%OPWyXcdkcl  mE  $F	&  MMODAq!|A::c?Sb6(!F873A#QvhA#$a  $ )nn((26
 	
rO   )rk   
ExportSpecr?  r@  )rp   rq   rr   rs   _ReactComponent__abstract_BkReactComponentr  ra  r   r   rE  r   r  rt   ru   s   @r=   r   r   {  sT    #J J$LN     
  
rO   r   c                       e Zd ZdZdZeZddZy)r   a  
    The `AnyWidgetComponent` allows you to create custom Panel components
    in the style of an AnyWidget component. Specifically this component
    type creates shims that make it possible to reuse AnyWidget ESM code
    as is, without having to adapt the callbacks to use Bokeh APIs.

    Reference: https://panel.holoviz.org/reference/custom_components/AnyWidgetComponent.html

    :Example:

    .. code-block:: python

        import param
        import panel as pn

        pn.extension()

        class CounterWidget(pn.custom.AnyWidgetComponent):
            _esm = """
            function render({ model, el }) {
            let count = () => model.get("value");
            let btn = document.createElement("button");
            btn.innerHTML = `count is ${count()}`;
            btn.addEventListener("click", () => {
                model.set("value", count() + 1);
                model.save_changes();
            });
            model.on("change:value", () => {
                btn.innerHTML = `count is ${count()}`;
            });
            el.appendChild(btn);
            }
            export default { render };
            """
            value = param.Integer()

        CounterWidget().servable()
    Tc                &    | j                  |       y)z
        Sends a custom event containing the provided message to the frontend.

        Parameters
        ----------
        msg: dict
        N)r8  )r9   r.  s     r=   sendzAnyWidgetComponent.send  s     	srO   N)r.  dict)rp   rq   rr   rs   _AnyWidgetComponent__abstract_BkAnyWidgetComponentr  rl  r1   rO   r=   r   r     s    %N J(LrO   r   )R
__future__r   r  r   r   r   r   r   r   r   collectionsr   collections.abcr   r   	functoolsr   typingr   r	   r
   r   r6   param.parameterizedr   r   io.datamodelr   io.resourcesr   io.stater   modelsr   ro  r   ri  r   rB  
models.esmr   r   models.reactive_htmlr   	pane.baser   reactiver   r   r   r   utilr   r   util.checksr    viewabler!   r"   r#   r$   r%   r&   widgets.baser'   bokeh.documentr(   bokeh.eventsr)   bokeh.modelr*   r+   pyviz_commsr,   rm  r   rD   r   rg  r.   rw   rI  r1   rO   r=   <module>r     s   "     	  
  # -    6  . 1   , *   0 )  %'", c4eCHo 5667JkH(J kH^
+ 
V7$0D V7r,+ ,^b
[ b
H4 4rO   