Skip to content

Commit

Permalink
Allow constructing empty HoloViews pane (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 15, 2019
1 parent 2a4ccc7 commit b7c3a49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
28 changes: 18 additions & 10 deletions panel/pane/holoviews.py
Expand Up @@ -10,6 +10,7 @@
from functools import partial

import param
from bokeh.models import Spacer as _BkSpacer

from ..io import state
from ..layout import Panel, Column
Expand Down Expand Up @@ -45,7 +46,7 @@ class HoloViews(PaneBase):

_panes = {'bokeh': Bokeh, 'matplotlib': Matplotlib, 'plotly': Plotly}

def __init__(self, object, **params):
def __init__(self, object=None, **params):
super(HoloViews, self).__init__(object, **params)
self.widget_box = Column()
self._update_widgets()
Expand Down Expand Up @@ -114,14 +115,17 @@ def _get_model(self, doc, root=None, parent=None, comm=None):
if root is None:
return self._get_root(doc, comm)
ref = root.ref['id']
plot = self._render(doc, comm, root)
child_pane = self._panes.get(self.backend, Pane)(plot.state)
model = child_pane._get_model(doc, root, parent, comm)
if ref in self._plots:
old_plot, old_pane = self._plots[ref]
old_plot.comm = None # Ensure comm does not cleaned up
old_plot.cleanup()
self._plots[ref] = (plot, child_pane)
if self.object is None:
model = _BkSpacer()
else:
plot = self._render(doc, comm, root)
child_pane = self._panes.get(self.backend, Pane)(plot.state)
model = child_pane._get_model(doc, root, parent, comm)
if ref in self._plots:
old_plot, old_pane = self._plots[ref]
old_plot.comm = None # Ensure comm does not cleaned up
old_plot.cleanup()
self._plots[ref] = (plot, child_pane)
self._models[ref] = (model, parent)
return model

Expand Down Expand Up @@ -245,9 +249,13 @@ def generate_panel_bokeh_map(root_model, panel_views):
mapping panel elements to its bokeh models
"""
map_hve_bk = defaultdict(list)
ref = root_model.ref['id']
for pane in panel_views:
if root_model.ref['id'] in pane._models:
bk_plots = pane._plots[root_model.ref['id']][0].traverse(lambda x: x, [is_bokeh_element_plot])
plot, subpane = pane._plots.get(ref, (None, None))
if plot is None:
continue
bk_plots = plot.traverse(lambda x: x, [is_bokeh_element_plot])
for plot in bk_plots:
for hv_elem in plot.link_sources:
map_hve_bk[hv_elem].append(plot)
Expand Down
20 changes: 19 additions & 1 deletion panel/tests/test_holoviews.py
Expand Up @@ -7,7 +7,7 @@

from bokeh.models import (Row as BkRow, Column as BkColumn, GlyphRenderer,
Scatter, Line, GridBox, Select as BkSelect,
Slider as BkSlider)
Slider as BkSlider, Spacer as BkSpacer)
from bokeh.plotting import Figure

from panel.layout import Column, Row
Expand Down Expand Up @@ -113,6 +113,24 @@ def test_holoviews_pane_bokeh_renderer(document, comm):
assert pane._models == {}


@pytest.mark.usefixtures("hv_bokeh")
@hv_available
def test_holoviews_pane_initialize_empty(document, comm):
pane = HoloViews()

# Create pane
row = pane._get_root(document, comm=comm)

assert isinstance(row, BkRow)
assert len(row.children) == 1
model = row.children[0]
assert isinstance(model, BkSpacer)

pane.object = hv.Curve([1, 2, 3])
model = row.children[0]
assert isinstance(model, Figure)


@hv_available
def test_holoviews_widgets_from_dynamicmap(document, comm):
range_dim = hv.Dimension('A', range=(0, 10.))
Expand Down

0 comments on commit b7c3a49

Please sign in to comment.