Skip to content

Instantly share code, notes, and snippets.

@philippjfr
Last active March 15, 2021 03:28
Show Gist options
  • Save philippjfr/cfc310b54c639f1f5b7accf6e5d29c59 to your computer and use it in GitHub Desktop.
Save philippjfr/cfc310b54c639f1f5b7accf6e5d29c59 to your computer and use it in GitHub Desktop.
Defines a BokehFigure element which allows wrapping bokeh Figures in a HoloMap - BSD-3 License
from holoviews import Element, Store
from bokeh.models import Column
from holoviews.plotting.bokeh import ElementPlot
class BokehFigure(Element):
"""Wraps bokeh figure"""
class BokehFigurePlot(ElementPlot):
def initialize_plot(self, ranges=None, plot=None, plots=None, source=None):
plot = Column(children=[self.hmap.last.data])
self.drawn = True
self.handles['plot'] = plot
return plot
def update_frame(self, key, ranges=None, element=None):
self.handles['plot'].children = [self._get_frame(key).data]
Store.register({BokehFigure: BokehFigurePlot}, 'bokeh')
# Testing code
import holoviews as hv
from bokeh.plotting import figure
def cb(i):
p = figure()
p.line([1, 2, 3], [1, 2, i])
return p
hmap = hv.HoloMap({i: BokehFigure(cb(i)) for i in range(3)})
hv.save(hmap, 'figure_holomap.html')
Copyright (c) 2019, Philipp Rudiger
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
* Neither the name of Philipp Rudiger nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@LinuxIsCool
Copy link

This is GOLD 🥇🥇🥇

@LinuxIsCool
Copy link

Actually, I get an error when running:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/core/dimension.py in _repr_mimebundle_(self, include, exclude)
   1314         combined and returned.
   1315         """
-> 1316         return Store.render(self)
   1317 
   1318 

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/core/options.py in render(cls, obj)
   1403         data, metadata = {}, {}
   1404         for hook in hooks:
-> 1405             ret = hook(obj)
   1406             if ret is None:
   1407                 continue

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
    280     if not ip.display_formatter.formatters['text/plain'].pprint:
    281         return None
--> 282     return display(obj, raw_output=True)
    283 
    284 

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw_output, **kwargs)
    256     elif isinstance(obj, (HoloMap, DynamicMap)):
    257         with option_state(obj):
--> 258             output = map_display(obj)
    259     elif isinstance(obj, Plot):
    260         output = render(obj)

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
    144         try:
    145             max_frames = OutputSettings.options['max_frames']
--> 146             mimebundle = fn(element, max_frames=max_frames)
    147             if mimebundle is None:
    148                 return {}, {}

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in map_display(vmap, max_frames)
    204         return None
    205 
--> 206     return render(vmap)
    207 
    208 

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
     66         renderer = renderer.instance(fig='png')
     67 
---> 68     return renderer.components(obj, **kwargs)
     69 
     70 

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/holoviews/plotting/renderer.py in components(self, obj, fmt, comm, **kwargs)
    408                 doc = Document()
    409                 with config.set(embed=embed):
--> 410                     model = plot.layout._render_model(doc, comm)
    411                 if embed:
    412                     return render_model(model, comm)

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/panel/viewable.py in _render_model(self, doc, comm)
    422         if comm is None:
    423             comm = state._comm_manager.get_server_comm()
--> 424         model = self.get_root(doc, comm)
    425 
    426         if config.embed:

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/panel/viewable.py in get_root(self, doc, comm, preprocess)
    482         root = self._get_model(doc, comm=comm)
    483         if preprocess:
--> 484             self._preprocess(root)
    485         ref = root.ref['id']
    486         state._views[ref] = (self, root, doc, comm)

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/panel/viewable.py in _preprocess(self, root)
    415         hooks = self._preprocessing_hooks+self._hooks
    416         for hook in hooks:
--> 417             hook(self, root)
    418 
    419     def _render_model(self, doc=None, comm=None):

~/.virtualenvs/boson-tokenomics/lib/python3.8/site-packages/panel/pane/holoviews.py in link_axes(root_view, root_model)
    611 
    612             fig = p.state
--> 613             if fig.x_range.tags:
    614                 range_map[fig.x_range.tags[0]].append((fig, p, fig.xaxis[0], fig.x_range))
    615             if fig.y_range.tags:

AttributeError: 'Column' object has no attribute 'x_range'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment