python - 如何在散景中使用“点击”来影响不同情节(或表格)的变化?
问题描述
我正在尝试创建一个散景仪表板(仅使用python
,而不是JS
!),其中通过单击一个图中的字形,它会影响同一仪表板中表格的输出。出于某种原因,我可以单击并选择该点,但我看不到表格中的任何变化。(另外由于某种原因,它没有打印到控制台,所以我发现很难调试(?))我尝试了许多不同的事情,但没有运气。如果有人可以提供任何意见,我将不胜感激。守则如下。下面的代码以该名称存在,"main.py"
并且位于名为"select_exp"
. 散景服务器启动时使用:bokeh serve select_exp。再次提前感谢您的帮助!吉诺
`
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, TableColumn
# Create data for plot
x = [0, 1]
y = [0, 1]
table_index = [0, 1]
# Create the plot (this will be clicked on)
plot = figure(height = 400, width = 600,
title='Select a point', tools='tap')
plot_source = ColumnDataSource(data = dict(x=x, y=y))
renderer = plot.circle('x', 'y', source=plot_source, size=30)
# Create two sets of data for the tablet
master_data = {}
master_data[0] = {'animals': ['dog', 'cat', 'cow', 'mouse'],
'plants': ['carrot', 'catnip', 'grass', 'cheese']}
master_data[1] = {'animals': ['elephant', 'lion', 'monkey', 'emu'],
'plants': ['grass', 'turnips', 'banana', 'petunias']}
# Create a table
data = master_data[0]
table_source = ColumnDataSource(data)
columns = [ TableColumn(field='animals', title = 'Animal'),
TableColumn(field='plants', title = 'Plant') ]
data_table = DataTable(source=table_source, columns=columns,
width=400, height=600)
# Here the reactions of the server are defined
def my_tap_handler(attr, old, new):
index = source.selected.indices
print(index)
data_table.source = ColumnDataSource(master_data[index])
renderer.data_source.on_change("selected", my_tap_handler)
# Collect it all together iin the current doc
curdoc().add_root(column(plot, data_table))
curdoc().title = 'Select experiment'
`
解决方案
在 Bokeh v1.0.4 中,您需要将回调应用于selected
属性的data_source
和indices
属性。运行代码bokeh serve --show app.py
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, TableColumn
# Create data for plot
x = [0, 1]
y = [0, 1]
table_index = [0, 1]
# Create the plot (this will be clicked on)
plot = figure(height = 400, width = 600,
title = 'Select a point', tools = 'tap')
plot_source = ColumnDataSource(data = dict(x = x, y = y))
renderer = plot.circle('x', 'y', source = plot_source, size = 30)
# Create two sets of data for the tablet
master_data = {}
master_data[0] = {'animals': ['dog', 'cat', 'cow', 'mouse'],
'plants': ['carrot', 'catnip', 'grass', 'cheese']}
master_data[1] = {'animals': ['elephant', 'lion', 'monkey', 'emu'],
'plants': ['grass', 'turnips', 'banana', 'petunias']}
# Create a table
data = master_data[0]
table_source = ColumnDataSource(data)
columns = [ TableColumn(field = 'animals', title = 'Animal'),
TableColumn(field = 'plants', title = 'Plant') ]
data_table = DataTable(source = table_source, columns = columns,
width = 400, height = 600)
# Here the reactions of the server are defined
def my_tap_handler(attr, old, new):
index = new[0]
data_table.source = ColumnDataSource(master_data[index])
plot_source.selected.on_change("indices", my_tap_handler)
# Collect it all together in the current doc
curdoc().add_root(column(plot, data_table))
curdoc().title = 'Select experiment'
结果:
推荐阅读
- c++ - 这两个位运算符在做什么?
- c# - GDI+ DrawImage 在 C++ (Win32) 中比在 C# (WinForms) 中慢得多
- jquery - Jquery 不使用 next() 方法遍历
- rust - Rust 和可变性中的模式匹配
- macos - pip:当前(Python 3.6+)相当于`npm -g install some-global-package`?
- c# - EF Core 3.1 一对一关系,对象引用未设置为对象的实例
- javascript - 使用 Object.keys 从 JavaScript 中 JSON 文件中的值获取的多维数组
- python - 从非常嘈杂的二进制阈值图像中过滤噪声
- python - 如何使用 ebpf python 从打开的系统调用中打印文件路径?
- c++ - 当我使用十进制值时,为什么我的计算器会重复相同的输出?