Skip to content

Commit

Permalink
Merge pull request #393 from ioam/bokeh0.11_compat
Browse files Browse the repository at this point in the history
Bokeh 0.11 Compatibility
  • Loading branch information
jlstevens committed Jan 7, 2016
2 parents 2304fd3 + 7834609 commit 817aead
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 50 deletions.
4 changes: 2 additions & 2 deletions holoviews/plotting/bokeh/bokehwidgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ var BokehMethods = {
}
if (data !== undefined) {
if (data.root !== undefined) {
doc = Bokeh.index[data.root].model.document;
var doc = Bokeh.index[data.root].model.document;
}
$.each(data.data, function(i, value) {
if (data.root !== undefined) {
ds = doc.get_model_by_id(value.id);
var ds = doc.get_model_by_id(value.id);
} else {
var ds = Bokeh.Collections(value.type).get(value.id);
}
Expand Down
23 changes: 8 additions & 15 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
try:
from bokeh.models import PlotObject
from bokeh.protocol import serialize_json
old_bokeh = True
bokeh_lt_011 = True
except ImportError:
from bokeh.models import Component as PlotObject
from bokeh._json_encoder import serialize_json
old_bokeh = False
from bokeh.core.json_encoder import serialize_json
bokeh_lt_011 = False

import param

from ...core.data import ArrayColumns
from .util import models_to_json


class Callback(param.ParameterizedFunction):
Expand Down Expand Up @@ -75,11 +76,11 @@ class Callback(param.ParameterizedFunction):
if (msg.msg_type == "execute_result") {
var data = JSON.parse(msg.content.data['text/plain'].slice(1, -1));
if (data.root !== undefined) {
doc = Bokeh.index[data.root].model.document;
var doc = Bokeh.index[data.root].model.document;
}
$.each(data.data, function(i, value) {
if (data.root !== undefined) {
ds = doc.get_model_by_id(value.id);
var ds = doc.get_model_by_id(value.id);
} else {
var ds = Bokeh.Collections(value.type).get(value.id);
}
Expand Down Expand Up @@ -153,18 +154,10 @@ def serialize(self, objects):
Serializes any Bokeh plot objects passed to it as a list.
"""
data = dict(data=[])
if not old_bokeh:
if not bokeh_lt_011:
plot = self.plot[0]
data['root'] = plot.state._id

for obj in objects:
if old_bokeh:
json = obj.vm_serialize(changed_only=True)
else:
json = obj.to_json(False)
data['data'].append({'id': obj.ref['id'], 'type': obj.ref['type'],
'data': json})
return serialize_json(data)
return serialize_json(models_to_json(objects))



Expand Down
12 changes: 8 additions & 4 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..util import dynamic_update
from .callbacks import Callbacks
from .plot import BokehPlot
from .renderer import old_bokeh
from .renderer import bokeh_lt_011
from .util import mpl_to_bokeh, convert_datetime


Expand Down Expand Up @@ -268,10 +268,11 @@ def _plot_properties(self, key, plot, element):
plot_props['title'] = self._format_title(key, separator=' ')
if self.bgcolor:
bg_attr = 'background_fill'
if not old_bokeh: bg_attr += ' _color'
if not bokeh_lt_011: bg_attr += '_color'
plot_props[bg_attr] = self.bgcolor
if self.border is not None:
plot_props['min_border'] = self.border
for p in ['left', 'right', 'top', 'bottom']:
plot_props['min_border_'+p] = self.border
lod = dict(self.defaults()['lod'], **self.lod)
for lod_prop, v in lod.items():
plot_props['lod_'+lod_prop] = v
Expand Down Expand Up @@ -623,7 +624,10 @@ def _process_legend(self):
if legend_fontsize:
plot.legend[0].label_text_font_size = legend_fontsize

plot.legend.orientation = self.legend_position
if bokeh_lt_011:
plot.legend.orientation = self.legend_position
else:
plot.legend.location = self.legend_position
legends = plot.legend[0].legends
new_legends = []
for label, l in legends:
Expand Down
38 changes: 15 additions & 23 deletions holoviews/plotting/bokeh/renderer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import uuid
from ...core import Store, HoloMap
from ..renderer import Renderer, MIME_TYPES
from .widgets import BokehScrubberWidget, BokehSelectionWidget
from .util import models_to_json

import param
from param.parameterized import bothmethod
Expand All @@ -11,10 +13,11 @@

try:
from bokeh.protocol import serialize_json
old_bokeh = True
bokeh_lt_011 = True
except ImportError:
from bokeh._json_encoder import serialize_json
old_bokeh = False
from bokeh.core.json_encoder import serialize_json
bokeh_lt_011 = False


class BokehRenderer(Renderer):

Expand Down Expand Up @@ -56,33 +59,22 @@ def __call__(self, obj, fmt=None):
plotobjects = [h for handles in plot.traverse(lambda x: x.current_handles)
for h in handles]
data = dict(data=[])
ids = []
if not old_bokeh:
if not bokeh_lt_011:
data['root'] = plot.state._id
json_data = []
for plotobj in plotobjects:
if plotobj.ref['id'] in ids:
continue
else:
ids.append(plotobj.ref['id'])
if old_bokeh:
json = plotobj.vm_serialize(changed_only=True)
else:
json = plotobj.to_json(False)
json_data.append({'id': plotobj.ref['id'],
'type': plotobj.ref['type'],
'data': json})
data['data'] = json_data
data['data'] = models_to_json(plotobjects)
return serialize_json(data), info


def figure_data(self, plot, fmt='html', **kwargs):
if not old_bokeh:
if not bokeh_lt_011:
doc = Document()
doc.add_root(plot.state)
plot.set_root(plot.state)
plot.set_document(doc)
return notebook_div(plot.state)
comms_target = str(uuid.uuid4())
doc.last_comms_target = comms_target
div = notebook_div(plot.state, comms_target)
return div
else:
return notebook_div(plot.state)


@classmethod
Expand Down
35 changes: 32 additions & 3 deletions holoviews/plotting/bokeh/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
except ImportError:
cm, colors = None, None

from bokeh.enums import Palette
from bokeh.plotting import figure, Plot
try:
from bokeh.enums import Palette
from bokeh.plotting import Plot
bokeh_lt_011 = True
except:
from bokeh.core.enums import Palette
from bokeh.models.plots import Plot
bokeh_lt_011 = False
from bokeh.plotting import Figure

# Conversion between matplotlib and bokeh markers
markers = {'s': {'marker': 'square'},
Expand Down Expand Up @@ -104,7 +111,7 @@ def layout_padding(plots):
expanded_plots.append([])
for c, p in enumerate(row):
if p is None:
p = figure(plot_width=widths[c],
p = Figure(plot_width=widths[c],
plot_height=heights[r])
p.text(x=0, y=0, text=[' '])
p.xaxis.visible = False
Expand All @@ -118,3 +125,25 @@ def layout_padding(plots):

def convert_datetime(time):
return time.astype('datetime64[s]').astype(float)*1000


def models_to_json(models):
"""
Convert list of bokeh models into json to update plot(s).
"""
json_data, ids = [], []
for plotobj in models:
if plotobj.ref['id'] in ids:
continue
else:
ids.append(plotobj.ref['id'])
if bokeh_lt_011:
json = plotobj.vm_serialize(changed_only=True)
else:
json = plotobj.to_json(False)
json.pop('tool_events', None)
json.pop('renderers', None)
json_data.append({'id': plotobj.ref['id'],
'type': plotobj.ref['type'],
'data': json})
return json_data
34 changes: 32 additions & 2 deletions holoviews/plotting/bokeh/widgets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import json
from distutils.version import LooseVersion

import param
import bokeh
from bokeh.io import Document

if LooseVersion(bokeh.__version__) >= LooseVersion('0.11'):
bokeh_lt_011 = False
from bokeh.io import push_notebook, _CommsHandle
from bokeh.util.notebook import get_comms
else:
bokeh_lt_011 = True

from ..widgets import NdWidget, SelectionWidget, ScrubberWidget


class BokehWidget(NdWidget):

css = param.String(default='bokehwidgets.css', doc="""
Expand Down Expand Up @@ -30,8 +42,26 @@ def _plot_figure(self, idx, fig_format='json'):
Returns the figure in html format on the
first call and
"""
self.plot.update(idx)
return self.renderer.html(self.plot, fig_format)
state = self.plot.update(idx)
if self.embed or fig_format == 'html' or bokeh_lt_011:
return self.renderer.html(self.plot, fig_format)
else:
doc = state.document

if hasattr(doc, 'last_comms_handle'):
handle = doc.last_comms_handle
else:
handle = _CommsHandle(get_comms(doc.last_comms_target),
doc, doc.to_json())
doc.last_comms_handle = handle

to_json = doc.to_json()
if handle.doc is not doc:
msg = dict(doc=to_json)
else:
msg = Document._compute_patch_between_json(handle.json, to_json)
handle._json = to_json
handle.comms.send(json.dumps(msg))


class BokehSelectionWidget(BokehWidget, SelectionWidget):
Expand Down
2 changes: 1 addition & 1 deletion holoviews/plotting/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_frames(self):
frames = OrderedDict([(idx, self._plot_figure(idx))
for idx in range(len(self.plot))])
else:
frames = {0: self._plot_figure(0)}
frames = {}
return self.encode_frames(frames)


Expand Down

0 comments on commit 817aead

Please sign in to comment.