Skip to content

Commit

Permalink
Merge d3ae4aa into 806f562
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 16, 2018
2 parents 806f562 + d3ae4aa commit 83a9a31
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions holoviews/plotting/plotly/renderer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import absolute_import, division, unicode_literals

import base64
import json

import param
with param.logging_level('CRITICAL'):
from plotly.offline.offline import utils, get_plotlyjs, init_notebook_mode
import plotly.graph_objs as go

from ..renderer import Renderer, MIME_TYPES
from ..renderer import Renderer, MIME_TYPES, HTML_TAGS
from ...core.options import Store
from ...core import HoloMap
from .widgets import PlotlyScrubberWidget, PlotlySelectionWidget
Expand All @@ -32,11 +33,11 @@ class PlotlyRenderer(Renderer):

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

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

mode_formats = {'fig': {'default': ['html', 'json']},
mode_formats = {'fig': {'default': ['html', 'png', 'svg', 'json']},
'holomap': {'default': ['widgets', 'scrubber', 'auto']}}

widgets = {'scrubber': PlotlyScrubberWidget,
Expand All @@ -54,7 +55,7 @@ def __call__(self, obj, fmt='html', divuuid=None):

if isinstance(plot, tuple(self.widgets.values())):
return plot(), mime_types
elif fmt == 'html':
elif fmt in ('html', 'png', 'svg'):
return self._figure_data(plot, divuuid=divuuid), mime_types
elif fmt == 'json':
return self.diff(plot), mime_types
Expand All @@ -76,6 +77,19 @@ def _figure_data(self, plot, fmt=None, divuuid=None, comm=True, as_script=False,
# Wrapping plot.state in go.Figure here performs validation
# and applies any default theme.
figure = go.Figure(plot.state)

if fmt in ('png', 'svg'):
import plotly.io as pio
data = pio.to_image(figure, fmt)
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)
div = tag.format(src=src, mime_type=mime_type, css='')
js = ''
return div, js
return data

if divuuid is None:
divuuid = plot.id

Expand Down

0 comments on commit 83a9a31

Please sign in to comment.