a
    Df4T                     @  s   d Z ddlmZ ddlZddlm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 ddlZddlZddlmZ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"m#Z# e	rddl$m%Z% ddl&m'Z' ddl(m)Z) G dd de ZG dd de!Z*dS )z1
Layout components to lay out objects in a grid.
    )annotationsN)
namedtuple)partial)TYPE_CHECKINGAnyClassVarDictListMappingOptionalTuple)FlexBoxGridBox   )
freeze_doc)hold)CDN_DIST   )	ListPanelPanel_col_row)Document)Model)Commc                      s   e Zd ZU dZejddddZejddddZeZ	de
d< d	Zd
e
d< ddiZde
d< dddZde
d< e dgZde
d< ed&ddZed'ddZdd Zd(ddZddddd d!d"d# fd$d%Z  ZS ))r   a2  
    The `GridBox` is a list-like layout (unlike `GridSpec`) that wraps objects
    into a grid according to the specified `nrows` and `ncols` parameters.

    It has a list-like API with methods to `append`, `extend`, `clear`,
    `insert`, `pop`, `remove` and `__setitem__`, which makes it possible to
    interactively update and modify the layout.

    Reference: https://panel.holoviz.org/reference/layouts/GridBox.html

    :Example:

    >>> pn.GridBox(
    ...    python_object_1, python_object_2, ...,
    ...    python_object_24, ncols=6
    ... )
    Nr   Nz0
      Number of rows to reflow the layout into.defaultZboundsdocz3
      Number of columns to reflow the layout into.ClassVar[Model]_bokeh_model zClassVar[Tuple[str, ...]]_linked_propertiesobjectschildren"ClassVar[Mapping[str, str | None]]_rename)Zscrollr#   _source_transformszcss/gridbox.cssClassVar[List[str]]_stylesheetsc           
   	     s   t dg dt dg d dd fddd	d
 d fdd	|||}g }|jD ]2\}}}}}	|d urd|||||| |	| f qd|S )NItem)layoutr0c0r1c1Grid)nrowsncolsitemsc                 S  s.   t | t | } }|dkr*|| |  } }q| S Nr   )abs)abr!   r!   ^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/layout/grid.pygcdN   s    z"GridBox._flatten_grid.<locals>.gcdc                   s    |D ]}| |  | | } q| S Nr!   )r6   restr7   )r9   r!   r8   lcmT   s    z"GridBox._flatten_grid.<locals>.lcmc                 S  s   | j dko| jdkS r4   r1   r2   )childr!   r!   r8   <lambda>Y       z'GridBox._flatten_grid.<locals>.<lambda>c              
     s  t ||d}t| trttt|| j}|s> ddg S dd |D  }|sftdd |D }g }d}|D ]R}||j }|j	D ]2\} }	}
}}|
| ||	 |
| || || f q||j7 }qr |||S t| trttt|| j}|s ddg S |stdd |D }dd |D  }g }d}|D ]V}||j }|j	D ]4\} }	}
}}|
| |	| ||
 || || f qP||j7 }q< |||S  dd| ddddgS d S )	Nr=   r   c                 S  s   g | ]
}|j qS r!   r1   .0r>   r!   r!   r8   
<listcomp>b   r@   z;GridBox._flatten_grid.<locals>._flatten.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r!   r2   rB   r!   r!   r8   rD   d   r@   c                 S  s   g | ]
}|j qS r!   rA   rB   r!   r!   r8   rD   w   r@   c                 S  s   g | ]
}|j qS r!   rE   rB   r!   r!   r8   rD   x   r@   r   )r   
isinstancer   listfiltermapr$   sumr1   r3   appendr2   r   )r+   r1   r2   Z	_flatten_r$   r3   offsetr>   Zfactorr,   r-   r.   r/   )r0   r*   _flattenr<   nonemptyr!   r8   rM   [   s@    

&
(z'GridBox._flatten_grid.<locals>._flatten)NN)r   r3   rK   )
clsr+   r1   r2   gridr$   r,   r-   r.   r/   r!   )r0   r*   rM   r9   r<   rN   r8   _flatten_gridI   s    -zGridBox._flatten_gridc                   sx   |dusdurTt  }du r2tt|| t fddtd|D }ndfdd	 }| ||S )z
        This is a copy of parts of the bokeh.layouts.grid implementation
        to avoid distributing non-filled columns.
        Nc                   s    g | ]}t  ||  qS r!   )r   )rC   i)r$   r2   r!   r8   rD      r@   z)GridBox._get_children.<locals>.<listcomp>r   c                   s>   t | tr6 d dkrtnt}| fdd| D S | S d S )Nr   r   c                   s   g | ]}| d  qS )r   r!   rB   )leveltraverser!   r8   rD      r@   z;GridBox._get_children.<locals>.traverse.<locals>.<listcomp>)rF   rG   r   r   )r$   rS   	container)rT   )rS   r8   rT      s    
z'GridBox._get_children.<locals>.traverse)r   )lenintmathceilr   rangerQ   )rO   r$   r1   r2   Nr+   r!   )r$   r2   rT   r8   _get_children   s    "zGridBox._get_childrenc                 C  s   d\}}|D ]f\}}}}}|j rB|j ds8|j drB|dM }nd}|j rn|j dsd|j drn|dM }qd}qg }|r|d |r|d |S )	N)TTZ_both_widthTFZ_heightzequal-widthzequal-height)sizing_modeendswithrK   )selfr$   Zequal_widthsZequal_heightsr>   _css_classesr!   r!   r8   _compute_css_classes   s    



zGridBox._compute_css_classesc                 C  s   |   }|p|}||f| j|jd < | |g |||\}}| || j| j}| |}	dd | |	 D }
|	|

dg  |
d< ||
d< |jf i |
 | || j||| |S )Nidc                 S  s   i | ]\}}|d vr||qS ))r2   r1   r!   rC   kvr!   r!   r8   
<dictcomp>   r@   z&GridBox._get_model.<locals>.<dictcomp>rb   r$   )r    _modelsref_get_objectsr\   r1   r2   rc   Z_get_propertiesr3   getupdateZ_link_propsr"   )r`   r   rootparentcommmodelr#   ra   r$   rb   
propertiesr!   r!   r8   
_get_model   s    
zGridBox._get_modelz$Dict[str, param.parameterized.Event]zDict[str, Any]r   r   zOptional[Comm]None)eventsmsgrn   rq   r   rp   returnc              
     s  ddl m} t|}t fdd|D } jd |v }	|	sLd|v sLd|v rd|v r`|d j}
n j}
 ||
|||\}} | j	 j
}|| jd < nd }t| dd	 | D }tj}d
t_zt||||	d tt |||||| |r"W d    W |t_W d    d S |jd }||jv rV|rV|j| d | | W d    n1 sl0    Y  W |t_n|t_0 W d    n1 s0    Y  d S )Nr   )statec                 3  s"   | ]} j || jv V  qd S r:   )r&   rl   _preprocess_params)rC   rf   r`   r!   r8   	<genexpr>   r@   z(GridBox._update_model.<locals>.<genexpr>r#   r2   r1   c                 S  s   i | ]\}}|d vr||qS )r=   r!   re   r!   r!   r8   rh      r@   z)GridBox._update_model.<locals>.<dictcomp>T)forcerd   r   )iorx   dictanyr&   oldr#   rk   r\   r1   r2   r   r3   r   Z_batch_updater   super_update_modelrj   Z_viewsZ_preprocess)r`   ru   rv   rn   rq   r   rp   rx   
preprocessZupdate_childrenr   r#   Z
old_modelsr$   rm   rj   	__class__rz   r8   r      s6    

:zGridBox._update_model)NN)NN)NNN)__name__
__module____qualname____doc__paramIntegerr1   r2   	BkGridBoxr    __annotations__r"   r&   r'   r   r)   classmethodrQ   r\   rc   rs   r   __classcell__r!   r!   r   r8   r       s$   
F
r   c                      s  e Zd ZU dZeji ddZejdg dddZej	dd	d
dZ
ej	dd	ddZeZded< dZded< dddddZded< dddZded< dgZded< e dgZded<  fddZejdd d!d"d# Zejd$d d!d%d& Zejdd d!d'd( Z fd)d*ZdFd+d,Z fd-d.Zed/d0 Zed1d2 Zed3d4 Z dGd5d6d7 fd8d9Z!ed:d; Z"d<d= Z#d>d? Z$d@dA Z%dBdC Z&dDdE Z'  Z(S )HGridSpeca  
    The `GridSpec` is an *array like* layout that allows arranging multiple Panel
    objects in a grid using a simple API to assign objects to individual grid cells or
    to a grid span.

    Other layout containers function like lists, but a GridSpec has an API similar
    to a 2D array, making it possible to use 2D assignment to populate, index, and slice
    the grid.

    See `GridStack` for a similar layout that allows the user to resize and drag the
    cells.

    Reference: https://panel.holoviz.org/reference/layouts/GridSpec.html

    :Example:

    >>> import panel as pn
    >>> gspec = pn.GridSpec(width=800, height=600)
    >>> gspec[:,   0  ] = pn.Spacer(styles=dict(background='red'))
    >>> gspec[0,   1:3] = pn.Spacer(styles=dict(background='green'))
    >>> gspec[1,   2:4] = pn.Spacer(styles=dict(background='orange'))
    >>> gspec[2,   1:4] = pn.Spacer(styles=dict(background='blue'))
    >>> gspec[0:1, 3:4] = pn.Spacer(styles=dict(background='purple'))
    >>> gspec
    z?
        The dictionary of child objects that make up the grid.)r   r   warn)r   erroroverridezM
        Whether to warn, error or simply override on overlapping assignment.)r   r#   r   Nr   z;
        Limits the number of columns that can be assigned.r   z8
        Limits the number of rows that can be assigned.r   r    r!   zClassVar[Tuple[str]]r"   r$   )r#   moder2   r1   r%   r&   r#   r   r'   r#   r(   ry   zcss/gridspec.cssr)   c                   sD   d|vri |d< t  jf i | d| _|   |   |   d S )Nr#   F)r   __init__	_updating_update_nrows_update_ncols_update_grid_sizer`   paramsr   r!   r8   r      s    zGridSpec.__init__r1   T)Zwatchc                 C  s   | j st| j| _d S r:   )r   boolr1   _rows_fixedrz   r!   r!   r8   r   )  s    zGridSpec._update_nrowsr2   c                 C  s   | j s| jd u| _d S r:   )r   r2   _cols_fixedrz   r!   r!   r8   r   .  s    zGridSpec._update_ncolsc                 C  s|   d| _ | js<dd | jD }|r(t|nt| jr6dnd| _| jsrdd | jD }|r^t|nt| jrldnd| _d| _ d S )NTc                 S  s    g | ]\}}}}|d ur|qS r:   r!   )rC   ra   x1r!   r!   r8   rD   7  r@   z.GridSpec._update_grid_size.<locals>.<listcomp>r   r   c                 S  s    g | ]\}}}}|d ur|qS r:   r!   )rC   ra   y1r!   r!   r8   rD   :  r@   F)r   r   r#   maxrV   r2   r   r1   )r`   Zmax_xidxZmax_yidxr!   r!   r8   r   3  s      zGridSpec._update_grid_sizec                   sP   t   }| jdvrLd|vr0d|v r0|d |d< d|vrLd|v rL|d |d< |S )NfixedNZ	min_widthwidthZ
min_heightheight)r   _init_paramsr^   r   r   r!   r8   r   >  s    

zGridSpec._init_paramsc                   s  ddl m} | jr&| jr&| j| j }nd}| jrD| jrD| j| j }nd}t| j }	t	|t
rlt| }|D ]}
|
|	vrp|
| qpg g  }}t| j D ]$\}\\}}}} |d u rdn|}|d u r| jn|}|d u rdn|}|d u r| jn|}|||| || f\}}}}i }| jdv rT|r<t|| |d< |rt|| |d< nP| j|d< d| jv r|rt|| |d< n"d| jv r|rt|| |d<  jjf i  fdd	| D   |v r j|jd
  \}}|| n|z ||||}W nf |yl } zL|jd ur4|j| ur4|d |_| ||	d | |||W  Y d }~  S d }~0 0 t	|trt|jdkr|jd jf i | n|jf i | ||||||f q||fS )Nr   )RerenderErrorr   r   r   r   r^   c                   s"   i | ]\}} j | js||qS r!   )r   readonlyre   objr!   r8   rh   q  s   z)GridSpec._get_objects.<locals>.<dictcomp>rd   r   )	pane.baser   r2   r   r1   r   rG   r#   valuesrF   r~   _cleanup	enumerater3   r^   rW   r   rm   ri   rj   rK   rs   r+   rk   	BkFlexBoxrV   r$   )r`   rq   Zold_objectsr   rn   rp   r   r   r   Zcurrent_objectsr   r$   Zold_childrenrR   y0x0r   r   rchwrr   r>   ra   er!   r   r8   rk   G  s^    

$
$
4zGridSpec._get_objectsc                   s   dd |D }t  ||S )Nc                 S  s   g | ]\}}}}}|qS r!   r!   )rC   r>   ra   r!   r!   r8   rD     r@   z1GridSpec._compute_sizing_mode.<locals>.<listcomp>)r   _compute_sizing_mode)r`   r$   propsr   r!   r8   r     s    zGridSpec._compute_sizing_modec                 C  s2   dd | j D }|r.t|t| j kr.t|S dS )Nc                 S  s    g | ]\}}}}|d ur|qS r:   r!   )rC   ra   r   r!   r!   r8   rD     r@   z%GridSpec._xoffset.<locals>.<listcomp>r   r#   rV   min)r`   Zmin_xidxr!   r!   r8   _xoffset  s    zGridSpec._xoffsetc                 C  s2   dd | j D }|r.t|t| j kr.t|S dS )Nc                 S  s    g | ]\}}}}|d ur|qS r:   r!   )rC   r   r   ra   r!   r!   r8   rD     r@   z%GridSpec._yoffset.<locals>.<listcomp>r   r   )r`   Zmin_yidxr!   r!   r8   _yoffset  s    zGridSpec._yoffsetc                 C  s   t j| j| jfd td}| j D ]\\}}}}}|d u r>dn|}|d u rP| jn|}|d u r`dn|}	|d u rr| jn|}
t|	|
D ].}t||D ]}||||f|fh|||f< qqq"|S )NZdtyper   )npfullr1   r2   objectr#   r3   rZ   )r`   rP   r   r   r   r   r   lr   tr7   yxr!   r!   r8   _object_grid  s     zGridSpec._object_gridzModel | Nonert   )rn   rw   c                   s*   t  | | j D ]}|| qd S r:   )r   r   r#   r   )r`   rn   pr   r!   r8   r     s    zGridSpec._cleanupc                 C  sJ   t j| j| jfdd}| jD ](\}}}}|||||f  d7  < q|S )Nuint8r   r   )r   zerosr1   r2   r#   )r`   rP   r   r   r   r   r!   r!   r8   rP     s    zGridSpec.gridc                 K  s@   t | j fi |}| js"|d= | js.|d= t| f i |S )z
        Makes a copy of the GridSpec sharing the same parameters.

        Arguments
        ---------
        params: Keyword arguments override the parameters on the clone.

        Returns
        -------
        Cloned GridSpec object
        r2   r1   )r~   r   r   r   r   type)r`   r   r   r!   r!   r8   clone  s    zGridSpec.clonec                 c  s   | j  D ]
}|V  q
d S r:   )r#   r   )r`   r   r!   r!   r8   __iter__  s    zGridSpec.__iter__c                 C  s   t |tr|\}}n|td  }}| j||f }t |tjrTtdd | D }nt|d d g}|D ]}| j	|= qj| j
d d S )Nc                 S  s   g | ]}t |d  qS r   rG   rC   or!   r!   r8   rD     r@   z(GridSpec.__delitem__.<locals>.<listcomp>r   r#   )rF   tupleslicer   r   ndarrayr~   flattenrG   r#   r   trigger)r`   indexyidxxidxsubgridZdeletedkeyr!   r!   r8   __delitem__  s    


zGridSpec.__delitem__c                 C  s  t |tr|\}}n|td  }}| j||f }t |tjrtdd | D }| j|d}|j	|j
 }}g }	|j D ]z\\}
}}}}|
d ur|
|8 }
|d ur||8 }|d ur||8 }|d ur||8 }|
|||f|f|	vr||	|
|||f|f q|t|	|_|jt| j }|jt| j }|jr:t|j| |_|jrRt|j| |_|jrjt|j| |_|jrt|j| |_|S t|d d S d S )Nc                 S  s   g | ]}t |d  qS r   r   r   r!   r!   r8   rD     r@   z(GridSpec.__getitem__.<locals>.<listcomp>)r#   r   r   )rF   r   r   r   r   r   r~   r   r   r   r   r#   r3   rK   r2   floatr1   r   rW   r   	max_widthZ
max_heightrG   )r`   r   r   r   r   r#   ZgspecZxoffZyoffadjustedr   r   r   r   r   Zwidth_scaleZheight_scaler!   r!   r8   __getitem__  s:    


zGridSpec.__getitem__c              	   C  s  ddl m} t|tstd|\}}t|tr@|j|j }}n||d  }}t|trh|j|j }}	n||d  }}	|d u rdn|}
|d u r| jn|}|d u rdn|}|	d u r| j	n|	}| j
r|
| jks|| jkrtd| j d| jr|| j	ks|| j	krtd| j	 d|||	|f}|| jv }| jt| jd	d
}|sf|||j|< |  |j}n"|j}||||
|f  d7  < |dk}t| j}| rd}g }tt| D ]\\}}z| ||f }W n ty   Y qY n0 ||vr|| |d|||f 7 }qd| d t|d }| jdkrHt|n| jdkr`| j| | j| }t|tr|rt|d d gng }ndd | D }t|D ],}z
||= W n t y   Y qY n0 q||||< || _d S )Nr   )panelz/Must supply a 2D index for GridSpec assignment.r   r   z]Assigned object to column(s) out-of-bounds of the grid declared by `ncols`. which was set to .z]Assigned object to column(s) out-of-bounds of the grid declared by `nrows`, which was set to r   r    z    (%d, %d): %s

zNSpecified region overlaps with the following existing object(s) in the grid:

zQThe following shows a view of the grid (empty: 0, occupied: 1, overlapping: 2):

r   r   r   c                 S  s   g | ]}t |d  d  qS r   r   r   r!   r!   r8   rD   >  r@   z(GridSpec.__setitem__.<locals>.<listcomp>)!r   r   rF   r   
IndexErrorr   startstopr2   r1   r   r   r#   r   r~   r   rP   r   zipr   where	ExceptionrK   strZastyper   r   warningr   setrG   r   KeyError)r`   r   r   r   r   r   r   r   r   r   r   r   r   r7   r   overlapr   rP   Zoverlap_gridZnew_objectsZoverlappingr#   Zold_objZoverlap_textr   Zdkeyr!   r!   r8   __setitem__  s    



 








zGridSpec.__setitem__)N)N))r   r   r   r   r   r   r#   ZObjectSelectorr   r   r2   r1   r   r    r   r"   r&   r'   ry   r   r)   r   dependsr   r   r   r   rk   r   propertyr   r   r   r   rP   r   r   r   r   r   r   r!   r!   r   r8   r      sJ   
	



	
@


	
"r   )+r   
__future__r   rX   collectionsr   	functoolsr   typingr   r   r   r   r	   r
   r   r   numpyr   r   Zbokeh.modelsr   r   r   r   Zio.documentr   Zio.modelr   Zio.resourcesr   baser   r   r   r   Zbokeh.documentr   Zbokeh.modelr   Zpyviz_commsr   r   r!   r!   r!   r8   <module>   s&   ( J