Skip to content

Commit

Permalink
Eliminate WidgetBox
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 6, 2018
1 parent d735794 commit 6662343
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 92 deletions.
46 changes: 7 additions & 39 deletions panel/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import param

from bokeh.layouts import (Column as BkColumn, Row as BkRow,
WidgetBox as BkWidgetBox, Spacer as BkSpacer)
from bokeh.layouts import (Column as BkColumn, Row as BkRow)
from bokeh.models import Spacer as BkSpacer
from bokeh.models.widgets import Tabs as BkTabs, Panel as BkPanel

from .pane import Pane, PaneBase
Expand All @@ -20,6 +20,10 @@ class Layout(Reactive):
Abstract baseclass for a layout of Panes.
"""

height = param.Integer(default=None, bounds=(0, None))

width = param.Integer(default=None, bounds=(0, None))

objects = param.List(default=[], doc="""
The list of child objects that make up the layout.""")

Expand Down Expand Up @@ -82,7 +86,7 @@ def _get_objects(self, model, old_objects, doc, root, comm=None):
return new_models

def _get_model(self, doc, root=None, parent=None, comm=None):
model = self._bokeh_model()
model = self._bokeh_model(width=self.width, height=self.height)
root = model if root is None else root
objects = self._get_objects(model, [], doc, root, comm)
props = dict(self._init_properties(), objects=objects)
Expand Down Expand Up @@ -130,42 +134,6 @@ class Column(Layout):
_bokeh_model = BkColumn


class WidgetBox(Layout):
"""
Box to group widgets.
"""

height = param.Integer(default=None, bounds=(0, None))

width = param.Integer(default=None, bounds=(0, None))

_bokeh_model = BkWidgetBox

def _get_objects(self, model, old_objects, doc, root, comm=None):
"""
Returns new child models for the layout while reusing unchanged
models and cleaning up any dropped objects.
"""
old_children = getattr(model, self._rename.get('objects', 'objects'))
new_models = []
for i, pane in enumerate(self.objects):
pane = Pane(pane)
self.objects[i] = pane
if pane in old_objects:
child = old_children[old_objects.index(pane)]
else:
child = pane._get_model(doc, root, model, comm)
if isinstance(child, BkWidgetBox):
new_models += child.children
else:
new_models.append(child)

for pane, old_child in zip(old_objects, old_children):
if old_child not in new_models:
pane._cleanup(old_child)
return new_models


class Tabs(Layout):
"""
Tabs allows selecting between the supplied panes.
Expand Down
14 changes: 9 additions & 5 deletions panel/pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import param

from bokeh.layouts import Row as _BkRow, WidgetBox as _BkWidgetBox
from bokeh.layouts import Row as _BkRow
from bokeh.models import LayoutDOM, CustomJS, Widget as _BkWidget, Div as _BkDiv

from .util import basestring, unicode, get_method_owner, push, remove_root, Div
from .util import basestring, unicode, get_method_owner, push, remove_root
from .viewable import Reactive, Viewable


Expand Down Expand Up @@ -123,6 +123,7 @@ def update_pane(change, history=[model]):
if self._updates:
def update_models():
self._update(old_model)

if comm:
update_models()
push(doc, comm)
Expand Down Expand Up @@ -166,7 +167,7 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
if isinstance(model, _BkWidget):
box_kws = {k: getattr(model, k) for k in ['width', 'height', 'sizing_mode']
if k in model.properties()}
model = _BkWidgetBox(model, **box_kws)
model = _BkColumn(model, **box_kws)

if root:
plot_id = root.ref['id']
Expand Down Expand Up @@ -227,6 +228,9 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
Should return the Bokeh model to be rendered.
"""
from holoviews import Store
if not Store.renderers:
import holoviews.plotting.bokeh
Store.current_backend == 'bokeh'
renderer = Store.renderers[Store.current_backend]
renderer = renderer.instance(mode='server' if comm is None else 'default')
kwargs = {'doc': doc} if renderer.backend == 'bokeh' else {}
Expand Down Expand Up @@ -340,7 +344,7 @@ def _get_properties(self):
if getattr(self,p) is not None}

def _get_model(self, doc, root=None, parent=None, comm=None):
model = Div(**self._get_properties())
model = _BkDiv(**self._get_properties())
self._link_object(model, doc, root, parent, comm)
return model

Expand Down Expand Up @@ -399,7 +403,7 @@ def _get_properties(self):
width, height = self._imgshape(data)
if self.width is None: p["width"] = width
if self.height is None: p["height"] = height
p["text"]=html
p["text"] = html
return p


Expand Down
12 changes: 4 additions & 8 deletions panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from param.parameterized import classlist

from .pane import PaneBase
from .layout import WidgetBox, Row, Layout, Tabs, Spacer
from .layout import Row, Column, Layout, Tabs, Spacer
from .util import default_label_formatter
from .widgets import (
LiteralInput, Select, Checkbox, FloatSlider, IntSlider, RangeSlider,
Expand Down Expand Up @@ -77,17 +77,13 @@ def __init__(self, object, **params):
params['name'] = object.name
super(Param, self).__init__(object, **params)
self._widgets = self._get_widgets()
self._widget_box = WidgetBox(*self._widgets.values(), height=self.height,
width=self.width, name=self.name)
if self.height is not None:
panes = [self._widget_box]
else:
panes = [Row(self._widget_box, Spacer(height=self.height), name=self.name)]
self._widget_box = Column(*self._widgets.values(), height=self.height,
width=self.width, name=self.name)

kwargs = {'name': self.name}
if self.subpanel_layout is Tabs:
kwargs['width'] = self.width
self._layout = self.subpanel_layout(*panes, **kwargs)
self._layout = self.subpanel_layout(self._widget_box, **kwargs)
self._link_subpanels()

def _link_subpanels(self):
Expand Down
5 changes: 2 additions & 3 deletions panel/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import pytest

from bokeh.models import (Div, Row as BkRow, Tabs as BkTabs,
Column as BkColumn, Panel as BkPanel,
WidgetBox as BkWidgetBox)
Column as BkColumn, Panel as BkPanel)
from panel.layout import Column, Row, Tabs, Spacer
from panel.pane import Bokeh, Pane


def get_div(box):
# Temporary utilities to unpack widget boxes
assert isinstance(box, BkWidgetBox)
assert isinstance(box, BkColumn)
return box.children[0]


Expand Down
14 changes: 7 additions & 7 deletions panel/tests/test_panes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import param
from bokeh.models import (Div, Row as BkRow, WidgetBox as BkWidgetBox,
from bokeh.models import (Div, Row as BkRow, Column as BkColumn,
GlyphRenderer, Circle, Line)
from bokeh.plotting import Figure
from panel.pane import (Pane, PaneBase, Bokeh, HoloViews, Matplotlib,
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_holoviews_pane_mpl_renderer(document, comm):
assert len(row.children) == 1
assert len(pane._callbacks) == 1
model = row.children[0]
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div = model.children[0]
assert isinstance(div, Div)
assert '<img' in div.text
Expand All @@ -85,7 +85,7 @@ def test_holoviews_pane_mpl_renderer(document, comm):
scatter = hv.Scatter([1, 2, 3])
pane.object = scatter
model = row.children[0]
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div2 = model.children[0]
assert isinstance(div2, Div)
assert div2.text != div.text
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_matplotlib_pane(document, comm):
assert len(row.children) == 1
assert len(pane._callbacks) == 1
model = row.children[0]
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div = model.children[0]
assert isinstance(div, Div)
assert '<img' in div.text
Expand All @@ -151,7 +151,7 @@ def test_matplotlib_pane(document, comm):
# Replace Pane.object
pane.object = mpl_figure()
model = row.children[0]
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div2 = model.children[0]
assert div is div2
assert div.text != text
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_param_method_pane_mpl(document, comm):
assert len(row.children) == 1
model = row.children[0]
assert model.ref['id'] in inner_pane._callbacks
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div = model.children[0]
assert isinstance(div, Div)
text = div.text
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_param_method_pane_changing_type(document, comm):
assert len(row.children) == 1
model = row.children[0]
assert model.ref['id'] in inner_pane._callbacks
assert isinstance(model, BkWidgetBox)
assert isinstance(model, BkColumn)
div = model.children[0]
assert isinstance(div, Div)
text = div.text
Expand Down
30 changes: 15 additions & 15 deletions panel/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from bokeh.layouts import WidgetBox
from bokeh.layouts import Column
from bokeh.models import Div as BkDiv, Slider as BkSlider
from panel.widgets import (
TextInput, StaticText, FloatSlider, IntSlider, RangeSlider,
Expand All @@ -15,7 +15,7 @@ def test_text_input(document, comm):

box = text._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, text._widget_type)
Expand All @@ -35,7 +35,7 @@ def test_static_text(document, comm):

box = text._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, text._widget_type)
Expand All @@ -51,7 +51,7 @@ def test_float_slider(document, comm):

box = slider._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, slider._widget_type)
Expand All @@ -74,7 +74,7 @@ def test_int_slider(document, comm):

box = slider._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, slider._widget_type)
Expand All @@ -97,7 +97,7 @@ def test_range_slider(document, comm):

box = slider._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, slider._widget_type)
Expand All @@ -120,7 +120,7 @@ def test_literal_input(document, comm):

box = literal._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, literal._widget_type)
Expand All @@ -146,7 +146,7 @@ def test_checkbox(document, comm):

box = checkbox._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, checkbox._widget_type)
Expand All @@ -171,7 +171,7 @@ def test_select(document, comm):

box = select._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, select._widget_type)
Expand All @@ -197,7 +197,7 @@ def test_multi_select(document, comm):

box = select._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, select._widget_type)
Expand All @@ -222,7 +222,7 @@ def test_button(document, comm):

box = button._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, button._widget_type)
Expand All @@ -239,7 +239,7 @@ def test_toggle(document, comm):

box = toggle._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, toggle._widget_type)
Expand All @@ -260,7 +260,7 @@ def test_date_picker(document, comm):

box = date_picker._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, date_picker._widget_type)
Expand All @@ -284,7 +284,7 @@ def test_date_range_slider(document, comm):

box = date_slider._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

widget = box.children[0]
assert isinstance(widget, date_slider._widget_type)
Expand All @@ -310,7 +310,7 @@ def test_discrete_slider(document, comm):

box = discrete_slider._get_model(document, comm=comm)

assert isinstance(box, WidgetBox)
assert isinstance(box, Column)

label = box.children[0]
widget = box.children[1]
Expand Down
Loading

0 comments on commit 6662343

Please sign in to comment.