ó
mÜJ]c           @` sw  d  Z  d d l m Z m Z m Z m Z d d l Z e j e ƒ Z	 d d l
 Z
 d d l m Z d d l m Z d d l m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z m Z m Z m Z m Z m Z d  Z d „  Z d „  Z  d „  Z! d „  Z" d d d d d d e$ d „ Z% g  d d d d „ Z& d e' f d „  ƒ  YZ( d „  Z) d „  Z* d d „ Z+ d „  Z, d S(!   u0    Functions for arranging bokeh layout objects.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t
   namedtuple(   t   string_typesi   (   t   Location(   t   ProxyToolbart
   ToolbarBox(   t   Plot(   t	   LayoutDOMt   Boxt   Rowt   Columnt   GridBoxt   Spacert	   WidgetBoxu   columnu   gridu   gridplotu   GridSpecu   layoutu   rowu   Spaceru	   widgetboxc          O` sÂ   | j  d d ƒ } | j  d d ƒ } t d | |  Œ } g  } xm | D]e } t | t ƒ rŒ | d k	 r| t | ƒ r| | | _ n  | j | ƒ qC t d | t	 | ƒ f ƒ ‚ qC Wt
 d | d | |  S(   uÃ   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.layouts.LayoutDOM` ): A list of instances for
            the row. Can be any of the following - :class:`~bokeh.models.plots.Plot`,
            :class:`~bokeh.models.widgets.widget.Widget`,
            :class:`~bokeh.models.layouts.Row`,
            :class:`~bokeh.models.layouts.Column`,
            :class:`~bokeh.models.tools.ToolbarBox`,
            :class:`~bokeh.models.layouts.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.layouts.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.layouts.LayoutDOM`.

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

    Examples:

        >>> row([plot_1, plot_2])
        >>> row(children=[widget_box_1, plot_1], sizing_mode='stretch_both')
    u   sizing_modeu   childrent   childrenuO   Only LayoutDOM items can be inserted into a row. Tried to insert: %s of type %st   sizing_modeN(   t   popt   Nonet   _handle_childrent
   isinstanceR
   t   _has_auto_sizingR   t   appendt
   ValueErrort   typeR   (   t   argst   kwargsR   R   t   row_childrent   item(    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   row7   s     c          O` sÂ   | j  d d ƒ } | j  d d ƒ } t d | |  Œ } g  } xm | D]e } t | t ƒ rŒ | d k	 r| t | ƒ r| | | _ n  | j | ƒ qC t d | t	 | ƒ f ƒ ‚ qC Wt
 d | d | |  S(   uÑ   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.layouts.LayoutDOM` ): A list of instances for
            the column. Can be any of the following - :class:`~bokeh.models.plots.Plot`,
            :class:`~bokeh.models.widgets.widget.Widget`,
            :class:`~bokeh.models.layouts.Row`,
            :class:`~bokeh.models.layouts.Column`,
            :class:`~bokeh.models.tools.ToolbarBox`,
            :class:`~bokeh.models.layouts.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.layouts.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.layouts.LayoutDOM`.

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

    Examples:

        >>> column([plot_1, plot_2])
        >>> column(children=[widget_1, plot_1], sizing_mode='stretch_both')
    u   sizing_modeu   childrenR   uR   Only LayoutDOM items can be inserted into a column. Tried to insert: %s of type %sR   N(   R   R   R   R   R
   R   R   R   R   R   R   (   R   R   R   R   t   col_childrenR   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   columnd   s     c          O` sÂ   | j  d d ƒ } | j  d d ƒ } t d | |  Œ } g  } xm | D]e } t | t ƒ rŒ | d k	 r| t | ƒ r| | | _ n  | j | ƒ qC t d | t	 | ƒ f ƒ ‚ qC Wt
 d | d | |  S(   u&   Create a column of bokeh widgets with predefined styling.

    Args:
        children (list of :class:`~bokeh.models.widgets.widget.Widget`): A list of widgets.

        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.layouts.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.layouts.LayoutDOM`.

    Returns:
        WidgetBox: A column layout of widget instances all with the same ``sizing_mode``.

    Examples:

        >>> widgetbox([button, select])
        >>> widgetbox(children=[slider], sizing_mode='scale_width')
    u   sizing_modeu   childrenR   uV   Only LayoutDOM items can be inserted into a widget box. Tried to insert: %s of type %sR   N(   R   R   R   R   R
   R   R   R   R   R   R   (   R   R   R   R   R    R   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt	   widgetbox‘   s     c          O` sF   | j  d d ƒ } | j  d d ƒ } t d | |  Œ } t | | |  S(   uT   Create a grid-based arrangement of Bokeh Layout objects.

    Args:
        children (list of lists of :class:`~bokeh.models.layouts.LayoutDOM` ): A list of lists of instances
            for a grid layout. Can be any of the following - :class:`~bokeh.models.plots.Plot`,
            :class:`~bokeh.models.widgets.widget.Widget`,
            :class:`~bokeh.models.layouts.Row`,
            :class:`~bokeh.models.layouts.Column`,
            :class:`~bokeh.models.tools.ToolbarBox`,
            :class:`~bokeh.models.layouts.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.layouts.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.layouts.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',
            )

    u   sizing_modeu   childrenR   N(   R   R   R   t   _create_grid(   R   R   R   R   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   layout¶   s    "u   abovec         C` sÇ  | d k r i  } n  | r@ t t | ƒ s@ t d | ƒ ‚ q@ n  t d |  ƒ }  | r’ t d „  |  Dƒ ƒ rz t d ƒ ‚ n  t t |  | ƒ ƒ }  n  |  s¡ g  }  n  g  } g  }	 x(t |  ƒ D]\ }
 } xt | ƒ D]ý \ } } | d k rñ qÓ qÓ t	 | t
 ƒ rÄ| rEx< | j t d t ƒ ƒ D] } | | j j 7} d | _ qWn  t	 | t ƒ r‡| d k	 rl| | _ n  | d k	 r‡| | _ q‡n  | d k	 r«t | ƒ r«| | _ n  |	 j | |
 | f ƒ qÓ t d ƒ ‚ qÓ Wqº W| sæ| rùt d |	 d | ƒ St d |	 ƒ } t d | |  } t d	 | d
 | ƒ } | d k rTt d | | g d | ƒ S| d k ryt d | | g d | ƒ S| d k ržt d | | g d | ƒ S| d k rÃt d | | g d | ƒ Sd S(   u©
   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 :class:`~bokeh.models.plots.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 :class:`~bokeh.models.plots.Plot` if called with
            ncols. OR an instance of GridSpec.

        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.layouts.LayoutDOM.sizing_mode`
            description on :class:`~bokeh.models.layouts.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.

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

        plot_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.tools.ToolbarBox`). If none is supplied,
            ToolbarBox's defaults will be used.

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

    Returns:
        Row or Column: A row or column containing the grid toolbar and the grid
            of plots (depending on whether the toolbar is left/right or
            above/below. The grid is always a Column of Rows of plots.

    Examples:

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

    u%   Invalid value of toolbar_location: %sR   c         s` s   |  ] } t  | t ƒ Vq d  S(   N(   R   t   list(   t   .0t   child(    (    s,   lib/python2.7/site-packages/bokeh/layouts.pys	   <genexpr>#  s    u-   Cannot provide a nested list when using ncolsR   u0   Only LayoutDOM items can be inserted into a gridR   t   toolst   toolbart   toolbar_locationu   aboveu   belowu   leftu   rightN(   R   t   hasattrR   R   R   t   anyR%   t   _chunkst	   enumerateR   R
   t   selectt   dictR	   R)   R(   R*   t
   plot_widtht   plot_heightR   R   R   R   R   R   R   R   (   R   R   R*   t   ncolsR1   R2   t   toolbar_optionst   merge_toolsR(   t   itemst   yR   t   xR   t   plott   gridt   proxyR)   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   gridplotà   sZ    :		c   
      ` s»  t  d d g ƒ ‰ t  d d g ƒ ‰  ‡  ‡ f d †  } t |  t ƒ rñ | d k	 s] | d k	 rÊ t |  ƒ } | d k r‹ t j | | ƒ } n  ˆ  g  t d | | ƒ D] } ˆ |  | | | !ƒ ^ q¡ ƒ } q]d ‡  ‡ ‡ f d † ‰ ˆ |  ƒ } nl t |  t ƒ r9d „  ‰ t	 ‡  ‡ ‡ ‡ f d † ‰ ˆ |  d	 t
 ƒ} n$ t |  t ƒ rQt ‚ n t d
 ƒ ‚ | | ƒ } | d k	 r·| | _ x6 | j D]( }	 |	 d } t | ƒ rˆ| | _ qˆqˆWn  | S(   u¿  
    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),
       ])

    u   rowu   childrenu   colc         ` sí   t  d d d d d d g ƒ ‰ t  d d d	 d
 g ƒ ‰  d „  ‰ ‡ f d †  ‰ d „  ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ f d †  ‰ ˆ |  ƒ } g  } xS | j D]H \ }  } } } } |  d  k	 r” | j |  | | | | | | f ƒ q” q” Wt d | ƒ S(   Nu   Itemu   layoutu   r0u   c0u   r1u   c1u   Gridu   nrowsu   ncolsu   itemsc         S` sA   t  |  ƒ t  | ƒ }  } x! | d k r< | |  | }  } q W|  S(   Ni    (   t   abs(   t   at   b(    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   gcdŽ  s    c         ` s,   x% | D] } |  | ˆ  |  | ƒ }  q W|  S(   N(    (   R>   t   restR?   (   R@   (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   lcm”  s    c         S` s   |  j  d k o |  j d k S(   Ni    (   t   nrowsR3   (   R'   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   <lambda>™  t    c      	   ` si  t  |  ˆ ƒ r t t ˆ t ˆ |  j ƒ ƒ ƒ } | sF ˆ  d d g  ƒ Sˆ g  | D] } | j ^ qP Œ  } t g  | D] } | j ^ qr ƒ } g  } d } xw | D]o } | | j } xL | j D]A \ }  } }	 }
 } | j	 |  | | |	 | | |
 | | f ƒ qº W| | j 7} q Wˆ  | | | ƒ St  |  ˆ ƒ r@t t ˆ t ˆ |  j ƒ ƒ ƒ } | sfˆ  d d g  ƒ St g  | D] } | j ^ qpƒ } ˆ g  | D] } | j ^ q’Œ  } g  } d } xw | D]o } | | j } xL | j D]A \ }  } }	 }
 } | j	 |  | | | |	 |
 | | | f ƒ qÚW| | j 7} q½Wˆ  | | | ƒ Sˆ  d d ˆ |  d d d d ƒ g ƒ Sd  S(   Ni    i   (
   R   R%   t   filtert   mapR   RC   t   sumR3   R6   R   (   R$   R   R'   RC   R3   R6   t   offsett   factort   r0t   c0t   r1t   c1(   t   Gridt   Itemt   _flattent   colRB   t   nonemptyR   (    s,   lib/python2.7/site-packages/bokeh/layouts.pyRQ   ›  s:    !""0!""0R   (   R   R6   R   R   R   (   R$   R:   R   RK   RL   RM   RN   (   RR   R   (   RO   RP   RQ   R@   RB   RS   s,   lib/python2.7/site-packages/bokeh/layouts.pyt   flattenŠ  s    		!*+i    c         ` s]   t  |  t ƒ rU | d d k r% ˆ  n ˆ } | g  |  D] } ˆ | | d ƒ ^ q5 ƒ S|  Sd  S(   Ni   i    i   (   R   R%   (   R   t   levelt	   containerR'   (   RR   R   t   traverse(    s,   lib/python2.7/site-packages/bokeh/layouts.pyRW   Õ  s    *c         S` s   t  |  ƒ o |  j d k S(   Ni    (   R   t   spacing(   R'   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt	   is_usableÞ  s    c         ` s`   t  |  t ƒ rX | s! ˆ |  ƒ rX t  |  t ƒ r6 ˆ  n ˆ } | t t ˆ |  j ƒ ƒ ƒ S|  Sd  S(   N(   R   R   R   R%   RG   R   (   R   t	   top_levelRV   (   RR   RY   R   RW   (    s,   lib/python2.7/site-packages/bokeh/layouts.pyRW   á  s    !RZ   u    expected a list, string or modelN(   R   R   R%   R   t   lent   matht   ceilt   rangeR
   t   Falset   TrueR   t   NotImplementedErrorR   R   R   R   (
   R   R   RC   R3   RT   t   Nt   iR$   R:   R'   (    (   RR   RY   R   RW   s,   lib/python2.7/site-packages/bokeh/layouts.pyR:   V  s4    1D?			
t   GridSpecc           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   u'    Simplifies grid layout specification. c         C` s   | |  _  | |  _ i  |  _ d  S(   N(   RC   R3   t   _arrangement(   t   selfRC   R3   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   __init__  s    		c   	      ` sR  | \ } } t  | t ƒ r9 | j |  j ƒ \ ‰ } } nS | d k  rU | |  j 7} n  | |  j k sp | d k  r t d ƒ ‚ n  | d  ‰ } t  | t ƒ r¹ | j |  j ƒ \ ‰ } } nS | d k  rÕ | |  j 7} n  | |  j k sð | d k  rÿ t d ƒ ‚ n  | d  ‰ } d „  } | d  k rC| d  k rCˆ |  j ˆ ˆ f <n| d  k r—xü t ˆ | ƒ D]1 ‰  | ‡  ‡ ‡ f d †  d  ƒ |  j ˆ ˆ  f <q_Wn· | d  k rëx¨ t ˆ | ƒ D]1 ‰ | ‡ ‡ ‡ f d †  d  ƒ |  j ˆ ˆ f <q³Wnc x` t	 t ˆ | ƒ t ˆ | ƒ ƒ D]= \ ‰ ‰  | ‡  ‡ ‡ ‡ ‡ f d †  d  ƒ |  j ˆ ˆ  f <qWd  S(   Ni    u   index out of rangec         S` s$   y |  ƒ  SWn t  k
 r | SXd  S(   N(   t
   IndexError(   t   fnt   default(    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   get_or_else   s    c           ` s   ˆ ˆ  ˆ S(   N(    (    (   RR   t   col1t   obj(    s,   lib/python2.7/site-packages/bokeh/layouts.pyRD   *  RE   c           ` s   ˆ  ˆ ˆ S(   N(    (    (   Rm   R   t   row1(    s,   lib/python2.7/site-packages/bokeh/layouts.pyRD   -  RE   c           ` s   ˆ ˆ ˆ ˆ  ˆ S(   N(    (    (   RR   Rl   Rm   R   Rn   (    s,   lib/python2.7/site-packages/bokeh/layouts.pyRD   0  RE   (
   R   t   slicet   indicesRC   Rh   R   R3   Re   R^   t   zip(	   Rf   t   keyRm   t   k1t   k2t   row2t   _t   col2Rk   (    (   RR   Rl   Rm   R   Rn   s,   lib/python2.7/site-packages/bokeh/layouts.pyt   __setitem__  s4    	22.c         C` sm   g  t  d |  j ƒ D] } d  g |  j ^ q } x1 |  j j ƒ  D]  \ \ } } } | | | | <q? Wt | ƒ S(   Ni    (   R^   RC   R   R3   Re   R6   t   iter(   Rf   Rv   t   arrayR   RR   Rm   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   __iter__2  s    /"(   t   __name__t
   __module__t   __doc__Rg   Rx   R{   (    (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyRd   þ  s   		,c         C` s+   |  j  d  k o* |  j d k o* |  j d k S(   Nu   auto(   R   R   t   width_policyt   height_policy(   R   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyR   <  s    c          O` s¹   | j  d ƒ } t |  ƒ d k r< | d  k	 r< t d ƒ ‚ n  | sµ t |  ƒ d k rt t |  d t ƒ rt |  d } qµ t |  ƒ d k r¦ t |  d t ƒ r¦ |  d } qµ t |  ƒ } n  | S(   Nu   childreni    u;   'children' keyword cannot be used with positional argumentsi   (   t   getR[   R   R   R   R%   Rd   (   R   R   R   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyR   ?  s    %%c         K` sá   g  } xœ |  D]” } t  | t ƒ rB | j t | | | d ƒ ƒ q t  | t ƒ r… | d k	 ru t | ƒ ru | | _ n  | j | ƒ q t d | t	 | ƒ f ƒ ‚ q W| d d k rË t
 d | d | |  St d | d | |  S(   u)   Recursively create grid from input lists.i   ub   Only LayoutDOM items can be inserted into a layout.
                Tried to insert: %s of type %si   i    R   R   N(   R   R%   R   R#   R
   R   R   R   R   R   R!   R   (   t   iterableR   t   layerR   t   return_listR   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyR#   Q  s     c         c` sR   t  | t ƒ s t d ƒ ‚ x0 t d t |  ƒ | ƒ D] } |  | | | !Vq4 Wd S(   u-   Yield successive n-sized chunks from list, l.u   ncols must be an integeri    N(   R   t   intt   AssertionErrorR^   R[   (   t   lR3   Rc   (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyR-   e  s    (   u   columnu   gridu   gridplotu   GridSpecu   layoutu   rowu   Spaceru	   widgetbox(-   R~   t
   __future__R    R   R   R   t   loggingt	   getLoggerR|   t   logR\   t   collectionsR   t   sixR   t
   core.enumsR   t   models.toolsR   R   t   models.plotsR	   t   models.layoutsR
   R   R   R   R   R   R   t   __all__R   R!   R"   R$   R   R`   R<   R:   t   objectRd   R   R   R#   R-   (    (    (    s,   lib/python2.7/site-packages/bokeh/layouts.pyt   <module>	   s<   "4       	-	-	%	*	u¨>		