
    hMhS                        d Z ddlmZmZ ddlmZ ddlmZm	Z	 ddlm
Z
 ddlmZ ddlZddlmZ d	d
lmZmZmZmZmZmZmZmZmZmZmZmZ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)m*Z* e	j+        Z+d Z,d Z-d Z.ddZ/d Z0 G d de          Z1 G d d          Z2e13                                Z4e45                    dd          Z6 G d de"          Z7dS )z&Interact with functions using widgets.    )IterableMapping)EnumMeta)	signature	Parameter)getcallargs)getfullargspecN)get_ipython   )WidgetValueWidgetTextFloatSlider	FloatText	IntSliderIntTextCheckboxDropdownVBoxButton	DOMWidgetOutput)displayclear_output)	HasTraitsAnyUnicodeobserve)RealIntegral)warnc                      dt           j        vrdS 	 ddl} ddlm} n# t
          $ r Y dS w xY w|                                 dk    s|                                 dk    r |             dS dS )aR  Show matplotlib plots immediately if using the inline backend.

    With ipywidgets 6.0, matplotlib plots don't work well with interact when
    using the inline backend that comes with ipykernel. Basically, the inline
    backend only shows the plot after the entire cell executes, which does not
    play well with drawing plots inside of an interact function. See
    https://github.com/jupyter-widgets/ipywidgets/issues/1181/ and
    https://github.com/ipython/ipython/issues/10376 for more details. This
    function displays any matplotlib plots if the backend is the inline backend.
    
matplotlibNr   )flush_figuresz'module://ipykernel.pylab.backend_inlinez)module://matplotlib_inline.backend_inline)sysmodulesr#    matplotlib_inline.backend_inliner$   ImportErrorget_backend)mplr$   s     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/ipywidgets/widgets/interaction.pyshow_inline_matplotlib_plotsr,      s     3;&&    BBBBBBB    	FFFHHH 	IHs   
 
++c                      t                       fd}                                D ]\  }}|                    |d           t                        |d           S )a  Connect widget controls to a function.

    This function does not generate a user interface for the widgets (unlike `interact`).
    This enables customisation of the widget user interface layout.
    The user interface layout must be defined and displayed manually.
    c                     d                                  D             }t                       5  t          d            di | t                       d d d            d S # 1 swxY w Y   d S )Nc                 $    i | ]\  }}||j         S  value).0kvs      r+   
<dictcomp>z8interactive_output.<locals>.observer.<locals>.<dictcomp>?   s     888!!AG888    Twaitr0   )itemsr,   r   )changekwargscontrolsfouts     r+   observerz$interactive_output.<locals>.observer>   s    88x~~'7'7888$&&& 	+ 	+d####AKKKKK(***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   'A$$A(+A(r2   N)r   r:   r   r,   )r>   r=   r@   r4   wr?   s   ``   @r+   interactive_outputrB   5   s     ((C+ + + + + + + ~~ % %!			(G$$$$ """HTNNNJr7   c                     t          |           t          |          k    sdS t          | |          }t          d |D                       S )z'Match a pattern of types in a sequence.Fc              3   <   K   | ]\  }}t          ||          V  d S N)
isinstance)r3   objkinds      r+   	<genexpr>z_matches.<locals>.<genexpr>Q   s0      ::Dz#d##::::::r7   )lenzipall)opatterncompss      r+   _matchesrP   L   sH    q66S\\!!u'NNE::E::::::r7   c                 *   |[| |$t          d                    | ||                    || z
  }| |dz  z   }t          |t          |                    s| |dz  z   }n`t          |t                    st          d|z            |dk    r||dz   f}n|dk    r	| d|z  f}nd|z  | f}| |d         } ||d         }|t          || z
  |z            }| ||z  z   }| |cxk    r|k    s&n t          d                    | ||                    | ||fS )	z=Return min, max, value given input values with possible None.Nz/unable to infer range, value from: ({}, {}, {})   zexpected a real number, got: %rr   r      z<value must be between min and max (min={}, value={}, max={}))
ValueErrorformatrF   typer   	TypeErrorint)minmaxr2   stepdiffvrangeticks          r+   _get_min_max_valuer_   T   st    };#+NUUVY[^`effgggSytax %d,, 	&419%E%&& 	G=EFFFA::UQY'FFQYYfag&FFgv&F;)C;)CECK4'((dTk!%3W^^_bdiknoopppU?r7   c              #     K   | j         }| j        }| j        }|t          t          f}|t          j        t          j        fv rB||v r|                    |          }n|t          ur|}n| j        r| j        }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.N)namerH   defaultemptyr   POSITIONAL_OR_KEYWORDKEYWORD_ONLYpop
annotationVAR_KEYWORDcopyr:   )	paramr<   ra   rH   rb   	not_foundr2   r4   r5   s	            r+   "_yield_abbreviations_for_parameterrl   v   s     :D:DmGue$I	/1GHHH6>>JJt$$EEE!!EE 	$EEOOOUG$$$$$$	&	&	&KKMM'')) 	 	DAqJJqMMMQ+	 
'	&	 	r7   c                       e Zd ZdZi fdZd Zd Zd Zd Ze	e
fd            Zed             Zed	             Zed
             Zed             Ze	d             ZdS )interactivea8  
    A VBox container containing a group of interactive widgets tied to a
    function.

    Parameters
    ----------
    __interact_f : function
        The function to which the interactive widgets are tied. The `**kwargs`
        should match the function signature.
    __options : dict
        A dict of options. Currently, the only supported keys are
        ``"manual"`` (defaults to ``False``), ``"manual_name"`` (defaults
        to ``"Run Interact"``) and ``"auto_display"`` (defaults to ``False``).
    **kwargs : various, optional
        An interactive widget is created for each keyword argument that is a
        valid widget abbreviation.

    Note that the first two parameters intentionally start with a double
    underscore to avoid being mixed up with keyword arguments passed by
    ``**kwargs``.
    c                 ^   t          j        | dg           d | _        g | _        i | _        |x| _        }|                    dd          | _        |                    dd          | _	        |                    dd          | _
        |                    d	d          | _        |                     |          }	 t          |           t          |fi d
 |D              n# t          $ r Y nw xY w|                     |          | _        d | j        D             }| j	        r4t%          | j
                  | _        |                    | j                   t+                      | _        |                    | j                   || _        | j	        rc| j                            | j                   | j        D ]:}t5          |t6                    r#d|_        |                    | j        d           ;d S | j        D ]}|                    | j        d           |                                  d S )Nzwidget-interact)_dom_classesr   TmanualFmanual_nameRun Interactauto_displayc                     i | ]	\  }}}||
S r0   r0   )r3   nr5   _s       r+   r6   z(interactive.__init__.<locals>.<dictcomp>   s     :::ea!a:::r7   c                 <    g | ]}t          |t                    |S r0   )rF   r   )r3   rA   s     r+   
<listcomp>z(interactive.__init__.<locals>.<listcomp>   s'    HHH1z!Y/G/GHQHHHr7   )descriptionr2   )names)r   __init__resultargsr<   r>   rf   r   getrq   rr   rt   find_abbreviationscheck_argspecr   rW   widgets_from_abbreviationskwargs_widgetsr   manual_buttonappendr   r?   childrenon_clickupdaterF   r   continuous_updater   )	self_interactive__interact_f_interactive__optionsr<   r>   
new_kwargscrA   widgets	            r+   r|   zinteractive.__init__   sI   d*;)<====	!!"JJ~t<<mmHe44$==GG%MM.%@@,,V44
	<!
 ;;::z:::;;;;	  	 	 	D	 #==jII
 IH+HHH ; 	)!'D4D!E!E!EDHHT'(((88	 ; 	''444 ( : :a&& :*/A'IIdkI999: :
 - ; ;t{'::::KKMMMMMs   8C 
C,+C,c                    i | _         | j        rd| j        _        	 t	                       | j        5  | j        rt          d           | j        D ]%}|                                }|| j         |j	        <   & | j
        di | j         | _        t	                       | j        r| j        t          | j                   ddd           n# 1 swxY w Y   nY# t          $ rL}t                      }|| j                            d|d           n|                                 Y d}~nd}~ww xY w| j        rd| j        _        dS dS # | j        rd| j        _        w xY w)z
        Call the interact function and update the output widget with
        the result of the function call.

        Parameters
        ----------
        *args : ignored
            Required for this method to be used as traitlets callback.
        Tr8   Nz"Exception in interact callback: %s)exc_infoFr0   )r<   rq   r   disabledr,   r?   r   r   get_interact_value_kwargr>   r}   rt   r   	Exceptionr
   logwarningshowtraceback)r   r~   r   r2   eips         r+   r   zinteractive.update   s    ; 	/*.D'	4(*** 	) 	)$ , d++++"1 7 7F"5577E16DK..$df33t{33,...$ ))@DK(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)  	# 	# 	#Bz  !EqSW XXXX  """	# { 4.3"+++4 4t{ 4.3"+3333sT   C BC	=C 	CC CC E 
D+AD&!E &D++E Ec                 *    t          | j                  S rE   )r   r>   r   s    r+   r   zinteractive.signature  s       r7   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 r0   r0   )r3   keyr2   s      r+   ry   z2interactive.find_abbreviations.<locals>.<listcomp>  s#    KKKZS%c5%(KKKr7   z5cannot find widget or abbreviation for argument: {!r})
r   rT   rW   r:   
parametersvaluesrl   rc   rU   r   )r   r<   r   sigrj   ra   r2   rb   s           r+   r   zinteractive.find_abbreviations  s     
	L..""CCI& 	L 	L 	LKK6<<>>KKKKKK	L ^**,, 	: 	:E(J5RX(Y(Y : :$eWE>>$%\%c%cdh%i%ijjj!!4"89999: s    /A
Ac                    g }|D ]\  }}}t          |t                    r7t          |t                    s"t          d                    |                    |                     ||          }|"t          d                    |                    t          |d          r|j        s||_        ||_	        |
                    |           |S )zQGiven a sequence of (name, abbrev, default) tuples, return a sequence of Widgets.z{!r} is not a ValueWidgetNz&{!r} cannot be transformed to a widgetrz   )rF   r   r   rW   rU   widget_from_abbrevrT   hasattrrz   r   r   )r   seqr}   ra   abbrevrb   r   s          r+   r   z&interactive.widgets_from_abbreviations  s    %( 		" 		"!D&'&&)) L:fk3R3R L ; B B6 J JKKK,,VW==F~ !I!P!PQW!X!XYYY6=11 *9K *%)" FMMM&!!!!r7   c                 $   t          |t                    st          |t                    r|S t          |t                    r9|                     |          }|t
          ur	 ||_        n# t          $ r Y nw xY w|S t          |t                    r| 	                    |          }||S | 
                    |          }||S t          |t                    r9|                     |          }|t
          ur	 ||_        n# t          $ r Y nw xY w|S dS )z=Build a ValueWidget instance given an abbreviation or Widget.N)rF   r   fixedtuplewidget_from_tuplerc   r2   r   rV   widget_from_annotationwidget_from_single_valuer   widget_from_iterable)clsr   rb   r   s       r+   r   zinteractive.widget_from_abbrev'  sQ    fk** 	j.G.G 	Mfe$$ 	**622Fe###*FLL    D M fd## 	//77F! --f55M fh'' 	--f55Fe###*FLL    D M ts$   !A) )
A65A66C> >
D
Dc                    t          | t                    rt          t          |                     S t          | t                    rt	          |           S t          | t
                    r't          dd|           \  }}}t          | ||          S t          | t                    r't          dd|           \  }}}t          | ||          S dS )zIMake widgets from single values, which can be used as parameter defaults.r1   Nr2   rY   rZ   )
rF   strr   boolr   r    r_   r   r   r   )rM   rY   rZ   r2   s       r+   r   z$interactive.widget_from_single_valueQ  s     a 	c!ff%%%%4   		!$$$$8$$ 	0tQ??OCe1#377774   	0tQ??OCeQCS99994r7   c                 8   | t           u rt                      S | t          u rt                      S | t          t
          hv rt                      S | t          t          hv rt                      S t          | t                    rt          d | D                       S dS )z=Make widgets from type annotation and optional default value.c                     i | ]
}|j         |S r0   )ra   )r3   options     r+   r6   z6interactive.widget_from_annotation.<locals>.<dictcomp>m  s    $I$I$IVV[&$I$I$Ir7   optionsN)r   r   r   r   rX   r    r   floatr   r   rF   EnumTyper   )ts    r+   r   z"interactive.widget_from_annotationa  s     8866M$YY::3/!!995$-;;8$$ 	$I$Iq$I$I$IJJJJ4r7   c                     t          | t          t          f          rVt          | d         | d                   \  }}}t          d | D                       rt          }nt
          } ||||          S t          | t          t          t          f          ry| d         }|dk    rt          d|z            t          | d         | d         |          \  }}}t          d | D                       rt          }nt
          } |||||	          S d
S )z'Make widgets from a tuple abbreviation.r   r   c              3   @   K   | ]}t          |t                    V  d S rE   rF   r    r3   rw   s     r+   rI   z0interactive.widget_from_tuple.<locals>.<genexpr>v  ,      66q:a**666666r7   r   rR   zstep must be >= 0, not %r)r[   c              3   @   K   | ]}t          |t                    V  d S rE   r   r   s     r+   rI   z0interactive.widget_from_tuple.<locals>.<genexpr>  r   r7   )r2   rY   rZ   r[   N)rP   r   r_   rL   r   r   rT   )rM   rY   rZ   r2   r   r[   s         r+   r   zinteractive.widget_from_tupleq  s,    Ad|$$ 	A01qt<<OCe66A66666 "!3U5555a$d+,, 		AQ4Dqyy !<t!CDDD01qt$GGGOCe66A66666 "!3U4@@@@		A 		Ar7   c                    t          | t          t          f          rt          |           S t          | t                    r/t          t          |                                                     S t          t          |                     S )zUMake widgets from an iterable. This should not be done for
        a string or tuple.r   )rF   listdictr   r   r:   )rM   s    r+   r   z interactive.widget_from_iterable  ss     a$&& 	-A&&&&7## 	-DOO4444DGG,,,,r7   c                 F    t          ddd          }t          | |          S )NFTrs   )rq   rt   rr   )r   _InteractFactory)r   r   s     r+   factoryzinteractive.factory  s&    e$NSSSW---r7   N)__name__
__module____qualname____doc__r|   r   r   r   r   classmethodrc   r   staticmethodr   r   r   r   r   r0   r7   r+   rn   rn      s#        * 02 8 8 8 8v!4 !4 !4H! ! !  &   05 ' ' ' ['R   \   \ A A \A( 
- 
- \
- . . [. . .r7   rn   c                   0    e Zd ZdZi fdZd ZddZd ZdS )r   a>  
    Factory for instances of :class:`interactive`.

    This class is needed to support options like::

        >>> @interact.options(manual=True)
        ... def greeting(text="World"):
        ...     print("Hello {}".format(text))

    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.
    c                 0    || _         || _        || _        d S rE   r   optsr<   )r   r   r   r<   s       r+   r|   z_InteractFactory.__init__  s    	r7   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   r>   s     r+   r   z_InteractFactory.widget  s$      tx4944444r7   Nc                 `   |rMt          | j                  }|                    |            t          |           | j        | j        |          } }|| S |                     |          }	 ||_        n# t          $ r fd}||_        Y nw xY wt                       t          |           |S )ap  
        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="Jupyter 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="Jupyter 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))
        Nc                       | i |S rE   r0   )r~   r<   _InteractFactory__interact_fs     r+   <lambda>z+_InteractFactory.__call__.<locals>.<lambda>  s    d(Ef(E(E r7   )
r   r<   r   rV   r   r   r   AttributeErrorr,   r   )r   r   r<   kwr>   rA   s    `    r+   __call__z_InteractFactory.__call__  s    z  	7dk""BIIf4::dh	266D9 K KKNN	AHH 	 	 	 FEEEAAHHH		
 	%&&&


s   -A5 5BBc                    t          | j                  }|D ]G}	 ||         }n0# t          $ r# t          d                    |                    w xY w||         ||<   H 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   r   KeyErrorrT   rU   rV   r   r<   )r   kwdsr   r4   rw   s        r+   r   z_InteractFactory.options$  s     DI 	 	AB G B B B !6!=!=a!@!@AAAB1gDGGtDzz$(D$+666s	   #-ArE   )r   r   r   r   r|   r   r   r   r0   r7   r+   r   r     sn         ( -/    
5 5 5$\ \ \ \|7 7 7 7 7r7   r   Trs   )rq   rr   c                   Z     e Zd ZdZ ed          Z edd          Z fdZd Z	 xZ
S )r   zDA pseudo-widget whose value is fixed and never synced to the client.zAny Python object)help c                 >     t                      j        dd|i| d S )Nr2   r0   )superr|   )r   r2   r<   	__class__s      r+   r|   zfixed.__init__A  s+    //u//////r7   c                     | j         S )zReturn the value for this widget which should be passed to
        interactive functions. Custom widgets can change this method
        to process the raw value ``self.value``.
        r1   r   s    r+   r   zfixed.get_interact_valueC  s    
 zr7   )r   r   r   r   r   r2   r   rz   r|   r   __classcell__)r   s   @r+   r   r   =  su        NNC()))E'"#6777K0 0 0 0 0      r7   r   )NN)8r   collections.abcr   r   enumr   r   inspectr   r   r   r	   r   r%   IPythonr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   IPython.displayr   r   	traitletsr   r   r   r   numbersr   r    warningsr!   rc   r,   rB   rP   r_   rl   rn   r   r   interactr   interact_manualr   r0   r7   r+   <module>r      s   - , - - - - - - - - % % % % % % ( ( ( ( ( ( ( (       3 3 3 3 3 3 



      / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 2 1 1 1 1 1 1 1 6 6 6 6 6 6 6 6 6 6 6 6 " " " " " " " "       	  4  .; ; ;       D  .J. J. J. J. J.$ J. J. J.Z\7 \7 \7 \7 \7 \7 \7 \7~   ""$N"KK    I     r7   