我正在尝试将此示例从 Bokeh 移植到笔记本内部(参见这里的源代码)
这是我的尝试:
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)
一个新单元格,它将重绘更新的布局。然而,我似乎没有正确地用新的布局更新旧的布局,因为新的曾经永远不会改变。我怎样才能解决这个问题?
谢谢!