Unable to refresh plot in Bokeh JS with jupyter notebook

224 Views Asked by At

I'm trying to port this example from Bokeh to inside a notebook (cf here the source code)

Here's my attempt:

import pandas as pd

from bokeh.layouts import row, widgetbox
from bokeh.models import Select
from bokeh.palettes import Spectral5
from bokeh.plotting import curdoc, figure

df = pandas.DataFrame({'A': [1.1, 2.7, 5.3], 'B': [2, 10, 9], 'C': [3.3, 5.4, 1.5], 'D': [4, 7, 15]},
                      index = ['a1', 'a2', 'a3'])

columns = sorted(df.columns)

def create_figure(x_v):
    xs = df[x_v].values
    ys = df[y.value].values
    x_title = x_v.title()
    y_title = y.value.title()

    kw = dict()
    kw['title'] = "%s vs %s" % (x_title, y_title)

    p = figure(plot_height=600, plot_width=800, tools='pan,box_zoom,hover,reset', **kw)
    p.xaxis.axis_label = x_title
    p.yaxis.axis_label = y_title


    sz = 9
    c = "#31AADE"
    p.circle(x=xs, y=ys, color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5)

    return p

fig = create_figure('A')

def update(x_value):
    global fig
    global layout
    global bokeh_handle
    fig = create_figure(x_value)
    layout = row(controls, fig)
    push_notebook(handle=bokeh_handle) #doesn't update?

x = Select(title='X-Axis', value='A', options=columns)
x.js_on_change('value', CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
    var kernel = IPython.notebook.kernel;
    cmd = "update('" + cb_obj.value + "')";
    kernel.execute(cmd, {}, {});
}
"""))

controls = widgetbox([x], width=200)
layout = row(controls, fig)

bokeh_handle = show(layout, notebook_handle=True)

This display my plot of 'A vs B' but then the plot never updates when I change the select widget. How can I make it work?

What I see is that the layout object is actually changed, because if I call show(layout) in a new cell it will redraw the updated layout. However it seems that I'm not correctly updating the old layout with the new one, as the new once never changes. How can I fix this?

thanks!

0

There are 0 best solutions below