Skip to content

Commit

Permalink
Undo widget nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 3, 2019
1 parent b34a869 commit 7a58f98
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 160 deletions.
4 changes: 3 additions & 1 deletion panel/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ class Spacer(Reactive):

_bokeh_model = BkSpacer

def _get_model(self, doc, root, parent, comm=None):
def _get_model(self, doc, root=None, parent=None, comm=None):
model = self._bokeh_model(**self._process_param_change(self._init_properties()))
if root is None:
root = model
self._models[root.ref['id']] = model
self._link_params(model, ['width', 'height'], doc, root, comm)
return model
Expand Down
8 changes: 3 additions & 5 deletions panel/tests/test_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,15 @@ def test(a):

column = interact_pane.layout._get_model(document, comm=comm)
assert isinstance(column, BkColumn)
box = column.children[1].children[0]
assert isinstance(box, BkColumn)
div = box.children[0]
div = column.children[1].children[0]
assert isinstance(div, BkDiv)
assert div.text == 'Test'
assert len(interact_pane._callbacks['instance']) == 1
assert column.ref['id'] in pane._callbacks
assert pane._models[column.ref['id']] is box
assert pane._models[column.ref['id']] is div

widget.value = True
assert box.ref['id'] not in pane._callbacks
assert column.ref['id'] not in pane._callbacks
assert pane._callbacks == {}
new_pane = interact_pane._pane
assert new_pane is not pane
Expand Down
87 changes: 39 additions & 48 deletions panel/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ def test_layout_select_by_function(panel):


@pytest.mark.parametrize(['panel', 'model_type'], [(Column, BkColumn), (Row, BkRow)])
def test_layout_get_model(panel, model_type, document, comm):
def test_layout_get_root(panel, model_type, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

assert isinstance(model, model_type)
children = model.children
assert get_divs(children[0].children) == [div1]
assert get_divs(children[1].children) == [div2]
assert model.children == [div1, div2]


@pytest.mark.parametrize('panel', [Column, Row])
Expand All @@ -93,13 +91,11 @@ def test_layout_append(panel, document, comm):
div2 = Div()
layout = panel(div1, div2)

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

div3 = Div()
layout.append(div3)
assert get_divs(model.children[0].children) == [div1]
assert get_divs(model.children[1].children) == [div2]
assert get_divs(model.children[2].children) == [div3]
assert model.children == [div1, div2, div3]


@pytest.mark.parametrize('panel', [Column, Row])
Expand All @@ -108,13 +104,11 @@ def test_layout_insert(panel, document, comm):
div2 = Div()
layout = panel(div1, div2)

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

div3 = Div()
layout.insert(1, div3)
assert get_divs(model.children[0].children) == [div1]
assert get_divs(model.children[1].children) == [div3]
assert get_divs(model.children[2].children) == [div2]
assert model.children == [div1, div3, div2]


@pytest.mark.parametrize('panel', [Column, Row])
Expand All @@ -124,14 +118,13 @@ def test_layout_setitem(panel, document, comm):
layout = panel(div1, div2)
p1, p2 = layout.objects

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is model.children[0]
div3 = Div()
layout[0] = div3
assert get_divs(model.children[0].children) == [div3]
assert get_divs(model.children[1].children) == [div2]
assert model.children == [div3, div2]
assert p1._callbacks == {}
assert p1._models == {}

Expand All @@ -143,12 +136,12 @@ def test_layout_pop(panel, document, comm):
layout = panel(div1, div2)
p1, p2 = layout.objects

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is model.children[0]
layout.pop(0)
assert get_divs(model.children[0].children) == [div2]
assert model.children == [div2]
assert p1._callbacks == {}
assert p1._models == {}

Expand All @@ -160,12 +153,12 @@ def test_layout_remove(panel, document, comm):
layout = panel(div1, div2)
p1, p2 = layout.objects

model = layout._get_model(document, comm=comm)
model = layout._get_root(document, comm=comm)

assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is model.children[0]
layout.remove(p1)
assert get_divs(model.children[0].children) == [div2]
assert model.children == [div2]
assert p1._callbacks == {}
assert p1._models == {}

Expand All @@ -176,17 +169,17 @@ def test_tabs_constructor(document, comm):
tabs = Tabs(('Div1', div1), ('Div2', div2))
p1, p2 = tabs.objects

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

assert isinstance(model, BkTabs)
assert len(model.tabs) == 2
assert all(isinstance(c, BkPanel) for c in model.tabs)
tab1, tab2 = model.tabs

assert tab1.title == 'Div1'
assert get_div(tab1.child.children[0]) is div1
assert tab1.child is div1
assert tab2.title == 'Div2'
assert get_div(tab2.child.children[0]) is div2
assert tab2.child is div2


def test_tabs_implicit_constructor(document, comm):
Expand All @@ -195,17 +188,17 @@ def test_tabs_implicit_constructor(document, comm):
p2 = Pane(div2, name='Div2')
tabs = Tabs(p1, p2)

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

assert isinstance(model, BkTabs)
assert len(model.tabs) == 2
assert all(isinstance(c, BkPanel) for c in model.tabs)
tab1, tab2 = model.tabs

assert tab1.title == 'Div1'
assert get_div(tab1.child.children[0]) is div1
assert tab1.child is div1
assert tab2.title == 'Div2'
assert get_div(tab2.child.children[0]) is div2
assert tab2.child is div2


def test_tabs_set_panes(document, comm):
Expand All @@ -214,7 +207,7 @@ def test_tabs_set_panes(document, comm):
p2 = Pane(div2, name='Div2')
tabs = Tabs(p1, p2)

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

div3 = Div()
p3 = Pane(div3, name='Div3')
Expand All @@ -226,11 +219,11 @@ def test_tabs_set_panes(document, comm):
tab1, tab2, tab3 = model.tabs

assert tab1.title == 'Div1'
assert get_div(tab1.child.children[0]) is div1
assert tab1.child is div1
assert tab2.title == 'Div2'
assert get_div(tab2.child.children[0]) is div2
assert tab2.child is div2
assert tab3.title == 'Div3'
assert get_div(tab3.child.children[0]) is div3
assert tab3.child is div3


def test_tabs_append(document, comm):
Expand All @@ -239,7 +232,7 @@ def test_tabs_append(document, comm):
p2 = Pane(div2, name='Div2')
tabs = Tabs(p1, p2)

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

div3 = Div()
tabs.append(div3)
Expand All @@ -251,14 +244,14 @@ def test_tabs_insert(document, comm):
div2 = Div()
tabs = Tabs(div1, div2)

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

div3 = Div()
tabs.insert(1, div3)
tab1, tab2, tab3 = model.tabs
assert get_div(tab1.child.children[0]) is div1
assert get_div(tab2.child.children[0]) is div3
assert get_div(tab3.child.children[0]) is div2
assert tab1.child is div1
assert tab2.child is div3
assert tab3.child is div2


def test_tabs_setitem(document, comm):
Expand All @@ -267,16 +260,16 @@ def test_tabs_setitem(document, comm):
tabs = Tabs(div1, div2)
p1, p2 = tabs.objects

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

tab1, tab2 = model.tabs
assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is tab1.child
div3 = Div()
tabs[0] = div3
tab1, tab2 = model.tabs
assert get_div(tab1.child.children[0]) is div3
assert get_div(tab2.child.children[0]) is div2
assert tab1.child is div3
assert tab2.child is div2
assert p1._callbacks == {}
assert p1._models == {}

Expand All @@ -287,15 +280,15 @@ def test_tabs_pop(document, comm):
tabs = Tabs(div1, div2)
p1, p2 = tabs.objects

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

tab1 = model.tabs[0]
assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is tab1.child
tabs.pop(0)
assert len(model.tabs) == 1
tab1 = model.tabs[0]
assert get_div(tab1.child.children[0]) is div2
assert tab1.child is div2
assert p1._callbacks == {}
assert p1._models == {}

Expand All @@ -306,26 +299,24 @@ def test_tabs_remove(document, comm):
tabs = Tabs(div1, div2)
p1, p2 = tabs.objects

model = tabs._get_model(document, comm=comm)
model = tabs._get_root(document, comm=comm)

tab1 = model.tabs[0]
assert model.ref['id'] in p1._callbacks
assert p1._models[model.ref['id']] is tab1.child
tabs.remove(p1)
assert len(model.tabs) == 1
tab1 = model.tabs[0]
assert get_div(tab1.child.children[0]) is div2
assert tab1.child is div2
assert p1._callbacks == {}
assert p1._models == {}


def test_spacer(document, comm):

spacer = Spacer(width=400, height=300)

root = spacer._get_root(document, comm=comm)
model = root.children[0]

model = spacer._get_root(document, comm=comm)

assert isinstance(model, spacer._bokeh_model)
assert model.width == 400
assert model.height == 300
Expand All @@ -349,7 +340,7 @@ def _load(self):
self._layout[-1] = self.select

test = TestClass()
model = test._layout._get_model(document, comm=comm)
model = test._layout._get_root(document, comm=comm)
test.select = 1
assert model.children[1].text == '<pre>1</pre>'

Loading

0 comments on commit 7a58f98

Please sign in to comment.