我是Bokeh的新手,我正在尝试构建一个可根据小部件提供的输入动态更新的图。但是,对于大多数小部件,Python回调的用法尚未完全记录下来,因此我陷入了困境。
on_event
或on_change
),我仍然必须弄清楚其签名和参数。例如,如果我使用on_change
,我可以监视哪些窗口小部件属性?这是一个合适的例子。我正在使用此示例中的笔记本嵌入式服务器。作为练习,我想将滑块替换为DataTable
任意值。这是我目前拥有的代码:
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, DataTable
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
output_notebook()
def modify_doc(doc):
df = sea_surface_temperature.copy()
source = ColumnDataSource(data=df)
source_table = ColumnDataSource(data={"alpha": [s for s in "abcdefgh"],
"num": list(range(8))})
plot = figure(x_axis_type='datetime', y_range=(0, 25),
y_axis_label='Temperature (Celsius)',
title="Sea Surface Temperature at 43.18, -70.43")
plot.line('time', 'temperature', source=source)
def callback(attr, old, new):
# This is the old callback from the example. What is "new" when I use
# a table widget?
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new)).mean()
source.data = ColumnDataSource(data=data).data
table = DataTable(source=source_table,
columns=[TableColumn(field="alpha", title="Alpha"),
TableColumn(field="num", title="Num")])
# How can I attach a callback to table so that the plot gets updated
# with the "num" value when I select a row?
# table.on_change("some_attribute", callback)
doc.add_root(column(table, plot))
show(modify_doc)
该答案是针对Bokeh v1.0.4给出的,可能与最新文档不兼容
JavaScript回调和Python回调是Bokeh中非常强大的工具,可以附加到任何Bokeh模型元素。另外,您可以通过使用TypeScript编写自己的扩展来扩展Bokeh功能(最终编译为JS)
可以使用以下两种方法之一添加JS回调:
Model.js_on_event('event', callback)
Model.js_on_change('attr', callback)
Python回调主要用于小部件:
Widget.on_event('event, onevent_handler)
Widget.on_change('attr', onchange_handler)
Widget.on_click(onclick_handler)
每个小部件的事件处理程序的确切函数签名可以是:
onevent_handler(event)
onchange_handler(attr, old, new)
onclick_handler(new)
onclick_handler()
的attr
可以是任何部件类(或它的基类)属性。因此,您始终需要查阅Bokeh参考页。还扩大了JSON原型有助于找出哪些属性的支持如寻找事业部,我们不能直接看到id
,name
,style
或text
属性,这来源于其基类。但是,所有这些属性都存在于Div的JSON原型中,因此受Div支持:
{
"css_classes": [],
"disabled": false,
"height": null,
"id": "32025",
"js_event_callbacks": {},
"js_property_callbacks": {},
"name": null,
"render_as_text": false,
"sizing_mode": "fixed",
"style": {},
"subscribed_events": [],
"tags": [],
"text": "",
"width": null
}
回到您的问题:很多时候,您可以使用不同的方法获得相同的结果。
据我所知,没有一种很好的方法可以列出每个小部件的所有受支持的事件,但是阅读文档并深入研究基类会大有帮助。
使用上述方法,可以检查可以在回调中使用的窗口小部件属性。当涉及到事件时,我建议您bokeh.events
在IDE中查看和探索该类。您可以在其中找到每个事件的扩展说明。随着程序员的直觉选择小部件支持的正确事件,随着时间的流逝自然会自然而然地发生(因此,没有理由button_click
,Plot
也没有pan
事件,Button
只有相反的方式)。
决定将回调附加到哪个小部件(模型元素)以及要选择哪种方法或将回调绑定到哪个事件,这主要取决于您:哪个用户操作应触发您的回调?
因此,您可以将JS回调附加到任何小部件(值更改,滑块移动等),任何工具(TapTool,HoverTool等),data_source(单击字形),绘制画布(例如单击)在字形以外的区域)或绘图范围(缩放或平移事件)等。
基本上,您需要了解所有Python对象在BokehJS中都有它们的等效项,因此您可以在两个域中以相同的方式使用它们(当然,在语法上有所不同)。
例如,该文档显示ColumnDataSource具有“ selected”属性,因此对于点,您可以检查source.selected.indices
并查看选择了绘图上的哪个点或在您的情况下喜欢:选择了哪些表行。您可以在Python中以及在浏览器中的代码中设置断点,并检查Python或BokehJS数据结构。在运行代码时,将环境变量BOKEH_MINIFIED
设置no
为在IDE(运行配置)或终端(例如BOKEH_MINIFIED=no python3 main.py
)中设置。这将使在浏览器中调试BokehJS更加容易。
这是您的代码(由于未安装Jupiter Notebook,因此对“ pure Bokeh” v1.0.4做了一些修改)
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, DataTable, TableColumn
from bokeh.plotting import figure, curdoc
from bokeh.io import show, output_notebook
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
# output_notebook()
def modify_doc(doc):
df = sea_surface_temperature.copy()
source = ColumnDataSource(data = df)
source_table = ColumnDataSource(data = {"alpha": [s for s in "abcdefgh"],
"num": list(range(8))})
plot = figure(x_axis_type = 'datetime', y_range = (0, 25),
y_axis_label = 'Temperature (Celsius)',
title = "Sea Surface Temperature at 43.18, -70.43")
plot.line('time', 'temperature', source = source)
def callback(attr, old, new): # here new is an array containing selected rows
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new[0])).mean() # asuming one row is selected
source.data = ColumnDataSource(data = data).data
table = DataTable(source = source_table,
columns = [TableColumn(field = "alpha", title = "Alpha"),
TableColumn(field = "num", title = "Num")])
source_table.selected.on_change('indices', callback)
doc().add_root(column(table, plot))
modify_doc(curdoc)
# show(modify_doc)
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句