Skip to content

Commit

Permalink
Added support for post_render_hooks to Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Feb 4, 2016
1 parent ed8d553 commit b4113e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions holoviews/plotting/bokeh/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ def __call__(self, obj, fmt=None):
elif fmt == 'html':
html = self.figure_data(plot)
html = '<center>%s</center>' % html
return html, info
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:
data['root'] = plot.state._id
data['data'] = models_to_json(plotobjects)
return serialize_json(data), info
return self._apply_post_render_hooks(serialize_json(data), obj, fmt), info


def figure_data(self, plot, fmt='html', **kwargs):
Expand Down
1 change: 1 addition & 0 deletions holoviews/plotting/mpl/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __call__(self, obj, fmt='auto'):
anim = plot.anim(fps=self.fps)
data = self._anim_data(anim, fmt)

data = self._apply_post_render_hooks(data, obj, fmt)
return data, {'file-ext':fmt,
'mime_type':MIME_TYPES[fmt]}

Expand Down
29 changes: 27 additions & 2 deletions holoviews/plotting/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ class Renderer(Exporter):
key_fn = param.Callable(None, allow_None=True, constant=True, doc="""
Renderers do not support the saving of object key metadata""")

post_render_hooks = param.Dict(default={'svg':[], 'png':[]}, doc="""
Optional dictionary of hooks that are applied to the rendered
data (according to the output format) before it is returned.
Each hook is passed the rendered data and the object that is
being rendered. These hooks allow post-processing of renderered
data before output is saved to file or displayed.""")

# Defines the valid output formats for each mode.
mode_formats = {'fig': {'default': [None, 'auto']},
'holomap': {'default': [None, 'auto']}}
Expand Down Expand Up @@ -204,10 +212,27 @@ def __call__(self, obj, fmt=None):
"""
plot, fmt = self._validate(obj, fmt)
if plot is None: return
# [Backend specific code goes here]
# [Backend specific code goes here to generate data]
data = None

# Example of how post_render_hooks are applied
data = self._apply_post_render_hooks(data, obj, fmt)
# Example of the return format where the first value is the rendered data.
return None, {'file-ext':fmt, 'mime_type':MIME_TYPES[fmt]}
return data, {'file-ext':fmt, 'mime_type':MIME_TYPES[fmt]}


def _apply_post_render_hooks(self, data, obj, fmt):
"""
Apply the post-render hooks to the data.
"""
hooks = self.post_render_hooks.get(fmt)
for hook in hooks:
try:
data = hook(data, obj, fmt, self.backend)
except Exception as e:
self.warning("The post_render_hook %r could not be applied:\n\n %s"
% (hook, e))
return data


def html(self, obj, fmt=None, css=None):
Expand Down

0 comments on commit b4113e9

Please sign in to comment.