0

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!

lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • You could try updating the `ColumnDataSource` instead of replotting the figure. See https://hub.mybinder.org/user/bokeh-bokeh-notebooks-y42307kg/notebooks/tutorial/06%20-%20Linking%20and%20Interactions.ipynb for examples. – Pallie Nov 08 '18 at 10:25
  • yes but honestly I'm very uncomfortable with that, I'd rather have a python callback that allows me to do whatever I want on my figure – lezebulon Nov 08 '18 at 16:26

0 Answers0