o
    ^i<                     @   s   d 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
 ddlmZ ddlmZm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iZeeG dd deZeeG dd deeZeeG dd deeZeeG dd deeZdS )z7Implement common widgets layouts as reusable components    N)defaultdict)InstanceBoolUnicodeCUnicodeCaselessStrEnumTuple)Integer)	HasTraits
TraitError)observevalidate   )Widget)GridBox)	doc_subststyle_paramsa  

    grid_gap : str
        CSS attribute used to set the gap between the grid cells

    justify_content : str, in ['flex-start', 'flex-end', 'center', 'space-between', 'space-around']
        CSS attribute used to align widgets vertically

    align_items : str, in ['top', 'bottom', 'center', 'flex-start', 'flex-end', 'baseline', 'stretch']
        CSS attribute used to align widgets horizontally

    width : str
    height : str
        width and heightc                       s   e Zd ZdZeddddZeg ddddZeg ddd	dZeddd
dZ	eddd
dZ
 fddZdd Zdd Zdd Z  ZS )LayoutPropertiesaK  Mixin class for layout templates

    This class handles mainly style attributes (height, grid_gap etc.)

    Parameters
    ----------

    {style_params}


    Note
    ----

    This class is only meant to be used in inheritance as mixin with other
    classes. It will not work, unless `self.layout` attribute is defined.

    NTzThe grid-gap CSS attribute.)
allow_nonehelp)
flex-startflex-endcenterzspace-betweenzspace-aroundz"The justify-content CSS attribute.)topbottomr   r   r   baselinestretchzThe align-items CSS attribute.zThe width CSS attribute.c                    s@   t  jdi | tt| _ddd| jd< |   |   d S )Nr   r   )r   r   align_items )super__init__r   dict_property_rewrite_copy_layout_props_set_observersselfkwargs	__class__r   ^/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/ipywidgets/widgets/widget_templates.pyr    O   s   
zLayoutProperties.__init__c                 C   s6   |d |d }}| j | ||}t| j|| dS )z=delegate the trait types to their counterparts in self.layoutnewnameN)r"   getsetattrlayout)r&   changevaluer,   r   r   r*   _delegate_to_layoutW   s   z$LayoutProperties._delegate_to_layoutc                 C   s   t  }| | j| dS )z<set observers on all layout properties defined in this classN)r   class_trait_namesr   r2   )r&   _propsr   r   r*   r$   ]   s   zLayoutProperties._set_observersc                 C   sD   t  }|D ]}t| |}|r| j| ||}t| j|| qd S N)r   r3   getattrr"   r-   r.   r/   )r&   r4   propr1   r   r   r*   r#   b   s   
z#LayoutProperties._copy_layout_props)__name__
__module____qualname____doc__r   grid_gapr   justify_contentr   widthheightr    r2   r$   r#   __classcell__r   r   r(   r*   r   "   s<    r   c                	       s   e Zd ZdZeeddZeeddZeeddZeeddZ	eeddZ
ee e e g ddZee e e g ddZeddZ fddZed	d
 Zdd Zdd Zedddddddddd Z  ZS )	AppLayouta"   Define an application like layout of widgets.

    Parameters
    ----------

    header: instance of Widget
    left_sidebar: instance of Widget
    center: instance of Widget
    right_sidebar: instance of Widget
    footer: instance of Widget
        widgets to fill the positions in the layout

    merge: bool
        flag to say whether the empty positions should be automatically merged

    pane_widths: list of numbers/strings
        the fraction of the total layout width each of the central panes should occupy
        (left_sidebar,
        center, right_sidebar)

    pane_heights: list of numbers/strings
        the fraction of the width the vertical space that the panes should occupy
         (left_sidebar, center, right_sidebar)

    {style_params}

    Examples
    --------

    Tr   )1fr2frrC   default_value)rC   3frrC   c                       t  jdi | |   d S Nr   r   r    _update_layoutr%   r(   r   r*   r          zAppLayout.__init__c                 C   s2   t d| r| S t d| r| d S td| )Nz\d+\.?\d*(px|fr|%)$z
\d+\.?\d*$frzqthe pane sizes must be in one of the following formats: '10px', '10fr', 10 (will be converted to '10fr').Got '{}')rematch	TypeErrorformat)sizer   r   r*   _size_to_css   s   zAppLayout._size_to_cssc                 C   s   t t| j|S r5   )listmaprS   )r&   	size_listr   r   r*   _convert_sizes   s   zAppLayout._convert_sizesc           
      C   s  g dg dg dg}|  | j}|  | j}| j| j| j| j| jd}dd | D }|s1d S | D ]\}}||j	_
q5| jrt|dkr^t| d }|||g|||g|||gg}n`| jd u rn|D ]}|d= qe|d= | jd u r}|d d |d d< | jd u r|d d	 |d d
< | jd u r| jd u r| jd u rdgdgg}dg}ddg}| jd u r|d= |d= | jd u r|d
= |d
= ddd |D }	d|| j	_d|| j	_|	| j	_t| | _d S )N)headerrX   rX   )left-sidebarr   right-sidebar)footerr[   r[   )rX   r[   rY   rZ   r   c                 S      i | ]\}}|d ur||qS r5   r   .0positionchildr   r   r*   
<dictcomp>   
    z,AppLayout._update_layout.<locals>.<dictcomp>r   r   rX   r[   rC   
c                 s        | ]}d  d|V  qdS z"{}" NrQ   joinr^   liner   r   r*   	<genexpr>       z+AppLayout._update_layout.<locals>.<genexpr>rh   )rW   pane_widthspane_heightsrX   r[   left_sidebarright_sidebarr   itemsr/   	grid_areamergelenrT   keysrj   grid_template_columnsgrid_template_rowsgrid_template_areastuplevalueschildren)
r&   rz   rx   ry   all_childrenr}   r_   r`   rowgrid_template_areas_cssr   r   r*   rK      sj   









zAppLayout._update_layoutr[   rX   r   rq   rr   ru   ro   rp   c                 C      |    d S r5   rK   r&   r0   r   r   r*   _child_changed   s   zAppLayout._child_changed)r8   r9   r:   r;   r   r   rX   r[   rq   rr   r   r   r   ro   rp   r   ru   r    staticmethodrS   rW   rK   r   r   r@   r   r   r(   r*   rA   l   s.     


FrA   c                       s`   e Zd ZdZe Ze Zd fdd	Zedddd Z	d	d
 Z
dd Zdd Zdd Z  ZS )GridspecLayouta   Define a N by M grid layout

    Parameters
    ----------

    n_rows : int
        number of rows in the grid

    n_columns : int
        number of columns in the grid

    {style_params}

    Examples
    --------

    >>> from ipywidgets import GridspecLayout, Button, Layout
    >>> layout = GridspecLayout(n_rows=4, n_columns=2, height='200px')
    >>> layout[:3, 0] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[1:, 1] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[-1, 0] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[0, 1] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout
    Nc                    sd   t  jdi | | _| _ fddt jD  _d jf  _d jf  _i  _d _	d S )Nc                    s   g | ]}d g j  qS ).)	n_columns)r^   ir&   r   r*   
<listcomp>  s    z+GridspecLayout.__init__.<locals>.<listcomp>zrepeat(%d, 1fr)r   r   )
r   r    n_rowsr   range_grid_template_areas_grid_template_rows_grid_template_columns	_children	_id_count)r&   r   r   r'   r(   r   r*   r      s   
zGridspecLayout.__init__r   r   c                 C   s   |d dkr
|d S t d)Nr1   r   z-n_rows and n_columns must be positive integer)r   )r&   proposalr   r   r*   _validate_integer#  s   z GridspecLayout._validate_integerc                 C   sn   t |tr|| j\}}}t|||}n|g}t |tr0|| j\}}}t|||}||fS |g}||fS )zDconvert a two-dimensional slice to a list of rows and column indices)
isinstancesliceindicesr   r   r   )r&   r   columnstartstopstriderowscolumnsr   r   r*   _get_indices_from_slice)  s   

z&GridspecLayout._get_indices_from_slicec           	      C   s   |\}}|  j d7  _ d| j  }||j_| ||\}}|D ]"}|D ]}| j| | }|dkr8|| jv r8| j|= || j| |< q"q|| j|< |   d S )Nr   z
widget%03dr   )r   r/   rt   r   r   r   rK   )	r&   keyr1   r   r   obj_idr   r   current_valuer   r   r*   __setitem__:  s   

zGridspecLayout.__setitem__c                 C   sV   | j | \}}d }|D ]}|D ]}| j| | }|p|}||kr$tdqq| j| S )NzTThe slice spans several widgets, but only a single widget can be retrieved at a time)r   r   rP   r   )r&   r   r   r   r   r   r   
new_obj_idr   r   r*   __getitem__L  s   
zGridspecLayout.__getitem__c                 C   sF   d dd | jD }| j| j_| j| j_|| j_t| j	
 | _d S )Nre   c                 s   rf   rg   ri   rk   r   r   r*   rm   ]  rn   z0GridspecLayout._update_layout.<locals>.<genexpr>)rj   r   r   r/   rx   r   ry   rz   r{   r   r|   r}   )r&   r   r   r   r*   rK   [  s   


zGridspecLayout._update_layout)NN)r8   r9   r:   r;   r	   r   r   r    r   r   r   r   r   rK   r@   r   r   r(   r*   r      s    
r   c                       sz   e Zd ZdZeeddZeeddZeeddZeeddZ	e
ddZ fddZdd Zed	d
ddddd Z  ZS )TwoByTwoLayouta   Define a layout with 2x2 regular grid.

    Parameters
    ----------

    top_left: instance of Widget
    top_right: instance of Widget
    bottom_left: instance of Widget
    bottom_right: instance of Widget
        widgets to fill the positions in the layout

    merge: bool
        flag to say whether the empty positions should be automatically merged

    {style_params}

    Examples
    --------

    >>> from ipywidgets import TwoByTwoLayout, Button
    >>> TwoByTwoLayout(top_left=Button(description="Top left"),
    ...                top_right=Button(description="Top right"),
    ...                bottom_left=Button(description="Bottom left"),
    ...                bottom_right=Button(description="Bottom right"))

    TrB   rE   c                    rH   rI   rJ   r%   r(   r   r*   r      rL   zTwoByTwoLayout.__init__c                 C   s  ddgddgg}| j | j| j| jd}dd | D }|s d S | D ]\}}||j_q$| jrt|dkrGt	|
 d	 }||g||gg}n\d
dg}t|D ]S\}}|d| |d| }	}
|d d }|	d u r|
d u r|d	 | |d	 |< |d | |d |< qO|	d u r|d | |d	 |< qO|
d u r|d	 | |d |< qOddd |D }d| j_d| j_|| j_t| | _d S )Ntop-left	top-rightbottom-leftbottom-right)r   r   r   r   c                 S   r\   r5   r   r]   r   r   r*   ra     rb   z1TwoByTwoLayout._update_layout.<locals>.<dictcomp>r   r   leftrightztop-zbottom-   re   c                 s   rf   rg   ri   rk   r   r   r*   rm     rn   z0TwoByTwoLayout._update_layout.<locals>.<genexpr>z1fr 1fr)top_left	top_rightbottom_leftbottom_rightrs   r/   rt   ru   rv   rT   rw   	enumerater-   rj   rx   ry   rz   r{   r|   r}   )r&   rz   r~   r}   r_   r`   r   r   r   r   r   i_neighbourr   r   r   r*   rK     sP   

zTwoByTwoLayout._update_layoutr   r   r   r   ru   c                 C   r   r5   r   r   r   r   r*   r     s   zTwoByTwoLayout._child_changed)r8   r9   r:   r;   r   r   r   r   r   r   r   ru   r    rK   r   r   r@   r   r   r(   r*   r   f  s    
4r   )r;   rN   collectionsr   	traitletsr   r   r   r   r   r   r	   r
   r   r   r   widgetr   
widget_boxr   docutilsr   _doc_snippetsr   rA   r   r   r   r   r   r*   <module>   s*     I k