Skip to content
This repository has been archived by the owner on Nov 29, 2019. It is now read-only.

Add support for custom widget layouts #66

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions parambokeh/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import sys
import inspect
import base64
from io import BytesIO

import bokeh
from bokeh.models import Model, CustomJS, LayoutDOM
from bokeh.models import Model, CustomJS, LayoutDOM, Div

try:
from IPython.display import publish_display_data
Expand Down Expand Up @@ -102,7 +104,7 @@ def patch_widgets(plot, doc, plot_id, comm):

def process_plot(plot, doc, plot_id, comm):
"""
Converts all acceptable plot and widget objects into displaybel
Converts all acceptable plot and widget objects into displayable
bokeh models. Patches any HoloViews plots or parambokeh Widgets
with the top-level comms and plot id.
"""
Expand All @@ -118,7 +120,15 @@ def process_plot(plot, doc, plot_id, comm):
from holoviews import renderer
renderer = renderer('bokeh').instance(mode='server' if comm is None else 'default')
plot = renderer.get_plot(plot, doc=doc)

elif plot.__class__.__name__ == 'Figure' and hasattr(plot, '_cachedRenderer'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this elif should have a comment above it saying "Matplotlib figure", as that's not at all obvious from what's being tested.

bytes_io = BytesIO()
plot.canvas.print_figure(bytes_io)
data = bytes_io.getvalue()
b64 = base64.b64encode(data).decode("utf-8")
src = "data:image/png;base64,{b64}".format(b64=b64)
html = "<img src='{src}'></img>".format(src=src)
width, height = plot.canvas.get_width_height()
return Div(text=html, width=width, height=height)
if not hasattr(plot, '_update_callbacks'):
raise ValueError('Can only render bokeh models or HoloViews objects.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this error message is out of date. Maybe "Not one of the currently supported displayable objects (Bokeh models, HoloViews objects, or Matplotlib figures)."?

Would it be possible to have a fallback that allows any object with a _repr_html_ method? That would cover Pandas .head() and possibly streamz objects, and would let users make their own wrappers around other things...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to have a fallback that allows any object with a repr_html method?

Sure.

and possibly streamz objects

Definitely not, those work using ipywidgets.


Expand Down