
    e!6                       d Z ddlmZ ddlZddlm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
j'        Z'd Z( G d de          Z) G d de*          Z+e),                                Z-e-.                    dd          Z/g dZ0dS )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)OrderedDict)IterableMapping)	Parametergetcallargsgetfullargspec	signature)TYPE_CHECKINGClassVar   )ColumnPanelRow)HTMLPaneBasepanel)ReplacementPane)Viewable)ButtonWidget)fixedwidget)Modelc              #  z  K   | j         }| j        }| j        }| j        }|t          t          f}|t
          j        t
          j        fv r||v r|                    |          }nq|t          ur-t          j
        j	                            dt                     |}n;|t          ur.|}t          |t          t          f          rt!          |          }n|V  |||fV  dS |t
          j        k    rM|                                                                D ](\  }}	|                    |           ||	t          fV  'dS dS )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.11/site-packages/panel/interact.py"_yield_abbreviations_for_parameterr4   %   s]     >D>D

CGue$I	/1GHHH6>>JJt$$EEJ$$ &]^pr r rEEE!!E%(G!455 %eOOOUG$$$$$$	&	&	&KKMM'')) 	 	DAqJJqMMMQ+	 
'	&	 	    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   )r8   _paneNz"ClassVar[Mapping[str, str | None]]_renamec                   t           t          d           t                      j        |fi | |                    dd          | _        |                     |          }	 t          |           t          |fi d |D              n# t          $ r Y nw xY w| 
                    |          }| j        r*|                    dt          | j                  f           t          |          | _         | j        di | j        }t'          |t(                    r|| _        d| _        n"t/          || j                  | _        d| _        t3          | j                  | _        d |D             }d	|v r+|                    d
t9          d| j        z                       t;          | | _        | j        | j        g| j        _         | !                                 | "                                 d S )Nz`interact requires either recent Python version (>=3.3 or IPython to inspect function signatures.	throttledFc                    i | ]	\  }}}||
S  rA   ).0nr2   _s       r3   
<dictcomp>z(interactive.__init__.<locals>.<dictcomp>a   s     "?"?"?51Qq1Q"?"?"?r5   manual)r   Tc                B    g | ]\  }}t          |t                    |S rA   )r(   r   )rB   rD   r   s      r3   
<listcomp>z(interactive.__init__.<locals>.<listcomp>o   s+    QQQiaj6P6PQ6QQQr5   r   r   z<h2>%s</h2>rA   )#r
   ImportErrorsuper__init__r#   r?   find_abbreviationscheck_argspecr   	TypeErrorwidgets_from_abbreviationsmanual_updateappendr   manual_namer   _widgetsobjectr-   r(   r   r<   	_internalr   r   r   _inner_layoutinsertr   r   
widget_boxlayoutobjects_link_widgets_sync_layout)selfrT   paramsr-   
new_kwargswidgetspane	__class__s          r3   rK   zinteractive.__init__O   s    R S S S 	**6***K77,,V44
	A&!!!
 @@"?"?J"?"?"?@@@@	  	 	 	D	 11*== 	FNNHf$2B&C&C&CDEEE#G,,t{))T[))dH%% 	"DJ"DNNt$)444DJ!DN __QQ7QQQVNN1d=49#<==>>> '*#0BCs   #B
 

BBc                <    | j                             ||||          S N)rV   
_get_model)r]   r:   rootparentcomms        r3   re   zinteractive._get_model{   s    !,,S$EEEr5   z_pane.sizing_modez_pane.width_policyz_pane.height_policyT)watchc                   t          | d          sd S d | j        j                                                                        D             }| j        j                            |           | j        j                            |           d S )NrV   c                "    i | ]\  }}|d v 	||S ))sizing_modewidth_policyheight_policyrA   )rB   r1   r2   s      r3   rE   z,interactive._sync_layout.<locals>.<dictcomp>   s4     
 
 
QDDD qDDDr5   )hasattrr<   r$   valuesr+   rV   updaterY   )r]   optss     r3   r\   zinteractive._sync_layout~   s    t_-- 	F
 
!Z-4466<<>>
 
 
 	 ''---  &&&&&r5   c                    g S rd   rA   r]   s    r3   _synced_paramszinteractive._synced_params   s    	r5   c                :     j         rd j        d         fg}n j                                        }|D ]e\  }} fd} j        rt	          |d          rd}nd}|dk    rdn|}|j                            ||          } j                            |           fd S )NrF   c                     j         di j        }t          j        |j        j                  \  }}|d S |_        |j        d<   |_        d S )Nr   rA   )rT   r-   r   _update_from_objectr<   rU   rV   )change
new_objectnew_paneinternalr]   s       r3   update_panez.interactive._link_widgets.<locals>.update_pane   sl    (T[774;77
%4%H
DN& &"( #F &
(0"1%!)r5   value_throttledr0   clicks)	rP   rS   r+   r?   ro   r$   ri   _internal_callbacksrQ   )r]   r`   r   
widget_objr}   r2   pnamewatchers   `       r3   r[   zinteractive._link_widgets   s     	, $-"9:;GGm))++G ' 	5 	5D** * * * * ~ '*6G"H"H % $ 0 0HHaE &,,[%@@G$++G4444-	5 	5r5   rf   Model | NonereturnNonec                ~    | j                             |           t                                          |           d S rd   )rV   _cleanuprJ   )r]   rf   rb   s     r3   r   zinteractive._cleanup   s8    ##D)))r5   c                H    d | j                                         D             S )Nc                0    i | ]\  }}|d k    ||j         S )rF   )r0   )rB   r1   r   s      r3   rE   z&interactive.kwargs.<locals>.<dictcomp>   s0     " " "IAv== 6< ==r5   )rS   r+   rt   s    r3   r-   zinteractive.kwargs   s0    " "1D1D1F1F " " " 	"r5   c                *    t          | j                  S rd   )r
   rT   rt   s    r3   r
   zinteractive.signature   s    %%%r5   c                   g }	 |                                  }n5# t          t          f$ r! d |                                D             cY S w xY w|j                                        D ]\}t          ||          D ]I\  }}}|t          u r"t          d                    |                    |	                    |||f           J]|S )zqFind the abbreviations for the given function and kwargs.
        Return (name, abbrev, default) tuples.
        c                    g | ]
\  }}|||fS rA   rA   )rB   keyr0   s      r3   rH   z2interactive.find_abbreviations.<locals>.<listcomp>   s#    KKKZS%c5%(KKKr5   z5cannot find widget or abbreviation for argument: {!r})
r
   
ValueErrorrN   r+   
parametersrp   r4   r    formatrQ   )r]   r-   r_   sigr,   r   r0   r   s           r3   rL   zinteractive.find_abbreviations   s     
	L..""CCI& 	L 	L 	LKK6<<>>KKKKKK	L ..00 	: 	:I(J9V\(](] : :$eWE>>$%\%c%cdh%i%ijjj!!4"89999: s    /A
Ac                F   g }|D ]\  }}}t          |t                    r|}nt          |||          }t          |t                    s?t          |t                    s*|]t	          d                    t                              |                    ||f           |S )zQGiven a sequence of (name, abbrev, default) tuples, return a sequence of Widgets.)r   r   Nz{!r} is not a ValueWidget)r(   r   r   r   rN   r   rQ   )r]   seqresultr   abbrevr   r   s          r3   rO   z&interactive.widgets_from_abbreviations   s    %( 
	. 
	.!D&'&%(( H#

#FwGGG
z622 PjU6S6S P%#$?$F$Fv$N$NOOOMM4,----r5   c                6    t          |t          j                  S rd   )r(   typesFunctionType)clsrT   s     r3   applieszinteractive.applies   s    &%"4555r5   c                D    t          dd          }t          | |          S )NFr;   rP   rR   )dict_InteractFactory)r   optionss     r3   factoryzinteractive.factory   s$    UGGGW---r5   )NNNrd   )rf   r   r   r   ) __name__
__module____qualname__r$   ClassSelectorr   r   default_layoutBooleanrP   StringrR   r   r<   r=   __annotations__rK   re   dependsr\   propertyru   r[   r   r-   r
   rL   rO   classmethodr   r   __classcell__)rb   s   @r3   r7   r7   A   s        (U(5:< < <N "EM% 6= > > >M %,~666KEx000E3:D/GAAAA&( & & & & & &XF F F F U]7/1EG\dhiii' ' ji'   X5 5 5<       " " X"& & &  $    6 6 [6 . . [. . . . .r5   r7   c                  .    e Zd ZdZddZd ZddZd ZdS )r   ax  
    Factory for instances of :class:`interactive`.

    Arguments
    ---------
    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                4    || _         || _        |pi | _        d S rd   r   rr   r-   )r]   r   r   r-   s       r3   rK   z_InteractFactory.__init__   s    	lr5   c                4     | 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   )r]   fs     r3   r   z_InteractFactory.widget   s$     tx4944444r5   c                @   |rt          t          j                  t          | j                  }|                    fd|                                D                        t          | j        fi fd|                                D             } t          |           | j	        ||          } }|| S dt          |          j        v rt          d          |                     |          }	 ||_        n# t          $ r fd}||_        Y nw xY w|j        S )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))
        c                $    i | ]\  }}|v	||S rA   rA   rB   r1   r2   r^   s      r3   rE   z-_InteractFactory.__call__.<locals>.<dictcomp>D  s$    JJJ1!6//q!///r5   c                $    i | ]\  }}|v 	||S rA   rA   r   s      r3   rE   z-_InteractFactory.__call__.<locals>.<dictcomp>E  s$    %U%U%Utq!far5   Nr?   z1A function cannot have "throttled" as an argumentc                      | i |S rd   rA   )argsr-   _InteractFactory__interact_fs     r3   <lambda>z+_InteractFactory.__call__.<locals>.<lambda>`  s    d(Ef(E(E r5   )listr7   r$   r   r-   rq   r+   rr   typer   rM   r   r   r   AttributeErrorrY   )r]   r   r-   kwrr   r   wr^   s    `     @r3   __call__z_InteractFactory.__call__  sA   d  	2++,,Fdk""BIIJJJJJJJKKK	VV%U%U%U%Uv||~~%U%U%UVVD4::dhb11D9 KM!,,111PQQQ KKNN	AHH 	 	 	 FEEEAAHHH		
 xs   5C= =DDc                    t          | j                  }|D ]3}||vr"t          d                    |                    ||         ||<   4 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 {!r})r   rr   r   r   r   r   r-   )r]   kwdsrr   r1   s       r3   r   z_InteractFactory.optionsd  ss     DI 	 	A}} !6!=!=a!@!@AAA1gDGGtDzz$(D$+666r5   rd   )r   r   r   __doc__rK   r   r   r   rA   r5   r3   r   r      si         # # # #
5 5 5S S S Sj7 7 7 7 7r5   r   Tr;   r   )interactinteract_manualr7   )1r   
__future__r   r   collectionsr   collections.abcr   r   inspectr   r   r	   rM   r
   typingr   r   r$   rY   r   r   r   ra   r   r   r   	pane.baser   viewabler   r`   r   r   widgets.widgetr   r   bokeh.modelr   r    r4   r7   rT   r   r   r   r   r   __all__rA   r5   r3   <module>r      s  	 	 # " " " " "  # # # # # # - - - - - - - -            + * * * * * * *  & & & & & & & & & & ' ' ' ' ' ' ' ' ' ' & & & & & &       # # # # # # # # ) ) ) ) ) ) ) ) "!!!!!!  8h. h. h. h. h.( h. h. h.VE7 E7 E7 E7 E7v E7 E7 E7P   "">"RR 9
8
8r5   