New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push_notebook() does not work with ipywidgets Output Widget #7023

Closed
mhamilton723 opened this Issue Oct 9, 2017 · 8 comments

Comments

Projects
None yet
4 participants
@mhamilton723
Copy link

mhamilton723 commented Oct 9, 2017

Runtime Info:

notebook server is 5.0.0
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:09:58)
IPython 6.0.0
bokeh '0.12.9'
ipywidgets '7.0.1'
ubuntu 16

I am trying to embed bokeh plots within the ipywidgets ecosystem. However, I cannot update these plots externally when they are wrapped in an ipywidget Output Widget
. Here is a simple repro

This displays the blank graph:

import bokeh
from bokeh.io import output_notebook, show, push_notebook
output_notebook(bokeh.resources.INLINE)
from bokeh.models import ColumnDataSource, Circle
import ipywidgets as widgets
from IPython.display import display


plot = figure()
source = ColumnDataSource(
    data=dict(
        x=[],
        y=[],
    )
)

circle = Circle(x="x", y="y", size=15, fill_color="blue", fill_alpha=0.8, line_color=None)
plot.add_glyph(source, circle)

outw = widgets.Output()
with outw:
    h = show(plot, notebook_handle=True)
display(outw)

This cell should update that graph:

source.data = {'x':[0], 'y':[0]}
push_notebook(handle=h);

However the first graph does not change. If I remove the wrapping in the output widget it does update. If i use an ipywidget (instead of a bokeh plot) inside of the output widget it updates as expected.

@bryevdv

This comment has been minimized.

Copy link
Member

bryevdv commented Oct 9, 2017

As requested, please take support questions to the mailing list or stack overflow. For reference, here is an example combining ipywidgets and push_notebook:

https://github.com/bokeh/bokeh/blob/master/examples/howto/notebook_comms/Jupyter%20Interactors.ipynb

@bryevdv

This comment has been minimized.

Copy link
Member

bryevdv commented Oct 9, 2017

Actually I guess the widgets.Output() makes this different. I have no idea what that is or what it does. It's unlikely that any of the (few) core devs would be able to look at this any time soon. I would recommend embedding a real Bokeh app in the notebook, e.g.

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

@mhamilton723

This comment has been minimized.

Copy link
Author

mhamilton723 commented Oct 9, 2017

Hey, Thanks for taking a look you are definitely right that the output widget changes things, the demo you liked to works for me, but the output wdget is needed to compose bokeh and ipywidgets.

http://ipywidgets.readthedocs.io/en/stable/examples/Widget%20List.html#Output

@bryevdv bryevdv changed the title push_notebook() does not work with ipywidgets push_notebook() does not work with ipywidgets Output Widget Oct 9, 2017

@dajuro

This comment has been minimized.

Copy link

dajuro commented Nov 6, 2017

Hi,

I just ran into the same problem and I found a workaround: you need to render the widgets before plotting into it! This code works for me:

import bokeh
from bokeh.io import output_notebook, show, push_notebook
output_notebook(bokeh.resources.INLINE)
from bokeh.models import ColumnDataSource, Circle
import ipywidgets as widgets
from IPython.display import display

plot = figure()
source = ColumnDataSource(
    data=dict(
        x=[1],
        y=[1],
    )
)

circle = Circle(x="x", y="y", size=15, fill_color="blue", fill_alpha=0.8, line_color=None)
plot.add_glyph(source, circle)

outw = widgets.Output()

# display widget!!!!
display(outw)

# plot into widget
with outw:
    h = show(plot, notebook_handle=True)

Now you can update the plot:

source.data = {'x':[5], 'y':[5]}
push_notebook(handle=h);

Edit and FYI: this is the same for matplotlib plots as well

@bryevdv

This comment has been minimized.

Copy link
Member

bryevdv commented Mar 17, 2018

If its the same for MPL then this is probably an upstream requirement we can't do anything about.

@bryevdv bryevdv closed this Mar 17, 2018

@ozel

This comment has been minimized.

Copy link

ozel commented Sep 3, 2018

the requirement to display the output widget early makes combinations of push-able bokeh plots and other ipywidgets impossible: e.g. adding the following line after @dajuro's example doesn't work as expected
display(widgets.HBox([outw,widgets.IntSlider()]))
is there an alternative way to instantiate the bokeh plot and generate the handle h without rendering the plot immediately inside the output widget?

@bryevdv

This comment has been minimized.

Copy link
Member

bryevdv commented Sep 3, 2018

No, there isn't, and I don't see how there could be. Setting up the handle is something that has to happen on the JS side, and it needs access to the thing it will be updating in the future at the time.

I'll reiterate my earlier suggestion to embed real Bokeh app with Bokeh widgets, instead of ipywidgets, if you need this kind of control over layout. It's very simple these days:

https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

@ozel

This comment has been minimized.

Copy link

ozel commented Sep 4, 2018

Thank you a lot @bryevdv for the quick explanation and especially for pointing out the embedded bokeh app solution again! I knew about it but actually wondered what its advantage would be. Having server-side python callbacks is the only reason I was looking at ipywidgets in the first place. Sometimes all the pyviz possibilities can be overwhelming :) I’m building a data acquisition interface with live views and storage&browsing of recorded chunks that needs some analysis flexibility via notebooks. Keeping the DAQ/GUI part within one bokeh app makes a lot of sense

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