注意:此问题涉及已被弃用并已经几年没有使用的“第一代”Bokeh服务器。这个问题或其答案与任何版本的Bokeh >= 0.11都无关。
有关使用现代支持的Bokeh Server的详细信息,请参阅用户指南中的Running a Bokeh Server章节。
我正在构建一个交互式应用程序,希望了解Bokeh。我正在查看Bokeh示例,发现大多数示例都是在全局命名空间中编写的,但在“app”子目录中编写的示例采用了很好的面向对象风格,其中主要类继承自类似于HBox的Property类。
由于我认为这种编程方式并没有得到很好的文档记录,因此这将是一些混乱的问题。我遇到的第一件事是,除非我包含
extra_generated_classes
,否则图形不会绘制。
extra_generated_classes 是什么?
其次,看起来事件循环的
setup_events
在启动时在create
之前被调用,并且每次图绘制触发事件时都会被调用。为什么 setup_events 需要在每次事件触发时注册回调函数?为什么它不等待 create 完成后再尝试首次注册它们?
我不确定的最后一件事是如何强制重绘一个 Glyph。滑块演示对我有效,我正在尝试做基本相同的事情,只是用散点图代替了线条。
我在我的
update_data
的最后设置了一个 pdb 跟踪,我可以保证self.source
与self.plot.renderers[-1].data_source
匹配,并且两者从一开始就被微调过了。然而,self.plot
本身并没有改变。面向对象方法中的 store_objects 调用更新图表的等效方法是什么?
我对第三个问题特别困惑,因为它看起来不像滑块应用程序示例需要任何类似的东西。为了澄清,我正在尝试制作可变数量的小部件/滑块,所以我的代码看起来像这样:
class属性:
extra_generated_classes = [['ScatterBias', 'ScatterBias', 'HBox']]
maxval = 100.0
inputs = Instance(bkw.VBoxForm)
outputs = Instance(bkw.VBoxForm)
plots = Dict(String, Instance(Plot))
source = Instance(ColumnDataSource)
cols = Dict(String, String)
widgets = Dict(String, Instance(bkw.Slider))
# unmodified source
df0 = Instance(ColumnDataSource)
初始化方法
@classmethod
def create(cls):
obj = cls()
##############################
## load DataFrame
##############################
df = pd.read_csv('data/crime2013_tagged_clean.csv', index_col='full_name')
obj.cols = {'x': 'Robbery',
'y': 'Violent crime total',
'pop': 'Population'
}
cols = obj.cols
# only keep interested values
df2= df.ix[:, cols.values()]
# drop empty rows
df2.dropna(axis=0, inplace=True)
df0 = df2.copy()
df0.reset_index(inplace=True)
# keep copy of original data
obj.source = ColumnDataSource(df2)
obj.df0 = ColumnDataSource(df0)
##############################
## draw scatterplot
##############################
obj.plots = {
'robbery': scatter(x=cols['x'],
y=cols['y'],
source=obj.source,
x_axis_label=cols['x'],
y_axis_label=cols['y']),
'pop': scatter(x=cols['pop'],
y=cols['y'],
source=obj.source,
x_axis_label=cols['pop'],
y_axis_label=cols['y'],
title='%s by %s, Adjusted by by %s'%(cols['y'],
cols['pop'], cols['pop'])),
}
obj.update_data()
##############################
## draw inputs
##############################
# bokeh.plotting.scatter
## TODO: refactor so that any number of control variables are created
# automatically. This involves subsuming c['pop'] into c['ctrls'], which
# would be a dictionary mapping column names to their widget titles
pop_slider = obj.make_widget(bkw.Slider, dict(
start=-obj.maxval,
end=obj.maxval,
value=0,
step=1,
title='Population'),
cols['pop'])
##############################
## make layout
##############################
obj.inputs = bkw.VBoxForm(
children=[pop_slider]
)
obj.outputs = bkw.VBoxForm(
children=[obj.plots['robbery']]
)
obj.children.append(obj.inputs)
obj.children.append(obj.outputs)
return obj
更新数据
def update_data(self):
"""Update y by the amount designated by each slider"""
logging.debug('update_data')
c = self.cols
## TODO:: make this check for bad input; especially with text boxes
betas = {
varname: getattr(widget, 'value')/self.maxval
for varname, widget in self.widgets.iteritems()
}
df0 = pd.DataFrame(self.df0.data)
adj_y = []
for ix, row in df0.iterrows():
## perform calculations and generate new y's
adj_y.append(self.debias(row))
self.source.data[c['y']] = adj_y
assert len(adj_y) == len(self.source.data[c['x']])
logging.debug('self.source["y"] now contains debiased data')
import pdb; pdb.set_trace()
请注意,我确定事件处理程序已经设置并正确触发。我只是不知道如何使更改后的源数据在散点图中反映出来。