Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for static bokeh png rendering #1493

Merged
merged 2 commits into from May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -29,7 +29,7 @@ install:
- conda info -a
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION scipy=0.18.1 numpy freetype nose bokeh=0.12.5 pandas=0.19.2 jupyter ipython=4.2.0 param pyqt=4 matplotlib=1.5.1 xarray datashader dask=0.13
- source activate test-environment
- conda install -c conda-forge -c scitools iris sip=4.18 plotly flexx
- conda install -c conda-forge iris sip=4.18 plotly flexx
- if [[ "$TRAVIS_PYTHON_VERSION" == "3.4" ]]; then
conda install python=3.4.3;
fi
Expand Down
14 changes: 12 additions & 2 deletions holoviews/plotting/bokeh/renderer.py
@@ -1,3 +1,5 @@
from io import BytesIO

import numpy as np
import param
from param.parameterized import bothmethod
Expand Down Expand Up @@ -25,7 +27,7 @@ class BokehRenderer(Renderer):

backend = param.String(default='bokeh', doc="The backend name.")

fig = param.ObjectSelector(default='auto', objects=['html', 'json', 'auto'], doc="""
fig = param.ObjectSelector(default='auto', objects=['html', 'json', 'auto', 'png'], doc="""
Output render format for static figures. If None, no figure
rendering will occur. """)

Expand All @@ -42,7 +44,7 @@ class BokehRenderer(Renderer):
bokeh server app. By default renders all output is rendered to HTML.""")

# Defines the valid output formats for each mode.
mode_formats = {'fig': {'default': ['html', 'json', 'auto'],
mode_formats = {'fig': {'default': ['html', 'json', 'auto', 'png'],
'server': ['html', 'json', 'auto']},
'holomap': {'default': ['widgets', 'scrubber', 'auto', None],
'server': ['server', 'auto', None]}}
Expand Down Expand Up @@ -75,6 +77,14 @@ def __call__(self, obj, fmt=None, doc=None):
return self.server_doc(plot, doc), info
elif isinstance(plot, tuple(self.widgets.values())):
return plot(), info
elif fmt == 'png':
if bokeh_version < '0.12.6':
raise RuntimeError('Bokeh png export only supported by versions >=0.12.6.')
from bokeh.io import _get_screenshot_as_png
img = _get_screenshot_as_png(plot.state)
imgByteArr = BytesIO()
img.save(imgByteArr, format='PNG')
return imgByteArr.getvalue(), info
elif fmt == 'html':
html = self.figure_data(plot, doc=doc)
html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html
Expand Down
12 changes: 11 additions & 1 deletion tests/testrenderclass.py
Expand Up @@ -9,7 +9,7 @@
from nose.plugins.attrib import attr
import numpy as np

from holoviews import HoloMap, Image, ItemTable, Store, GridSpace, Table
from holoviews import HoloMap, Image, ItemTable, Store, GridSpace, Table, Curve
from holoviews.element.comparison import ComparisonTestCase
from holoviews.plotting import Renderer

Expand All @@ -23,6 +23,7 @@

try:
from holoviews.plotting.bokeh import BokehRenderer
from holoviews.plotting.bokeh.util import bokeh_version
except:
pass

Expand Down Expand Up @@ -146,3 +147,12 @@ def test_get_size_tables_in_layout(self):
plot = self.renderer.get_plot(table+table)
w, h = self.renderer.get_size(plot)
self.assertEqual((w, h), (680, 300))

def test_render_to_png(self):
if bokeh_version < str('0.12.6'):
raise SkipTest('Bokeh static png rendering requires bokeh>=0.12.6')
curve = Curve([])
renderer = BokehRenderer.instance(fig='png')
png, info = renderer(curve)
self.assertIsInstance(png, bytes)
self.assertEqual(info['file-ext'], 'png')