注意:此问题涉及"第一代"Bokeh服务器,该服务器已被弃用和删除多年.此问题或其答案中的任何内容均与任何版本的Bokeh> = 0.11无关
有关使用现代支持的Bokeh服务器的详细信息,请参阅"用户指南"中的" 运行散热服务器"一章.
我正在尝试了解Bokeh是否正在构建一个交互式应用程序.我正在查看Bokeh示例,我发现大多数示例都是在全局命名空间中编写的,但是"app"子目录中的那些示例是以一种漂亮的,面向对象的方式编写的,其中主类是固有的来自像HBox这样的Property类.
这将是一个混乱的问题,因为我不认为这种编程Bokeh的方式有很好的记录.我遇到的第一件事是除非我包括,否则情节不会画出来extra_generated_classes.
extra_generated_classes做什么?
其次,看起来事件循环setup_events在启动之前调用,create并且随后每次触发事件时调用.
为什么每次触发事件时setup_events都需要注册回调?为什么它不会在第一次尝试注册之前等待创建完成?
我不确定的最后一件事是如何在这里强制重绘一个雕文.滑块演示适用于我,我试图基本上做同样的事情,除了散点图而不是线.
我在我的最后设置了一个pdb跟踪update_data,我可以保证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)
Run Code Online (Sandbox Code Playgroud)
初始化方法
@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
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
请注意,我确信事件处理程序已正确设置并触发.我只是不知道如何使更改的源数据反映在散点图中.
我正在寻找相同的答案(缺乏文档使其变得困难)。
在回答问题#1时,“extra_ generated_classes”的用途是什么:
tl;dr extra_ generated_classes 定义了模板生成 js/html 代码时使用的模块名、类名和父类名,并扩展了传递到应用程序类中的父类(在示例中通常是 HBox 或 VBox)。
更长的答案。查看 bokeh/server/utils/plugins.py 中的源代码,这是使用 --script 命令行参数传递到 bokeh-server 的代码上运行的代码。在plugins.py的末尾,您可以看到extra_ generated_classes被传递给flask方法render_template,该方法渲染一个Jinja2模板。查看模板 oneobj.html 内部,extra_ generated_classes 是一个由三个元素组成的数组:模块名、类名和父名,它们被传递到 bokeh.server.generatejs 中:
{% block extra_scripts %}
{% for modulename, classname, parentname in extra_generated_classes %}
<script
src="{{ url_for('bokeh.server.generatejs', modulename=modulename, classname=classname, parentname=parentname) }}"
></script>
{% endfor %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
bokeh.server.generatejs 是 bokeh/server/views/plugins.py 中的 Python 代码,仅调用模板 app.js 的 render_template,您可以在 bokeh/server/templates 中找到该模板。该模板采用模块名、类名和父名,并基本上创建将父名(例如 HBox 或 VBox)扩展到类名(您的应用程序)的 js 代码。