a
    oDf5                     @  s  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rddl#m$Z$ ej%Z%dd Z&G dd deZ'G dd de(Z)e'* Z+e+j,dddZ-g dZ.d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)IterableMapping)	Parametergetcallargsgetfullargspec	signature)TYPE_CHECKINGClassVar   )ColumnPanelRow)HTMLPaneBasepanel)ReplacementPane)Viewable)ButtonWidget)fixedwidget)Modelc           
      c  s   | j }| j}| j}| j}|ttf}|tjtjfv r||v rF||}nH|turdt	j
j	dt |}n*|tur|}t|ttfrt|}n|V  |||fV  n6|tjkr|  D ]\}}	|| ||	tfV  qd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v r0   [/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/interact.py"_yield_abbreviations_for_parameter$   s.    




r2   c                      s   e Zd ZU ejeeddZejdddZ	ej
ddZejedZdd	iZd
ed< i f fdd	Zd,ddZejdddddddd Zedd Zdd Zd-ddd fddZed d! Zd"d# Zd$d% Zd&d' Zed(d) Zed*d+ Z  ZS ).interactiveF)r   class_Zis_instancez:
        Whether to update manually by clicking on button.)r   docRun Interact)r   )r4   _paneNz"ClassVar[Mapping[str, str | None]]_renamec                   sN  t d u rtdt j|fi | |dd| _| |}zt| W n tyZ   Y n0 t	|fi dd |D  | 
|}| jr|dt| jdf t|| _| jf i | j}t|tr|| _d| _nt|| jd| _d| _t| j| _d	d
 |D }d|v r |dtd| j  t| | _| j| jg| j_ | !  | "  d S )Nz`interact requires either recent Python version (>=3.3 or IPython to inspect function signatures.	throttledFc                 S  s   i | ]\}}}||qS r0   r0   ).0nr/   _r0   r0   r1   
<dictcomp>`       z(interactive.__init__.<locals>.<dictcomp>manual)r   Tc                 S  s   g | ]\}}t |tr|qS r0   )r%   r   )r:   r<   r   r0   r0   r1   
<listcomp>n   r>   z(interactive.__init__.<locals>.<listcomp>r   r   z<h2>%s</h2>)#r   ImportErrorsuper__init__r    r9   find_abbreviationscheck_argspec	TypeErrorr   widgets_from_abbreviationsmanual_updateappendr   manual_namedict_widgetsobjectr*   r%   r   r7   	_internalr   r   r   _inner_layoutinsertr   r   Z
widget_boxlayoutZobjects_link_widgets_sync_layout)selfrM   paramsr*   
new_kwargswidgetspane	__class__r0   r1   rC   N   s8    





zinteractive.__init__c                 C  s   | j ||||S N)rO   
_get_model)rT   r5   rootparentZcommr0   r0   r1   r\   z   s    zinteractive._get_modelz_pane.sizing_modez_pane.width_policyz_pane.height_policyT)watchc                 C  sH   t | dsd S dd | jj  D }| jj| | jj| d S )NrO   c                 S  s   i | ]\}}|d v r||qS ))Zsizing_modeZwidth_policyZheight_policyr0   r:   r.   r/   r0   r0   r1   r=      s   z,interactive._sync_layout.<locals>.<dictcomp>)hasattrr7   r!   valuesr(   rO   updaterQ   )rT   optsr0   r0   r1   rS   }   s    
zinteractive._sync_layoutc                 C  s   g S r[   r0   rT   r0   r0   r1   _synced_params   s    zinteractive._synced_paramsc                   s    j rd jd fg}n
 j }|D ]X\}} fdd} jrPt|drPd}nd}|dkr`dn|}|j||} j| q&d S )Nr?   c                   sN    j f i  j}t| j j\}}|d u r4d S | _| jd< | _d S )Nr   )rM   r*   r   Z_update_from_objectr7   rN   rO   )ZchangeZ
new_objectZnew_paneZinternalre   r0   r1   update_pane   s    

z.interactive._link_widgets.<locals>.update_paneZvalue_throttledr-   Zclicks)	rH   rL   r(   r9   ra   r!   r_   Z_internal_callbacksrI   )rT   rW   r   
widget_objrg   r/   ZpnameZwatcherr0   re   r1   rR      s    
zinteractive._link_widgetszModel | NoneNone)r]   returnc                   s   | j | t | d S r[   )rO   _cleanuprB   )rT   r]   rY   r0   r1   rk      s    zinteractive._cleanupc                 C  s   dd | j  D S )Nc                 S  s    i | ]\}}|d kr||j qS )r?   )r-   )r:   r.   r   r0   r0   r1   r=      s   z&interactive.kwargs.<locals>.<dictcomp>)rL   r(   re   r0   r0   r1   r*      s    zinteractive.kwargsc                 C  s
   t | jS r[   )r   rM   re   r0   r0   r1   r      s    zinteractive.signaturec              	   C  s   g }z|   }W n( ttfy8   dd | D  Y S 0 |j D ]@}t||D ]0\}}}|tu rrtd||	|||f qRqD|S )zqFind the abbreviations for the given function and kwargs.
        Return (name, abbrev, default) tuples.
        c                 S  s   g | ]\}}|||fqS r0   r0   )r:   keyr-   r0   r0   r1   r@      r>   z2interactive.find_abbreviations.<locals>.<listcomp>z5cannot find widget or abbreviation for argument: {!r})
r   
ValueErrorrF   r(   
parametersrb   r2   r   formatrI   )rT   r*   rV   sigr)   r   r-   r   r0   r0   r1   rD      s    zinteractive.find_abbreviationsc                 C  sr   g }|D ]d\}}}t |tr"|}nt|||d}t |ts^t |ts^|du rPqntdt|||f q|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   rF   ro   rI   )rT   seqresultr   Zabbrevr   rh   r0   r0   r1   rG      s    
z&interactive.widgets_from_abbreviationsc                 C  s   t |tjS r[   )r%   typesFunctionType)clsrM   r0   r0   r1   applies   s    zinteractive.appliesc                 C  s   t ddd}t| |S )NFr6   rH   rJ   )rK   _InteractFactory)ru   optionsr0   r0   r1   factory   s    zinteractive.factory)NNN)N) __name__
__module____qualname__r!   ZClassSelectorr   r   Zdefault_layoutBooleanrH   StringrJ   r   r7   r8   __annotations__rC   r\   dependsrS   propertyrf   rR   rk   r*   r   rD   rG   classmethodrv   rz   __classcell__r0   r0   rY   r1   r3   @   s0   
,




r3   c                   @  s4   e Zd ZdZdddZdd ZdddZd	d
 ZdS )rx   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                 C  s   || _ || _|pi | _d S r[   ru   rd   r*   )rT   ru   ry   r*   r0   r0   r1   rC      s    z_InteractFactory.__init__c                 C  s   | 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   )rT   fr0   r0   r1   r      s    z_InteractFactory.widgetc                   s   |rjt tjt| j}|fdd| D  t| jfi fdd| D }t| | j	||}  }|du rz| S dt
|jv rtd| |}z
||_W n$ ty    fdd}||_Y n0 |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                   s   i | ]\}}| vr||qS r0   r0   r`   rU   r0   r1   r=   C  r>   z-_InteractFactory.__call__.<locals>.<dictcomp>c                   s   i | ]\}}| v r||qS r0   r0   r`   r   r0   r1   r=   D  r>   Nr9   z1A function cannot have "throttled" as an argumentc                    s    | i |S r[   r0   )argsr*   )_InteractFactory__interact_fr0   r1   <lambda>_  r>   z+_InteractFactory.__call__.<locals>.<lambda>)listr3   r!   rK   r*   rc   r(   rd   typeru   rE   r   rm   r   AttributeErrorrQ   )rT   r   r*   kwrd   r   wr0   )r   rU   r1   __call__  s$    2

$

z_InteractFactory.__call__c                 K  sJ   t | j}|D ]&}||vr(td||| ||< q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})rK   rd   rm   ro   r   ru   r*   )rT   kwdsrd   r.   r0   r0   r1   ry   c  s    
z_InteractFactory.options)N)N)r{   r|   r}   __doc__rC   r   r   ry   r0   r0   r0   r1   rx      s
   

Urx   Tr6   rw   )interactinteract_manualr3   )/r   
__future__r   rs   collections.abcr   r   inspectr   r   r   rE   r   typingr	   r
   r!   rQ   r   r   r   rX   r   r   r   Z	pane.baser   Zviewabler   rW   r   r   Zwidgets.widgetr   r   Zbokeh.modelr   r   r2   r3   rM   rx   rz   r   ry   r   __all__r0   r0   r0   r1   <module>   s.   
 , 	