
    Xh<                     J   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e             Z ee       G d dee             Z ee       G d dee             Z ee       G d dee             Zy)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                        e Zd ZdZ eddd      Z eg ddd      Z eg ddd	      Z eddd
      Z	 eddd
      Z
 fdZd Zd Zd Z x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                     t        |   di | t        t              | _        ddd| j                  d<   | j                          | j                          y )Nr   r   )r   r   align_items )super__init__r   dict_property_rewrite_copy_layout_props_set_observersselfkwargs	__class__s     W/var/www/html/myenv/lib/python3.12/site-packages/ipywidgets/widgets/widget_templates.pyr#   zLayoutProperties.__init__O   sQ    "6"!,T!28D;E1G}-!    c                     |d   |d   }}| j                   |   j                  ||      }t        | j                  ||       y)z=delegate the trait types to their counterparts in self.layoutnewnameN)r%   getsetattrlayout)r)   changevaluer0   s       r,   _delegate_to_layoutz$LayoutProperties._delegate_to_layoutW   s@    UmVF^t&&t,00>T5)r-   c                 d    t         j                         }| j                  | j                  |       y)z<set observers on all layout properties defined in this classN)r   class_trait_namesr   r6   )r)   _propss     r,   r'   zLayoutProperties._set_observers]   s$    !335T--v6r-   c                     t         j                         }|D ]G  }t        | |      }|s| j                  |   j	                  ||      }t        | j                  ||       I y N)r   r8   getattrr%   r1   r2   r3   )r)   r9   propr5   s       r,   r&   z#LayoutProperties._copy_layout_propsb   s[    !335 	2DD$'E..t488FT51		2r-   )__name__
__module____qualname____doc__r   grid_gapr   justify_contentr    widthheightr#   r6   r'   r&   __classcell__r+   s   @r,   r   r   "   s    & *,H &	*1	3O
 "	  >	@K
 ')E ')F*7
2r-   r   c            
       R    e Zd ZdZ eed      Z eed      Z eed      Z eed      Z	 eed      Z
 e e        e        e       g d      Z e e        e        e       g d      Z ed      Z fdZed        Zd	 Zd
 Z edddddddd      d        Z x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2frrK   default_value)rK   3frrK   c                 D    t        |   di | | j                          y Nr!   r"   r#   _update_layoutr(   s     r,   r#   zAppLayout.__init__       "6"r-   c                     t        j                  d|       r| S t        j                  d|       r| dz   S t        dj                  |             )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)sizes    r,   _size_to_csszAppLayout._size_to_css   sI    88*D1K88M4($; ##)6$<1 	1r-   c                 @    t        t        | j                  |            S r;   )listmapr\   )r)   	size_lists     r,   _convert_sizeszAppLayout._convert_sizes   s    C))9566r-   c                    g dg dg dg}| j                  | j                        }| j                  | j                        }| j                  | j                  | j
                  | j                  | j                  d}|j                         D ci c]
  \  }}||| }}}|sy |j                         D ]  \  }}||j                  _
         | j                  rt        |      dk(  r+t        |j                               d   }|||g|||g|||gg}n| j                  |D ]  }|d=  |d= | j
                  |d   d   |d   d<   | j                  |d   d   |d   d<   | j
                  %| j                  | j                  d	gd
gg}dg}ddg}| j                  |d= |d= | j                  |d= |d= dj                  d |D              }	dj                  |      | j                  _        dj                  |      | j                  _        |	| j                  _        t'        |j)                               | _        y c c}}w )N)headerrc   rc   )left-sidebarr   right-sidebar)footerrf   rf   )rc   rf   rd   re   r   r   r   rc   rf   rK   
c              3   \   K   | ]$  }d j                  dj                  |             & ywz"{}" NrZ   join.0lines     r,   	<genexpr>z+AppLayout._update_layout.<locals>.<genexpr>   +      ,M04 -3MM#((4.,I ,M   *,rl   )ra   pane_widthspane_heightsrc   rf   left_sidebarright_sidebarr   itemsr3   	grid_areamergelenr^   keysrn   grid_template_columnsgrid_template_rowsgrid_template_areastuplevalueschildren)
r)   r   r~   r   all_childrenpositionchildr   rowgrid_template_areas_csss
             r,   rS   zAppLayout._update_layout   s   =J=? !% 3 3D4D4D E!001B1BC"&++"&++(,(9(9)-););"&++	/ ,8+=+=+?*'%( u$ * * '~~/ 	.OHe%-ELL"	. ::8}!03(0(H'E(0(H'E(0(H'E'G#
 ;;&2 #F#-a0$$,0CA0Fq0I'*1-%%-1DQ1G1K'*2.%%-**2+,4:z*B'-2G)*/&;;&+A.*1-;;&+B/*2. #')) ,M8K,M #M -0HH5J,K)),2D)E&*A'hoo/0k*s   H<rf   rc   r   rw   rx   r{   ru   rv   c                 $    | j                          y r;   rS   r)   r4   s     r,   _child_changedzAppLayout._child_changed   s     	r-   )r>   r?   r@   rA   r   r   rc   rf   rw   rx   r   r	   r   ru   rv   r   r{   r#   staticmethodr\   ra   rS   r   r   rF   rG   s   @r,   rI   rI   l   s    @ f.Ff.FFt4LV5Mf.F 
HJ
&;=KXZ'<>L t$E 1 17D1L Xx>?GN,,r-   rI   c                   v     e Zd ZdZ e       Z e       Zd
 fd	Z edd      d        Z	d Z
d Zd Zd	 Z x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
    c                 &   t        |   di | || _        || _        t	        | j                        D cg c]  }dg| j                  z   c}| _        d| j                  fz  | _        d| j                  fz  | _        i | _        d| _	        y c c}w )N.zrepeat(%d, 1fr)r   r!   )
r"   r#   n_rows	n_columnsrange_grid_template_areas_grid_template_rows_grid_template_columns	_children	_id_count)r)   r   r   r*   ir+   s        r,   r#   zGridspecLayout.__init__  s    "6""EJ4;;EW$XcUT^^%;$X!#4~#E &74>>:K&K# %Ys   Br   r   c                 2    |d   dkD  r|d   S t        d      )Nr5   r   z-n_rows and n_columns must be positive integer)r   )r)   proposals     r,   _validate_integerz GridspecLayout._validate_integer#  s&    Gq G$$HIIr-   c                    t        |t              r-|j                  | j                        \  }}}t	        |||      }n|g}t        |t              r0|j                  | 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columnss           r,   _get_indices_from_slicez&GridspecLayout._get_indices_from_slice)  s     c5!"%++dkk":E4f-D5Dfe$"(.."@E4E40G W} hGW}r-   c                    |\  }}| xj                   dz  c_         d| j                   z  }||j                  _        | j                  ||      \  }}|D ]M  }|D ]F  }| j                  |   |   }|dk7  r|| j
                  v r| j
                  |= || j                  |   |<   H O || j
                  |<   | j                          y )Nr   z
widget%03dr   )r   r3   rz   r   r   r   rS   )	r)   keyr5   r   r   obj_idr   r   current_values	            r,   __setitem__zGridspecLayout.__setitem__:  s    V!.!'44S&Ag 	@C! @ $ 9 9# >v F C'MT^^,K}59?))#.v6	@	@ "'vr-   c                      | j                   | \  }}d }|D ]1  }|D ]*  }| j                  |   |   }|xs |}||k7  s!t        d       3 | j                  |   S )NzTThe slice spans several widgets, but only a single widget can be retrieved at a time)r   r   rY   r   )r)   r   r   r   r   r   r   
new_obj_ids           r,   __getitem__zGridspecLayout.__getitem__L  s    444c:g 	1C! 1!66s;FC
-:Z'# %0 1 1	1	1 ~~f%%r-   c                 &   dj                  d | j                  D              }| j                  | j                  _        | j
                  | j                  _        || j                  _        t        | j                  j                               | _        y )Nri   c              3   \   K   | ]$  }d j                  dj                  |             & ywrk   rm   ro   s     r,   rr   z0GridspecLayout._update_layout.<locals>.<genexpr>]  s+      ,S04 -3MM#((4.,I ,Srt   )rn   r   r   r3   r~   r   r   r   r   r   r   r   )r)   r   s     r,   rS   zGridspecLayout._update_layout[  sr    "&)) ,S8<8Q8Q,S #S -1,G,G))-)A)A&*A'dnn3356r-   )NN)r>   r?   r@   rA   r
   r   r   r#   r   r   r   r   r   rS   rF   rG   s   @r,   r   r      sN    2 YF	I	 h$J %J
"$&7r-   r   c                        e Zd ZdZ eed      Z eed      Z eed      Z eed      Z	 e
d      Z fdZd Z eddd	d
d      d        Z x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"))

    TrJ   rM   c                 D    t        |   di | | j                          y rQ   rR   r(   s     r,   r#   zTwoByTwoLayout.__init__  rT   r-   c                    ddgddgg}| j                   | j                  | j                  | j                  d}|j	                         D ci c]
  \  }}||| }}}|sy |j	                         D ]  \  }}||j
                  _         | j                  rt        |      dk(  r%t        |j                               d   }||g||gg}ndd	g}t        |      D ]x  \  }}|j                  d
|z         |j                  d|z         }
}	|dz   dz  }|	|
|d   |   |d   |<   |d   |   |d   |<   W|	|d   |   |d   |<   h|
k|d   |   |d   |<   z dj                  d |D              }d| j
                  _        d| j
                  _        || j
                  _        t#        |j%                               | _        y c c}}w )Ntop-left	top-rightbottom-leftbottom-right)r   r   r   r   r   r   leftrightztop-zbottom-   ri   c              3   \   K   | ]$  }d j                  dj                  |             & ywrk   rm   ro   s     r,   rr   z0TwoByTwoLayout._update_layout.<locals>.<genexpr>  rs   rt   z1fr 1fr)top_left	top_rightbottom_leftbottom_rightry   r3   rz   r{   r|   r^   r}   	enumerater1   rn   r~   r   r   r   r   r   )r)   r   r   r   r   r   r   r   r   r   r   i_neighbourr   s                r,   rS   zTwoByTwoLayout._update_layout  s     !+K8 -~>@ &*]]&*nn(,(8(8)-):):< ,8+=+=+?*'%( u$ * * '~~/ 	.OHe%-ELL"	. ::8}!03(0(';(0(';'=# "7+!*7!3 NIAv"*,,v"?iZ`N`AaC#$q5A+K{v~4G4J;4W+A.q14G4J;4W+A.q14G4J14M+A.q14G4J14M+A.q1N #')) ,M8K,M #M -6))2&*A'hoo/0O*s   F<r   r   r   r   r{   c                 $    | j                          y r;   r   r   s     r,   r   zTwoByTwoLayout._child_changed  s    r-   )r>   r?   r@   rA   r   r   r   r   r   r   r   r{   r#   rS   r   r   rF   rG   s   @r,   r   r   f  ss    8 40HD1I6d3KFt4L t$E21h Z^WM Nr-   r   )rA   rW   collectionsr   	traitletsr   r   r   r   r   r	   r
   r   r   r   r   widgetr   
widget_boxr   docutilsr   _doc_snippetsr   rI   r   r   r!   r-   r,   <module>r      s    = 	 # O O  + '     " =G2y G2 G2R =J) J JZ =h7W. h7 h7V =_W. _ _r-   