Skip to content

Commit

Permalink
Decode bytes in bokeh plots (#2357)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 1, 2018
1 parent 36c8bc8 commit 3a1813b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions holoviews/plotting/bokeh/element.py
Expand Up @@ -25,7 +25,8 @@
from ..util import dynamic_update, process_cmap
from .plot import BokehPlot, TOOLS
from .util import (mpl_to_bokeh, get_tab_title, py2js_tickformatter,
rgba_tuple, recursive_model_update, glyph_order)
rgba_tuple, recursive_model_update, glyph_order,
decode_bytes)

property_prefixes = ['selection', 'nonselection', 'muted', 'hover']

Expand Down Expand Up @@ -571,7 +572,7 @@ def _update_range(self, axis_range, low, high, factors, invert, shared, log, str
if streaming:
axis_range.trigger(k, old, new)
elif isinstance(axis_range, FactorRange):
factors = list(factors)
factors = list(decode_bytes(factors))
if invert: factors = factors[::-1]
axis_range.factors = factors

Expand Down Expand Up @@ -1156,6 +1157,7 @@ def _get_cmapper_opts(self, low, high, factors, colors):
opts.update({opt: colors[name] for name, opt in color_opts if name in colors})
else:
colormapper = CategoricalColorMapper
factors = decode_bytes(factors)
opts = dict(factors=factors)
if 'NaN' in colors:
opts['nan_color'] = colors['NaN']
Expand Down
4 changes: 3 additions & 1 deletion holoviews/plotting/bokeh/plot.py
Expand Up @@ -15,7 +15,7 @@
GenericElementPlot, GenericOverlayPlot)
from ..util import attach_streams
from .util import (layout_padding, pad_plots, filter_toolboxes, make_axis,
update_shared_sources, empty_plot)
update_shared_sources, empty_plot, decode_bytes)

from bokeh.layouts import gridplot
from bokeh.plotting.helpers import _known_tools as known_tools
Expand Down Expand Up @@ -165,13 +165,15 @@ def _init_datasource(self, data):
"""
Initializes a data source to be passed into the bokeh glyph.
"""
data = {k: decode_bytes(vs) for k, vs in data.items()}
return ColumnDataSource(data=data)


def _update_datasource(self, source, data):
"""
Update datasource with data for a new frame.
"""
data = {k: decode_bytes(vs) for k, vs in data.items()}
if (self.streaming and self.streaming[0].data is self.current_frame.data
and self._stream_data):
stream = self.streaming[0]
Expand Down
18 changes: 17 additions & 1 deletion holoviews/plotting/bokeh/util.py
@@ -1,4 +1,4 @@
import inspect, re, time
import inspect, re, time, sys
from distutils.version import LooseVersion
from collections import defaultdict
import datetime as dt
Expand Down Expand Up @@ -66,6 +66,22 @@ def rgba_tuple(rgba):
return rgba


def decode_bytes(array):
"""
Decodes an array, list or tuple of bytestrings to avoid python 3
bokeh serialization errors
"""
if (sys.version_info.major == 2 or not len(array) or
(isinstance(array, np.ndarray) and array.dtype.kind != 'O')):
return array
decoded = [v.decode('utf-8') if isinstance(v, bytes) else v for v in array]
if isinstance(array, np.ndarray):
return np.asarray(decoded)
elif isinstance(array, tuple):
return tuple(decoded)
return decoded


def get_cmap(cmap):
"""
Returns matplotlib cmap generated from bokeh palette or
Expand Down

0 comments on commit 3a1813b

Please sign in to comment.