
    ;g5                    b   d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
ZmZ ddlmZm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 ddlmZ ddlmZmZ ddl m!Z!m"Z" erddl#m$Z$ ejJ                  Z%d Z& G d de      Z' G d d      Z(e'jS                         Z*e*jW                  dd      Z,g dZ-y)aL  
Interact with functions using widgets.

The interact Pane implemented in this module mirrors
ipywidgets.interact in its API and implementation. Large parts of the
code were copied directly from ipywidgets:

Copyright (c) Jupyter Development Team and PyViz Development Team.
Distributed under the terms of the Modified BSD License.
    )annotationsN)IterableMapping)	Parametergetcallargsgetfullargspec	signature)TYPE_CHECKINGClassVar   )ColumnPanelRow)HTMLPanepanel)ReplacementPane)Viewable)Button
WidgetBase)fixedwidget)Modelc              #    K   | j                   }| j                  }| j                  }| j                  }|t        t        f}|t
        j                  t
        j                  fv r||v r|j                  |      }ni|t        ur1t        j                  j                  j                  dt               |}n0|t        ur$|}t        |t        t        f      rt!        |      }n| ||f y|t
        j"                  k(  rC|j%                         j'                         D ]!  \  }}	|j                  |       ||	t        f # yyw)z-Get an abbreviation for a function parameter.zUsing function annotations to implicitly specify interactive controls is deprecated. Use an explicit keyword argument for the parameter instead.N)namekind
annotationdefaultemptyr   POSITIONAL_OR_KEYWORDKEYWORD_ONLYpopparammainwarningDeprecationWarning
isinstancer   r   r   VAR_KEYWORDcopyitems)
	parameterkwargsr   r   annr   	not_foundvaluekvs
             .lib/python3.12/site-packages/panel/interact.py"_yield_abbreviations_for_parameterr3   $   s    >>D>>D


CGue$I	//1G1GHH6>JJt$EJJ$$ &]^prEE!E%(G!45eOUG$$	&&	&KKM'')DAqJJqMQ+ * 
's   D>E c                  |    e Zd ZU  ej                  eed      Z ej                  dd      Z	 ej                  d      Z ej                  e      Zdd	iZd
ed<   i f fd	ZddZ ej$                  ddddd      d        Zed        Zd Zdd fdZed        Zd Zd Zd Zed        Zed        Z xZS ) interactiveF)r   class_is_instancez:
        Whether to update manually by clicking on button.)r   docRun Interact)r   )r6   _paneNz"ClassVar[Mapping[str, str | None]]_renamec                   t         t        d      t        |   |fi | |j	                  dd      | _        | j                  |      }	 t        |       t        |fi |D ci c]	  \  }}}|| c}}} | j                  |      }| j                  r'|j                  dt        | j                        f       t        |      | _         | j"                  di | j$                  }	t'        |	t(              r|	| _        d| _        n#t/        |	| j0                        | _        d| _        t3        | j*                        | _        |D 
cg c]  \  }}
t'        |
t6              s|
 }}}
d|v r)|j9                  dt;        d	| j0                   d
             t=        | | _        | j>                  | j4                  g| j@                  _!        | jE                          | jG                          y c c}}}w # t        $ r Y w xY wc c}
}w )Nz`interact requires either recent Python version (>=3.3 or IPython to inspect function signatures.	throttledFmanual)r   Tr   r   z<h2>z</h2> )$r	   ImportErrorsuper__init__r"   r=   find_abbreviationscheck_argspecr   	TypeErrorwidgets_from_abbreviationsmanual_updateappendr   manual_namedict_widgetsobjectr,   r'   r   r:   	_internalr   r   r   _inner_layoutr   insertr   r   
widget_boxlayoutobjects_link_widgets_sync_layout)selfrL   paramsr,   
new_kwargsnr1   _widgetspaner   	__class__s              r2   rB   zinteractive.__init__N   s    R S S 	*6*K7,,V4
	A&!
 @J"?J51Qq1Q3J"?@11*=NNHf$2B2B&CDEWt{{)T[[)dH%DJ"DNt$))4DJ!DN _+2U7iaj6T67UVNN1dT$))E#:;< '*#0B0BC) #@	  		$ Vs$   G4 %G-H$H4	H Hc                >    | j                   j                  ||||      S N)rN   
_get_model)rU   r8   rootparentcomms        r2   r_   zinteractive._get_modelz   s    !!,,S$EE    z_pane.sizing_modez_pane.width_policyz_pane.height_policyT)watchc                P   t        | d      sy | j                  j                  j                         j	                         D ci c]  \  }}|dv r|| }}}| j
                  j                  j                  |       | j                  j                  j                  |       y c c}}w )NrN   )sizing_modewidth_policyheight_policy)hasattrr:   r#   valuesr*   rN   updaterQ   )rU   r0   r1   optss       r2   rT   zinteractive._sync_layout}   s    t_-!ZZ--446<<>
>TQDD qD> 	 
 	  ''-  &
s   B"c                    g S r^   r?   rU   s    r2   _synced_paramszinteractive._synced_params   s    	rc   c                P     j                   rd j                  d   fg}n j                  j                         }|D ]g  \  }} fd} j                  rt	        |d      rd}nd}|dk(  rdn|}|j
                  j                  ||      } j                  j                  |       i y )Nr>   c                     j                   di j                  }t        j                  |j                  j
                        \  }}|y |_        |j                  d<   |_        y )Nr   r?   )rL   r,   r   _update_from_objectr:   rM   rN   )change
new_objectnew_paneinternalrU   s       r2   update_panez.interactive._link_widgets.<locals>.update_pane   si    (T[[74;;7
%4%H%H

DNN&"( # &
(0""1%!)rc   value_throttledr/   clicks)	rG   rK   r*   r=   ri   r#   rd   _internal_callbacksrH   )rU   rZ   r   
widget_objrw   r1   pnamewatchers   `       r2   rS   zinteractive._link_widgets   s     $--"9:;Gmm))+G 'D** ~~'*6G"H% $ 0HaE &&,,[%@G$$++G4- !(rc   c                Z    | j                   j                  |       t        |   |       y r^   )rN   _cleanuprA   )rU   r`   r\   s     r2   r   zinteractive._cleanup   s$    ##D)rc   c                    | j                   j                         D ci c]  \  }}|dk7  r||j                   c}}S c c}}w )Nr>   )rK   r*   r/   )rU   r0   r   s      r2   r,   zinteractive.kwargs   sG    151D1D1F "1FIAv= 6<<1F " 	" "s   >c                ,    t        | j                        S r^   )r	   rL   rn   s    r2   r	   zinteractive.signature   s    %%rc   c                l   g }	 | j                         }|j                  j                         D ]A  }t        ||      D ]0  \  }}}|t        u rt        d|      |j                  |||f       2 C |S # t        t        f$ r0 |j                         D cg c]
  \  }}|||f nc c}}w c}}cY S w xY w)zqFind the abbreviations for the given function and kwargs.
        Return (name, abbrev, default) tuples.
        z1cannot find widget or abbreviation for argument: )	r	   
ValueErrorrE   r*   
parametersrj   r3   r   rH   )	rU   r,   rW   sigkeyr/   r+   r   r   s	            r2   rC   zinteractive.find_abbreviations   s     
	L.."C
 ..0I(J9V\(]$eWE>$'XY]X`%abb!!4"89 )^ 1
  I& 	L;A<<>K>ZS%c5%(>KK	Ls   A4 4"B3B&%B32B3c                    g }|D ]o  \  }}}t        |t              r|}nt        |||      }t        |t              s%t        |t              s|Kt	        t        d      |j                  ||f       q |S )zQGiven a sequence of (name, abbrev, default) tuples, return a sequence of Widgets.)r   r   z is not a ValueWidget)r'   r   r   r   rE   rH   )rU   seqresultr   abbrevr   r{   s          r2   rF   z&interactive.widgets_from_abbreviations   s}    %(!D&'&%(#
#FwG
z:6*ZQV:W%#vj0E$FGGMM4,- &) rc   c                6    t        |t        j                        S r^   )r'   typesFunctionType)clsrL   s     r2   applieszinteractive.applies   s    &%"4"455rc   c                4    t        dd      }t        | |      S )NFr9   rG   rI   )rJ   _InteractFactory)r   optionss     r2   factoryzinteractive.factory   s    UGW--rc   )NNNr^   )r`   zModel | NonereturnNone) __name__
__module____qualname__r#   ClassSelectorr   r   default_layoutBooleanrG   StringrI   r   r:   r;   __annotations__rB   r_   dependsrT   propertyro   rS   r   r,   r	   rC   rF   classmethodr   r   __classcell__)r\   s   @r2   r5   r5   @   s   (U((5:<N "EMM% 6= >M %,,~6KEx0E3:D/G/A&( &XF U]]7/1EG\dhi' j'  5< " "&$  6 6 . .rc   r5   c                  ,    e Zd ZdZddZd ZddZd Zy)r   az  
    Factory for instances of :class:`interactive`.

    Parameters
    ----------
    cls: class
      The subclass of :class:`interactive` to construct.
    options: dict
      A dict of options used to construct the interactive
      function. By default, this is returned by
      ``cls.default_options()``.
    kwargs: dict
      A dict of **kwargs to use for widgets.
    Nc                6    || _         || _        |xs i | _        y r^   r   rl   r,   )rU   r   r   r,   s       r2   rB   z_InteractFactory.__init__   s    	lrc   c                R     | j                   || j                  fi | j                  S )a  
        Return an interactive function widget for the given function.
        The widget is only constructed, not displayed nor attached to
        the function.
        Returns
        -------
        An instance of ``self.cls`` (typically :class:`interactive`).
        Parameters
        ----------
        f : function
            The function to which the interactive widgets are tied.
        r   )rU   fs     r2   r   z_InteractFactory.widget   s$     txx499444rc   c           
        |rt        t        j                        }t        | j                        }|j                  |j                         D ci c]  \  }}||vs|| c}}       t        | j                  fi |j                         D ci c]  \  }}||v s|| c}}} t        |       | j                  ||      } }|| S dt        |      j                  v rt        d      | j                  |      }		 |	|_        |	j                  S c c}}w c c}}w # t        $ r fd}|	|_        Y |	j                  S w xY w)ae  
        Make the given function interactive by adding and displaying
        the corresponding :class:`interactive` widget.
        Expects the first argument to be a function. Parameters to this
        function are widget abbreviations passed in as keyword arguments
        (``**kwargs``). Can be used as a decorator (see examples).
        Returns
        -------
        f : __interact_f with interactive widget attached to it.
        Parameters
        ----------
        __interact_f : function
            The function to which the interactive widgets are tied. The `**kwargs`
            should match the function signature. Passed to :func:`interactive()`
        **kwargs : various, optional
            An interactive widget is created for each keyword argument that is a
            valid widget abbreviation. Passed to :func:`interactive()`
        Examples
        --------
        Render an interactive text field that shows the greeting with the passed in
        text::
            # 1. Using interact as a function
            def greeting(text="World"):
                print("Hello {}".format(text))
            interact(greeting, text="IPython Widgets")
            # 2. Using interact as a decorator
            @interact
            def greeting(text="World"):
                print("Hello {}".format(text))
            # 3. Using interact as a decorator with named parameters
            @interact(text="IPython Widgets")
            def greeting(text="World"):
                print("Hello {}".format(text))
        Render an interactive slider widget and prints square of number::
            # 1. Using interact as a function
            def square(num=1):
                print("{} squared is {}".format(num, num*num))
            interact(square, num=5)
            # 2. Using interact as a decorator
            @interact
            def square(num=2):
                print("{} squared is {}".format(num, num*num))
            # 3. Using interact as a decorator with named parameters
            @interact(num=5)
            def square(num=2):
                print("{} squared is {}".format(num, num*num))
        r=   z1A function cannot have "throttled" as an argumentc                      | i |S r^   r?   )argsr,   _InteractFactory__interact_fs     r2   <lambda>z+_InteractFactory.__call__.<locals>.<lambda>_  s    d(Ef(Erc   )listr5   r#   rJ   r,   rk   r*   rl   typer   rD   r   r   r   AttributeErrorrQ   )
rU   r   r,   rV   kwr0   r1   rl   r   ws
    `        r2   __call__z_InteractFactory.__call__  s,   d +++,Fdkk"BIIJ1!6/q!tJK		Vv||~%U~tq!fad~%UVD4:dhhb1D9 KM!,111PQQ KKN	AH xx= K%U0  	 FAAHxx	s*   D
D
DD>D E ?E c                    t        | j                        }|D ]  }||vrt        d|      ||   ||<     t        |       | j                  || j
                        S )z
        Change options for interactive functions.
        Returns
        -------
        A new :class:`_InteractFactory` which will apply the
        options when called.
        zinvalid option )rJ   rl   r   r   r   r,   )rU   kwdsrl   r0   s       r2   r   z_InteractFactory.optionsc  s`     DIIA} ?1%!8991gDG  tDz$((D$++66rc   r^   )r   r   r   __doc__rB   r   r   r   r?   rc   r2   r   r      s    #
5Sj7rc   r   Tr9   r   )interactinteract_manualr5   ).r   
__future__r   r   collections.abcr   r   inspectr   r   r   rD   r	   typingr
   r   r#   rQ   r   r   r   r[   r   r   r   	pane.baser   viewabler   rZ   r   r   widgets.widgetr   r   bokeh.modelr   r   r3   r5   r   r   r   r   r   __all__r?   rc   r2   <module>r      s   	 #  -  +  & & # # &  ' )!8h.$ h.VE7 E7P  "">"R 9rc   