Skip to content

Commit

Permalink
Initial fixes for bokeh 0.12 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jun 20, 2016
1 parent 16d97f4 commit dfffcb7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 45 deletions.
20 changes: 9 additions & 11 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from collections import defaultdict

import numpy as np
from bokeh.models import CustomJS, TapTool, ColumnDataSource

try:
from bokeh.protocol import serialize_json
bokeh_lt_011 = True
except ImportError:
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
from .renderer import bokeh_version
from .util import models_to_json, bokeh_version

from bokeh.models import CustomJS, TapTool, ColumnDataSource
if bokeh_version < '0.11':
from bokeh.protocol import serialize_json
else:
from bokeh.core.json_encoder import serialize_json


class Callback(param.ParameterizedFunction):
Expand Down Expand Up @@ -152,7 +150,7 @@ def serialize(self, objects):
Serializes any Bokeh plot objects passed to it as a list.
"""
data = dict(data=models_to_json(objects))
if not bokeh_lt_011:
if bokeh_version >= '0.11':
plot = self.plots[0]
data['root'] = plot.state._id
return serialize_json(data)
Expand Down
11 changes: 4 additions & 7 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from ..util import dynamic_update
from .callbacks import Callbacks
from .plot import BokehPlot
from .renderer import bokeh_lt_011
from .util import mpl_to_bokeh, convert_datetime, update_plot
from .util import bokeh_version, mpl_to_bokeh, convert_datetime, update_plot


# Define shared style properties for bokeh plots
Expand Down Expand Up @@ -281,14 +280,12 @@ def _plot_properties(self, key, plot, element):
"""
Returns a dictionary of plot properties.
"""
title_font = self._fontsize('title', 'title_text_font_size')
plot_props = dict(plot_height=self.height, plot_width=self.width,
title_text_color='black', **title_font)
plot_props = dict(plot_height=self.height, plot_width=self.width)
if self.show_title:
plot_props['title'] = self._format_title(key, separator=' ')
if self.bgcolor:
bg_attr = 'background_fill'
if not bokeh_lt_011: bg_attr += '_color'
if bokeh_version > '0.11': bg_attr += '_color'
plot_props[bg_attr] = self.bgcolor
if self.border is not None:
for p in ['left', 'right', 'top', 'bottom']:
Expand Down Expand Up @@ -679,7 +676,7 @@ def _process_legend(self):
if legend_fontsize:
plot.legend[0].label_text_font_size = legend_fontsize

if bokeh_lt_011:
if bokeh_version < '0.11':
plot.legend.orientation = self.legend_position
else:
plot.legend.location = self.legend_position
Expand Down
15 changes: 12 additions & 3 deletions holoviews/plotting/bokeh/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
from ..plot import DimensionedPlot, GenericCompositePlot, GenericLayoutPlot
from ..util import get_dynamic_mode, initialize_sampled
from .renderer import BokehRenderer
from .util import layout_padding
from .util import bokeh_version, layout_padding

if bokeh_version >= '0.12':
from bokeh.layouts import gridplot


class BokehPlot(DimensionedPlot):
"""
Expand Down Expand Up @@ -261,7 +265,10 @@ def initialize_plot(self, ranges=None, plots=[]):
else:
plots[r].append(None)
passed_plots.append(None)
self.handles['plot'] = BokehGridPlot(children=plots[::-1])
if bokeh_version < '0.12':
self.handles['plot'] = BokehGridPlot(children=plots[::-1])
else:
self.handles['plot'] = gridplot(plots[::-1])
self.handles['plots'] = plots
if self.shared_datasource:
self.sync_sources()
Expand Down Expand Up @@ -481,7 +488,7 @@ def initialize_plot(self, ranges=None):

# Replace None types with empty plots
# to avoid bokeh bug
if adjoined:
if adjoined and bokeh_version < '0.12':
plots = layout_padding(plots)

# Determine the most appropriate composite plot type
Expand All @@ -496,6 +503,8 @@ def initialize_plot(self, ranges=None):
for c, child in enumerate(row)
if child is not None]
layout_plot = Tabs(tabs=panels)
elif bokeh_version >= '0.12':
layout_plot = gridplot(children=plots)
elif len(plots) == 1 and not adjoined:
layout_plot = VBox(children=[HBox(children=plots[0])])
elif len(plots[0]) == 1:
Expand Down
16 changes: 8 additions & 8 deletions holoviews/plotting/bokeh/renderer.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import uuid

from ...core import Store, HoloMap
from ..renderer import Renderer, MIME_TYPES
from .widgets import BokehScrubberWidget, BokehSelectionWidget
from .util import models_to_json
from .util import bokeh_version, models_to_json

import param
from param.parameterized import bothmethod

import bokeh
from bokeh.embed import notebook_div
from bokeh.io import load_notebook
from bokeh.resources import CDN, INLINE

try:
if bokeh_version < '0.11':
from bokeh.protocol import serialize_json
bokeh_lt_011 = True
except ImportError:
else:
from bokeh.core.json_encoder import serialize_json
from bokeh.model import _ModelInDocument as add_to_document
bokeh_lt_011 = False


class BokehRenderer(Renderer):
Expand Down Expand Up @@ -55,20 +55,20 @@ def __call__(self, obj, fmt=None):
return plot(), info
elif fmt == 'html':
html = self.figure_data(plot)
html = '<center>%s</center>' % html
html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html
return self._apply_post_render_hooks(html, obj, fmt), info
elif fmt == 'json':
plotobjects = [h for handles in plot.traverse(lambda x: x.current_handles)
for h in handles]
data = dict(data=[])
if not bokeh_lt_011:
if bokeh_version >= '0.11':
data['root'] = plot.state._id
data['data'] = models_to_json(plotobjects)
return self._apply_post_render_hooks(serialize_json(data), obj, fmt), info


def figure_data(self, plot, fmt='html', **kwargs):
if not bokeh_lt_011:
if bokeh_version >= '0.11':
doc_handler = add_to_document(plot.state)
with doc_handler:
doc = doc_handler._doc
Expand Down
16 changes: 9 additions & 7 deletions holoviews/plotting/bokeh/util.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
from distutils.version import LooseVersion

from collections import defaultdict
import numpy as np
from ...core.options import abbreviated_exception

try:
from matplotlib import colors
import matplotlib.cm as cm
except ImportError:
cm, colors = None, None

try:
import bokeh
bokeh_version = LooseVersion(bokeh.__version__)
if bokeh_version < '0.11':
from bokeh.enums import Palette
from bokeh.plotting import Plot
bokeh_lt_011 = True
except:
else:
from bokeh.core.enums import Palette
from bokeh.models.plots import Plot
bokeh_lt_011 = False

from bokeh.models import GlyphRenderer
from bokeh.plotting import Figure

from ...core.options import abbreviated_exception

# Conversion between matplotlib and bokeh markers
markers = {'s': {'marker': 'square'},
'd': {'marker': 'diamond'},
Expand Down Expand Up @@ -128,7 +130,7 @@ def models_to_json(models):
continue
else:
ids.append(plotobj.ref['id'])
if bokeh_lt_011:
if bokeh_version < '0.11':
json = plotobj.vm_serialize(changed_only=True)
else:
json = plotobj.to_json(False)
Expand Down
14 changes: 5 additions & 9 deletions holoviews/plotting/bokeh/widgets.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import json
from distutils.version import LooseVersion

from .util import bokeh_version
from ..widgets import NdWidget, SelectionWidget, ScrubberWidget

import param
import bokeh
from bokeh.io import Document

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

from ..widgets import NdWidget, SelectionWidget, ScrubberWidget


class BokehWidget(NdWidget):
Expand Down Expand Up @@ -43,7 +39,7 @@ def _plot_figure(self, idx, fig_format='json'):
first call and
"""
self.plot.update(idx)
if self.embed or fig_format == 'html' or bokeh_lt_011:
if self.embed or fig_format == 'html' or bokeh_version < '0.11':
return self.renderer.html(self.plot, fig_format)
else:
doc = self.plot.document
Expand Down

0 comments on commit dfffcb7

Please sign in to comment.