【问题标题】:How to update the selected source indices programmatically?如何以编程方式更新选定的源索引?
【发布时间】:2018-11-05 18:04:03
【问题描述】:

从文档中,Selection 对象说:

选择通常是通过使用 SelectTool 在绘图中选择点来创建的,但也可以通过编程方式指定。

但我不知道如何以编程方式设置一些选定的点。例如,如果我想通过单击某个按钮来更新选择。我可以更新元素source.selected.indices,但是没有触发事件,点也没有标记为选中

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import column
from bokeh.models.tools import LassoSelectTool, TapTool
from bokeh.models.widgets.buttons import Button

source = ColumnDataSource(dict(
    x=[1, 2, 3, 4, 5, 6],
    y=[1, 2, 3, 4, 5, 6],
))

p = figure(
    plot_height=300,
    tools='',
)
p.circle( x='x', y='y', size=20, source=source)

lasso_select = LassoSelectTool(
    select_every_mousemove=False,
)
tap = TapTool()
tools = (lasso_select, tap)
p.add_tools(*tools)

def update_selection_programmatically():
    source.selected.update(indices=[4])           # the indices attribute is updated but the figure is not repainted, some event is not triggered.
                                                  # So the points are not marked as selected
bt = Button(
    label="Update Selection",
    button_type="success",
    width=50
)

bt.on_click(update_selection_programmatically)


def update_selection(attr, old, new):
    print('>> NEW SELECTION: {}'.format(new.indices))
    # new.indices = [0]       # this works fine here

source.on_change('selected', update_selection)

curdoc().add_root(column([p, bt]))

索引属性已更新,但图形未重新绘制。

【问题讨论】:

  • 看起来像一个错误,我建议一个 GH 问题

标签: python python-3.x plot selection bokeh


【解决方案1】:

这适用于 CustomJS 回调

bt.callback = CustomJS(args={'source':source},code="""source.selected.indices = [4];source.change.emit();""")

我认为 Python 回调应该自动执行 source.change.emit(),所以不确定为什么你的回调不起作用。 但是你可以在 JS 回调中显式地做到这一点。

我不知道为什么,但它不会触发 update_selection

【讨论】:

  • 感谢您的建议。无论如何,我已经写了一个GH issue。让我们看看会发生什么
【解决方案2】:

答案更新

最后,散景开发者解决了这个问题。现在可以更新索引了

source.selected.update(indices=[4])

# or equivalently

source.selected.indices = [4]

旧的解决方法

johncthomas wrote a comment in the GitHub issue。有一个解决方法:

我想出了一个解决办法

# When this does not update the visible selections 
source.selected.indices = new_indicies

# This causes the selections to be visible
source.data = source.data

特别是在 13.0 版本中运行服务器时。显然不理想,但它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 2018-05-30
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多