【发布时间】:2014-11-28 08:13:05
【问题描述】:
注意:这个问题涉及“第一代”散景服务器,该服务器已被弃用和删除了几年。此问题或其答案中的任何内容都与任何版本的 Bokeh >= 0.11
无关有关使用受支持的现代散景服务器的详细信息,请参阅用户指南的Running a Bokeh Server 章节。
我正在尝试了解Bokeh 我正在构建的交互式应用程序。我正在查看Bokeh examples,我看到大多数示例都是在全局命名空间中编写的,但是“app”子目录中的示例以很好的面向对象样式编写,其中主类继承自 HBox 等 Property 类。
这将是一堆问题,因为我认为这种 Bokeh 编程方式没有得到很好的记录。我遇到的第一件事是除非我包含extra_generated_classes,否则情节不会绘制。
-
extra_generated_classes 有什么作用?
其次,看起来事件循环
setup_events在启动时在create之前被调用,随后每次情节触发事件时。 -
为什么 setup_events 每次触发事件都需要注册回调?为什么在第一次尝试注册它们之前不等待创建完成?
我不确定的最后一件事是如何在此处强制重绘字形。滑块演示适用于我,我尝试做基本相同的事情,除了使用散点图而不是线。
我在
update_data的最后设置了一个pdb 跟踪,我可以保证self.source与self.plot.renderers[-1].data_source匹配,并且从一开始就对它们进行了调整。但是,self.plot本身并没有改变。 -
什么是面向对象的方法相当于调用 store_objects 来更新绘图?
我对第三个特别困惑,因为它看起来不像 sliders_app 示例需要这样的东西。为了澄清起见,我正在尝试制作可变数量的小部件/滑块,所以这就是我的代码的样子:
类属性:
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
update_data
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()
请注意,我确信事件处理程序已正确设置和触发。我只是不知道如何使更改后的源数据反映在散点图中。
【问题讨论】:
-
你找到解决这个问题的方法了吗,尤其是重新渲染散点图?我正在调查 Bokeh 是否有类似的东西。
-
如果你想得到答案,试着把你的问题分成更小的部分。
-
我投票决定将此问题作为离题结束,因为它涉及 Bokeh 包的一部分,该包多年前已被删除,仅在 1.0 之前的早期版本中,不再受支持或以任何方式可用。保留这个问题只会让 Bokeh 用户感到困惑。