Skip to content

Commit

Permalink
Correctly handle bokeh png rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 7, 2018
1 parent 64fe079 commit 4bde590
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
45 changes: 29 additions & 16 deletions holoviews/plotting/bokeh/renderer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from io import BytesIO
import base64
import logging

import param
Expand All @@ -14,7 +15,7 @@

from ...core import Store, HoloMap
from ..plot import Plot, GenericElementPlot
from ..renderer import Renderer, MIME_TYPES
from ..renderer import Renderer, MIME_TYPES, HTML_TAGS
from .widgets import BokehScrubberWidget, BokehSelectionWidget, BokehServerWidgets
from .util import attach_periodic, compute_plot_size, bokeh_version

Expand Down Expand Up @@ -114,11 +115,8 @@ def __call__(self, obj, fmt=None, doc=None):
elif isinstance(plot, tuple(self.widgets.values())):
return plot(), info
elif fmt == 'png':
from bokeh.io.export import get_screenshot_as_png
img = get_screenshot_as_png(plot.state, None)
imgByteArr = BytesIO()
img.save(imgByteArr, format='PNG')
return imgByteArr.getvalue(), info
png = self._figure_data(plot, fmt=fmt, doc=doc)
return png, info
elif fmt == 'html':
html = self._figure_data(plot, doc=doc)
html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html
Expand Down Expand Up @@ -268,19 +266,34 @@ def _figure_data(self, plot, fmt='html', doc=None, as_script=False, **kwargs):
# but at the holoviews level these are not issues
logger = logging.getLogger(bokeh.core.validation.check.__file__)
logger.disabled = True
try:
js, div, _ = notebook_content(model, comm_id)
html = NOTEBOOK_DIV.format(plot_script=js, plot_div=div)
html = encode_utf8(html)
doc.hold()
except:

if fmt == 'png':
from bokeh.io.export import get_screenshot_as_png
img = get_screenshot_as_png(plot.state, None)
imgByteArr = BytesIO()
img.save(imgByteArr, format='PNG')
data = imgByteArr.getvalue()
if as_script:
b64 = base64.b64encode(data).decode("utf-8")
(mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt]
src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64)
data = tag.format(src=src, mime_type=mime_type, css='')
js = ''
else:
try:
js, div, _ = notebook_content(model, comm_id)
html = NOTEBOOK_DIV.format(plot_script=js, plot_div=div)
data = encode_utf8(html)
doc.hold()
except:
logger.disabled = False
raise
logger.disabled = False
raise
logger.disabled = False

plot.document = doc
if as_script:
return div, js
return html
return data, js
return data


def diff(self, plot, binary=True):
Expand Down
2 changes: 1 addition & 1 deletion holoviews/plotting/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def components(self, obj, fmt=None, comm=True, **kwargs):
js, html = plot(as_script=True)
plot_id = plot.plot_id
else:
html, js = self._figure_data(plot, as_script=True, **kwargs)
html, js = self._figure_data(plot, fmt, as_script=True, **kwargs)
plot_id = plot.id
if comm and plot.comm is not None and self.comm_msg_handler:
msg_handler = self.comm_msg_handler.format(plot_id=plot_id)
Expand Down

0 comments on commit 4bde590

Please sign in to comment.