我正在尝试将this示例从Bokeh移植到笔记本内部(请参见here源代码)
这是我的尝试:
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)
这将显示我的“ A vs B”图,但是当我更改选择小部件时,该图永远不会更新。 我该如何运作?
我看到的是布局对象实际上已更改,因为如果我在新单元格中调用show(layout)
,它将重新绘制更新的布局。
但是,似乎我没有用新的布局正确地更新旧的布局,因为新的布局永远都不会改变。
我该如何解决?
谢谢!