Bokeh in Jupyter Notebook

Hi, i’m new here. I made a course about interactive data visualization on Datacamp. I want it to reproduce the exercises on jupyter notebook. The problem is, the graph doesn’t change when i change the Slider and got the next error:

WARNING:bokeh.embed.util:
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html

My code is:

from bokeh.io import output_notebook, push_notebook, show
output_notebook()

    def update_plot(attr, old, new):
        yr = slider.value
        new_data = {
            'x'       : data.loc[yr].fertility,
            'y'       : data.loc[yr].life,
            'country' : data.loc[yr].Country,
            'pop'     : (data.loc[yr].population / 20000000) + 2,
            'region'  : data.loc[yr].region,
        }
        source.data = new_data
        plot.title.text = 'Gapminder data for %d' % yr

    slider = Slider(start= 1964, end= 2013, step= 1, value= 1964, title= 'Year')
    slider.on_change('value', update_plot)

    layout = row(Column(slider), plot )
    curdoc().add_root(layout)

    output_file('gapminder4.html')
    show(layout, notebook_handle= True)

Maybe I is something to do with the server but I look for information in the bokeh website and i’m really confused

Hi @kihon,

It looks like your code is a mixture of both standalone Bokeh (using a show function; standalone Bokeh needs a Javascript callback) and server (with curdoc().add_root; server Bokeh uses python callbacks).

If I understand your use case, it sounds like you may just need standalone Bokeh. Try the documentation here: Using with Jupyter — Bokeh 2.3.1 Documentation

If you do find that you need Bokeh server, then there is an example of an embedded server here: bokeh/notebook_embed.ipynb at branch-2.4 · bokeh/bokeh · GitHub

Let us know if you have further questions after review.

Yes, i already do that. I imported the output_notebook() from bokeh.io. My graph displays but it doesn’t change when I move the Slider

@kihon In order to use real, actual Python callbacks, i.e. with on_change and not js_on_change, that requires embedding a Bokeh server application in the notebook. That is what this error message is telling you:

WARNING:bokeh.embed.util:
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

And also what @carolyn explained. It’s not simply a matter of only calling output_notebook, there is specific pattern of use required to embedding a Bokeh app in a notebook. @carolyn already linked you to a specific example notebook that you can emulate, but I will repeat it here:

bokeh/notebook_embed.ipynb at branch-2.4 · bokeh/bokeh · GitHub

If you want to use real Python callbacks you must follow the pattern in that notebook (which you are not doing above) i.e. create a function that defines how to manufacture app sessions, and pass that function to show.

1 Like

Bryan, firstly it’s an honor. I really like your course on Datacamp.
Okay I understand, I’ll look the example with more detail. Thanks Carolyn and Bryan :grin: