首页 > 解决方案 > 如何在散景中使用“点击”来影响不同情节(或表格)的变化?

问题描述

我正在尝试创建一个散景仪表板(仅使用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'

`

标签: pythonbokeh

解决方案


在 Bokeh v1.0.4 中,您需要将回调应用于selected属性的data_sourceindices属性。运行代码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'

结果:

在此处输入图像描述


推荐阅读