U
    cY                     @  s|  U d Z ddlmZ ddlZee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 ddlmZmZ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'm(Z(m)Z)m*Z* dd	l+m,Z, erdd
l-m.Z. dZ/edddddddddZ0edddddddddZ0dddddddddZ0edddddddddZ1edddddddddZ1dddddddddZ1dddddddddd d!Z2dd"ddddd#d$d%dd&d'd'd'dd(d)d*	d+d,Z3ed-dd.dd/d0d1d2Z4ed-dd3dd/d0d4d2Z4ed-dd5dd6d/d7d8d2Z4ed-dd5dd6d/d9d:d2Z4ed-dd5dd6d6d/d;d<d2Z4ed-dd=dd/d0d>d2Z4g dddfddd'd'd/d;d?d2Z4ed@e'dAZ5eee5 ee5 gee'e)df f Z6dBe7dC< ddDdEdFdEdGdHdIZ8dJd(dKdLdMZ9edNe"dAZ:ddOdPdQdRdSdTdUZ;ddd=dVdWdXdYZ<dddZdd6d3d[d\d]Z=ed^Z>d_d6d`dadbdcZ?dS )ez0 Functions for arranging bokeh layout objects.

    )annotationsN)defaultdict)TYPE_CHECKINGAnyCallableIterableIteratorListSequenceTypeTypeVarUnionoverload   )LocationLocationTypeSizingModeType)ColumnCopyToolExamineToolFlexBoxFullscreenToolGridBoxGridPlot	LayoutDOMPlotRowSaveToolSpacerToolToolbar	ToolProxy	UIElement)	dataclass)	TypeAlias)columngridgridplotlayoutrowr   )sizing_modezlist[UIElement]zSizingModeType | Noner   r   )childrenr*   kwargsreturnc                K  s   d S N r+   r*   r,   r/   r/   ,lib/python3.8/site-packages/bokeh/layouts.pyr)   R   s    r)   r"   c                 O  s   d S r.   r/   r*   r+   r,   r/   r/   r1   r)   T   s    zUIElement | list[UIElement]c                 O  s8   t |d|ddi}t|| dd tf || d|S )a)   Create a row of Bokeh Layout objects. Forces all objects to
    have the same sizing_mode, which is required for complex layouts to work.

    Args:
        children (list of :class:`~bokeh.models.LayoutDOM` ): A list of instances for
            the row. Can be any of the following - |Plot|,
            :class:`~bokeh.models.Widget`,
            :class:`~bokeh.models.Row`,
            :class:`~bokeh.models.Column`,
            :class:`~bokeh.models.Spacer`.

        sizing_mode (``"fixed"``, ``"stretch_both"``, ``"scale_width"``, ``"scale_height"``, ``"scale_both"`` ): How
            will the items in the layout resize to fill the available space.
            Default is ``"fixed"``. For more information on the different
            modes see :attr:`~bokeh.models.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.LayoutDOM`.

    Returns:
        Row: A row of LayoutDOM objects all with the same sizing_mode.

    Examples:

        >>> row(plot1, plot2)
        >>> row(children=[widgets, plot], sizing_mode='stretch_both')
    r+   Nr)   widgetr+   r*   )_parse_children_argpop_handle_child_sizingr   r*   r+   r,   	_childrenr/   r/   r1   r)   W   s    r   c                K  s   d S r.   r/   r0   r/   r/   r1   r%   u   s    r%   c                 O  s   d S r.   r/   r2   r/   r/   r1   r%   w   s    c                 O  s8   t |d|ddi}t|| dd tf || d|S )a;   Create a column of Bokeh Layout objects. Forces all objects to
    have the same sizing_mode, which is required for complex layouts to work.

    Args:
        children (list of :class:`~bokeh.models.LayoutDOM` ): A list of instances for
            the column. Can be any of the following - |Plot|,
            :class:`~bokeh.models.Widget`,
            :class:`~bokeh.models.Row`,
            :class:`~bokeh.models.Column`,
            :class:`~bokeh.models.Spacer`.

        sizing_mode (``"fixed"``, ``"stretch_both"``, ``"scale_width"``, ``"scale_height"``, ``"scale_both"`` ): How
            will the items in the layout resize to fill the available space.
            Default is ``"fixed"``. For more information on the different
            modes see :attr:`~bokeh.models.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.LayoutDOM`.

    Returns:
        Column: A column of LayoutDOM objects all with the same sizing_mode.

    Examples:

        >>> column(plot1, plot2)
        >>> column(children=[widgets, plot], sizing_mode='stretch_both')
    r+   Nr%   r3   r5   )r6   r7   r8   r   r9   r/   r/   r1   r%   z   s    r5   zlist[UIElement] | None)argsr+   r*   r,   r-   c                 O  s   t |d| i}t||f|S )a   Create a grid-based arrangement of Bokeh Layout objects.

    Args:
        children (list of lists of :class:`~bokeh.models.LayoutDOM` ): A list of lists of instances
            for a grid layout. Can be any of the following - |Plot|,
            :class:`~bokeh.models.Widget`,
            :class:`~bokeh.models.Row`,
            :class:`~bokeh.models.Column`,
            :class:`~bokeh.models.Spacer`.

        sizing_mode (``"fixed"``, ``"stretch_both"``, ``"scale_width"``, ``"scale_height"``, ``"scale_both"`` ): How
            will the items in the layout resize to fill the available space.
            Default is ``"fixed"``. For more information on the different
            modes see :attr:`~bokeh.models.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.LayoutDOM`.

    Returns:
        Column: A column of ``Row`` layouts of the children, all with the same sizing_mode.

    Examples:

        >>> layout([[plot_1, plot_2], [plot_3, plot_4]])
        >>> layout(
                children=[
                    [widget_1, plot_1],
                    [slider],
                    [widget_2, plot_2, plot_3]
                ],
                sizing_mode='fixed',
            )

    r+   )r6   _create_grid)r+   r*   r;   r,   r:   r/   r/   r1   r(      s    !r(   ZaboveT)r*   toolbar_locationncolswidthheighttoolbar_optionsmerge_toolszlist[list[UIElement | None]]zLocationType | Nonez
int | Noneboolr   )	r+   r*   r=   r>   r?   r@   rA   rB   r-   c                C  s|  |dkri }|r(t t|s(td| t| d} |r^tdd | D rPtdtt| |} | sfg } g }g }	t| D ]\}
}t|D ]\}}|dkrqqt|t	r|r|
ttdD ]}||jj d|_q|dk	r||_|dk	r||_|dk	r
t|r
||_|	||
|f qt|tr,qqtdqqvd	d
ddd}tf d|sX|n
t||di|}t|	|||dS )aM	   Create a grid of plots rendered on separate canvases.

    The ``gridplot`` function builds a single toolbar for all the plots in the
    grid. ``gridplot`` is designed to layout a set of plots. For general
    grid layout, use the :func:`~bokeh.layouts.layout` function.

    Args:
        children (list of lists of |Plot|): An array of plots to display in a
            grid, given as a list of lists of Plot objects. To leave a position
            in the grid empty, pass None for that position in the children list.
            OR list of |Plot| if called with ncols.

        sizing_mode (``"fixed"``, ``"stretch_both"``, ``"scale_width"``, ``"scale_height"``, ``"scale_both"`` ): How
            will the items in the layout resize to fill the available space.
            Default is ``"fixed"``. For more information on the different
            modes see :attr:`~bokeh.models.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.LayoutDOM`.

        toolbar_location (``above``, ``below``, ``left``, ``right`` ): Where the
            toolbar will be located, with respect to the grid. Default is
            ``above``. If set to None, no toolbar will be attached to the grid.

        ncols (int, optional): Specify the number of columns you would like in your grid.
            You must only pass an un-nested list of plots (as opposed to a list of lists of plots)
            when using ncols.

        width (int, optional): The width you would like all your plots to be

        height (int, optional): The height you would like all your plots to be.

        toolbar_options (dict, optional) : A dictionary of options that will be
            used to construct the grid's toolbar (an instance of
            :class:`~bokeh.models.Toolbar`). If none is supplied,
            Toolbar's defaults will be used.

        merge_tools (``True``, ``False``): Combine tools from all child plots into
            a single toolbar.

    Returns:
        GridPlot:

    Examples:

        >>> gridplot([[plot_1, plot_2], [plot_3, plot_4]])
        >>> gridplot([plot_1, plot_2, plot_3, plot_4], ncols=2, width=200, height=100)
        >>> gridplot(
                children=[[plot_1, plot_2], [None, plot_3]],
                toolbar_location='right'
                sizing_mode='fixed',
                toolbar_options=dict(logo='gray')
            )

    Nz#Invalid value of toolbar_location: r+   c                 s  s   | ]}t |tV  qd S r.   
isinstancelist.0childr/   r/   r1   	<genexpr>  s     zgridplot.<locals>.<genexpr>z-Cannot provide a nested list when using ncols)typez>Only UIElement and LayoutDOM items can be inserted into a gridz
type[Tool]z
list[Tool]clsgroupc                 S  s    t | ttttfr|  S d S d S r.   )
issubclassr   r   r   r   rM   r/   r/   r1   merge(  s    zgridplot.<locals>.mergetoolsrQ   )r+   toolbarr=   r*   )hasattrr   
ValueErrorr6   anyrG   _chunks	enumeraterF   r   Zselectdictr   extendrT   rR   r=   r?   r@   _has_auto_sizingr*   appendr"   r    group_toolsr   )r+   r*   r=   r>   r?   r@   rA   rB   rR   itemsyr)   xitemZplotrQ   rT   r/   r/   r1   r'      sH    >

$r'   .z-list[UIElement | list[UIElement | list[Any]]]r   )r+   r*   r-   c                C  s   d S r.   r/   r5   r/   r/   r1   r&   2  s    r&   zRow | Columnc                C  s   d S r.   r/   r5   r/   r/   r1   r&   4  s    zlist[UIElement | None]int)r+   r*   nrowsr-   c                C  s   d S r.   r/   )r+   r*   rd   r/   r/   r1   r&   6  s    )r+   r*   r>   r-   c                C  s   d S r.   r/   )r+   r*   r>   r/   r/   r1   r&   8  s    )r+   r*   rd   r>   r-   c                C  s   d S r.   r/   )r+   r*   rd   r>   r/   r/   r1   r&   :  s    strc                C  s   d S r.   r/   r5   r/   r/   r1   r&   <  s    c           	        s  t G fdddt G fdddt G dd dt G fddd d	d
 fdd}ttr|dk	sdk	rt}dkrt|| fddtd|D }n"d!dddfdd}n`ttr6dddddd"dddfdddd}nttrHt	nt
d ||}|dk	r||_|jD ]}|d }t|rn||_qn|S )#a  
    Conveniently create a grid of layoutable objects.

    Grids are created by using ``GridBox`` model. This gives the most control over
    the layout of a grid, but is also tedious and may result in unreadable code in
    practical applications. ``grid()`` function remedies this by reducing the level
    of control, but in turn providing a more convenient API.

    Supported patterns:

    1. Nested lists of layoutable objects. Assumes the top-level list represents
       a column and alternates between rows and columns in subsequent nesting
       levels. One can use ``None`` for padding purpose.

       >>> grid([p1, [[p2, p3], p4]])
       GridBox(children=[
           (p1, 0, 0, 1, 2),
           (p2, 1, 0, 1, 1),
           (p3, 2, 0, 1, 1),
           (p4, 1, 1, 2, 1),
       ])

    2. Nested ``Row`` and ``Column`` instances. Similar to the first pattern, just
       instead of using nested lists, it uses nested ``Row`` and ``Column`` models.
       This can be much more readable that the former. Note, however, that only
       models that don't have ``sizing_mode`` set are used.

       >>> grid(column(p1, row(column(p2, p3), p4)))
       GridBox(children=[
           (p1, 0, 0, 1, 2),
           (p2, 1, 0, 1, 1),
           (p3, 2, 0, 1, 1),
           (p4, 1, 1, 2, 1),
       ])

    3. Flat list of layoutable objects. This requires ``nrows`` and/or ``ncols`` to
       be set. The input list will be rearranged into a 2D array accordingly. One
       can use ``None`` for padding purpose.

       >>> grid([p1, p2, p3, p4], ncols=2)
       GridBox(children=[
           (p1, 0, 0, 1, 1),
           (p2, 0, 1, 1, 1),
           (p3, 1, 0, 1, 1),
           (p4, 1, 1, 1, 1),
       ])

    c                      s   e Zd ZU ded< dS )zgrid.<locals>.rowlist[row | col]r+   N__name__
__module____qualname____annotations__r/   colr)   r/   r1   r)   p  s   
r)   c                      s   e Zd ZU ded< dS )zgrid.<locals>.colrf   r+   Nrg   r/   rl   r/   r1   rm   s  s   
rm   c                   @  s6   e Zd ZU ded< ded< ded< ded< ded< dS )	zgrid.<locals>.Itemr   r(   rc   r0c0r1c1Nrg   r/   r/   r/   r1   Itemw  s
   
rr   c                      s&   e Zd ZU ded< ded< ded< dS )zgrid.<locals>.Gridrc   rd   r>   z
list[Item]r_   Nrg   r/   )rr   r/   r1   Grid  s   
rs   r   )r-   c              	     s   ddddddddddfdddd	d
ddddd fdd  | }g }|j D ]8}|jd k	rj||j|j|j|j|j |j|j f qjt|dS )Nrc   )abr-   c                 S  s.   t | t | } }|dkr*|| |  } }q| S Nr   )abs)rt   ru   r/   r/   r1   gcd  s    z"grid.<locals>.flatten.<locals>.gcd)rt   restr-   c                   s    |D ]}| |  | | } q| S r.   r/   )rt   ry   ru   )rx   r/   r1   lcm  s    z"grid.<locals>.flatten.<locals>.lcmrs   rC   rJ   r-   c                 S  s   | j dko| jdkS rv   )rd   r>   rJ   r/   r/   r1   nonempty  s    z'grid.<locals>.flatten.<locals>.nonemptyzrow | col | LayoutDOM)r(   r-   c           	        s  t | rttt| j}|s0 ddg S dd |D  }tdd |D }g }d}|D ]T}||j }|jD ]4}||j	||j
 |j| ||j |j|  qt||j7 }q` |||S t | rttt| j}|s ddg S tdd |D }dd |D  }g }d}|D ]X}||j }|jD ]6}||j	|j
| ||j |j| ||j  q8||j7 }q$ |||S  dd| ddddgS d S )	Nr   c                 S  s   g | ]
}|j qS r/   rd   rH   r/   r/   r1   
<listcomp>  s     z;grid.<locals>.flatten.<locals>._flatten.<locals>.<listcomp>c                 s  s   | ]}|j V  qd S r.   r>   rH   r/   r/   r1   rK     s     z:grid.<locals>.flatten.<locals>._flatten.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S r.   r~   rH   r/   r/   r1   rK     s     c                 S  s   g | ]
}|j qS r/   r   rH   r/   r/   r1   r     s     r   )rF   rG   filtermapr+   sumrd   r_   r]   r(   rn   ro   rp   rq   r>   )	r(   r+   rd   r>   r_   offsetrJ   Zfactori)rs   rr   _flattenrm   rz   r}   r)   r/   r1   r     s:    


2

4z'grid.<locals>.flatten.<locals>._flattenrD   )r_   r(   r]   rn   ro   rp   rq   r   )r(   r&   r+   r   )rs   rr   rm   r)   )r   rx   rz   r}   r1   flatten  s     *

,zgrid.<locals>.flattenNc                   s    g | ]} ||  qS r/   r/   )rI   r   )r+   r>   r)   r/   r1   r     s     zgrid.<locals>.<listcomp>r   zlist[LayoutDOM]rc   )r+   levelc                   s>   t | tr6 d dkrn}| fdd| D S | S d S )N   r   c                   s   g | ]}| d  qS )r   r/   rH   )r   traverser/   r1   r     s     z*grid.<locals>.traverse.<locals>.<listcomp>rE   )r+   r   	container)rm   r)   r   )r   r1   r     s    
zgrid.<locals>.traverser   rC   r{   c                 S  s   t | o| jdkS rv   )r\   Zspacingr|   r/   r/   r1   	is_usable  s    zgrid.<locals>.is_usableF)rb   	top_levelc                   sD   t | tr<|s| r<t | tr$ n}|tt| jS | S d S r.   )rF   r   r   rG   r   r+   )rb   r   r   )rm   r   r)   r   r/   r1   r     s    T)r   z expected a list, string or model)r   )F)r#   rF   rG   lenmathZceilranger   re   NotImplementedErrorrV   r*   r+   r\   )	r+   r*   rd   r>   r   Nr(   r&   rJ   r/   )rs   rr   r+   rm   r   r>   r)   r   r1   r&   ?  s@    1C
$




T)Zboundr$   MergeFnrS   zlist[Tool | ToolProxy]zMergeFn[Tool] | None)rR   rQ   r-   c                C  sX  t G dd d}tt}g }| D ]F}t|tr<|| q"| }d|krR|d= ||j ||| q"| D ]\}}|dk	r||dd |D }	|	dk	r||	 qr|rr|^}
}|
j	g}t|D ]&}|j
|
j
kr||j	 || q||
 t|dkr||d  q|dk	r@||| }dk	r@|| q|t|d	 qqr|S )
z' Group common tools into tool proxies. c                   @  s   e Zd ZU ded< ded< dS )zgroup_tools.<locals>.ToolEntryr   toolr   propsNrg   r/   r/   r/   r1   	ToolEntry  s   
r   ZoverlayNc                 S  s   g | ]
}|j qS r/   )r   )rI   entryr/   r/   r1   r     s     zgroup_tools.<locals>.<listcomp>r   r   )rR   )r#   r   rG   rF   r!   r]   Zproperties_with_values	__class__r_   r   r   remover   )rR   rQ   r   Zby_typeZcomputedr   r   rN   entriesZmergedheadtailrO   rb   r/   r/   r1   r^     s>    


r^   r   )rb   r-   c                 C  s   | j d ko| jdko| jdkS )Nauto)r*   Zwidth_policyZheight_policy)rb   r/   r/   r1   r\   +  s    r\   LrD   zL | list[L]zlist[L] | Nonezlist[L])r;   r+   r-   c                 G  sL   t |dkr| d k	rtd| sHt |dkr@|\}t|tr@|S t|S | S )Nr   z;'children' keyword cannot be used with positional argumentsr   )r   rV   rF   rG   )r+   r;   argr/   r/   r1   r6   /  s    
r6   None)r+   r*   r4   r-   c             	   C  sX   | D ]N}t |trqt |ts<td| d| dt| |d k	rt|r||_qd S )Nz,Only LayoutDOM items can be inserted into a z. Tried to insert: z	 of type )rF   r"   r   rV   rL   r\   r*   )r+   r*   r4   rb   r/   r/   r1   r8   >  s    

r8   z%Iterable[UIElement | list[UIElement]])iterabler*   layerr-   c                 K  s   g }| D ]|}t |tr.|t|||d  qt |trZ|dk	rNt|rN||_|| qt |trp|| qtd|t	|f q|d dkrt
f ||d|S tf ||d|S dS )z)Recursively create grid from input lists.r   NzbOnly LayoutDOM items can be inserted into a layout.
                Tried to insert: %s of type %sr   r   r5   )rF   rG   r]   r<   r   r\   r*   r"   rV   rL   r%   r)   )r   r*   r   r,   Zreturn_listrb   r/   r/   r1   r<   G  s$    



r<   IzSequence[I]zIterator[Sequence[I]])lr>   r-   c                 c  s>   t |tstdtdt| |D ]}| |||  V  q"dS )z-Yield successive n-sized chunks from list, l.zncols must be an integerr   N)rF   rc   AssertionErrorr   r   )r   r>   r   r/   r/   r1   rX   _  s    rX   )r   )@__doc__Z
__future__r   ZloggingZ	getLoggerrh   logr   collectionsr   typingr   r   r   r   r   r	   r
   r   r   r   r   Z
core.enumsr   r   r   Zmodelsr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   Zutil.dataclassesr#   Ztyping_extensionsr$   __all__r)   r%   r(   r'   r&   r   r   rk   r^   r\   r   r6   r8   r<   r   rX   r/   r/   r/   r1   <module>   sn   
4H&"u  :*0	