-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update save and show utilities (#451)
* Update save and show utilities * Fixed flakes
- Loading branch information
1 parent
e95be9d
commit 8dc94cb
Showing
1 changed file
with
11 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,26 @@ | ||
import panel as _pn | ||
import holoviews as _hv | ||
|
||
from bokeh.io import export_png as _export_png, show as _show, save as _save | ||
from bokeh.resources import CDN as _CDN | ||
|
||
renderer = _hv.renderer('bokeh') | ||
|
||
save = _hv.save | ||
|
||
def save(obj, filename, title=None, resources=None): | ||
""" | ||
Saves HoloViews objects and bokeh plots to file. | ||
Parameters | ||
---------- | ||
obj : HoloViews object | ||
HoloViews object to export | ||
filename : string | ||
Filename to save the plot to | ||
title : string | ||
Optional title for the plot | ||
resources: bokeh resources | ||
One of the valid bokeh.resources (e.g. CDN or INLINE) | ||
""" | ||
if isinstance(obj, _hv.core.Dimensioned): | ||
plot = renderer.get_plot(obj).state | ||
else: | ||
raise ValueError('%s type object not recognized and cannot be saved.' % | ||
type(obj).__name__) | ||
|
||
if filename.endswith('png'): | ||
_export_png(plot, filename=filename) | ||
return | ||
if not filename.endswith('.html'): | ||
filename = filename + '.html' | ||
|
||
if title is None: | ||
title = 'hvPlot Plot' | ||
if resources is None: | ||
resources = _CDN | ||
|
||
if obj.traverse(lambda x: x, [_hv.HoloMap]): | ||
renderer.save(plot, filename) | ||
else: | ||
_save(plot, filename, title=title, resources=resources) | ||
|
||
|
||
def show(obj, filename=None): | ||
def show(obj, title=None, port=0, **kwargs): | ||
""" | ||
Displays HoloViews objects in and outside the notebook | ||
Parameters | ||
---------- | ||
obj : HoloViews object | ||
HoloViews object to export | ||
HoloViews object to export | ||
title : str | ||
A string title to give the Document (if served as an app) | ||
port: int (optional, default=0) | ||
Allows specifying a specific port | ||
**kwargs: dict | ||
Additional keyword arguments passed to Panel show method. | ||
""" | ||
if not isinstance(obj, _hv.core.Dimensioned): | ||
raise ValueError('%s type object not recognized and cannot be shown.' % | ||
type(obj).__name__) | ||
|
||
if obj.traverse(lambda x: x, [_hv.HoloMap]): | ||
renderer.app(obj, show=True, new_window=True) | ||
else: | ||
from bokeh.io import output_file | ||
output_file(filename or 'default.html') | ||
_show(renderer.get_plot(obj).state) | ||
_pn.pane.HoloViews(obj).show(title, port, **kwargs) |