散景服务器-如何在回调函数中操作选择 [英] Bokeh server - How to manipulate a selection in a callback function

查看:75
本文介绍了散景服务器-如何在回调函数中操作选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制几个补丁,这些补丁按数据源中的组"类别分组.我要实现的目标如下:通过单击一个补丁,不仅补丁本身,而且同一组的所有补丁都应突出显示为选中状态.

I am plotting several patches that are grouped by a category "group" in the data source. What would I like to achieve is the following: By clicking on one patch, not only the patch itself but all patches of the same group should be highlighted as selected.

我发现ColumnDataSource具有一个已选择的属性.但是,在回调函数中操作此属性并没有达到预期的效果.

I found that ColumnDataSource has an attribute selected. However, manipulating this attribute in the callback function does not have the desired effect.

import os
from bokeh.models import ColumnDataSource, Patches
from bokeh.plotting import figure
from bokeh.layouts import row
from bokeh.io import output_file, curdoc
import pandas as pd

x = [[1,2,4], [3,5,6], [7,9,7], [5,7,6]]
y = [[4,2,1], [6,5,8], [3,9,6], [2,2,1]]
group = ['A', 'A', 'B', 'B']
id = [0,1,2,3]

df = pd.DataFrame(data=dict(x=x, y=y, group=group, id=id))
source = ColumnDataSource(df)

p = figure(tools="tap")

renderer = p.patches('x', 'y', source=source)

# Event handler
def my_tap_handler(attr,old,new):
    global source
    group_name = source.data['group'][new['1d']['indices'][0]]
    group_indices = df['id'][df['group'] == group_name]
    source.selected.indices = list(group_indices)
    print("source.selected.indices", source.selected.indices)


selected_patches = Patches(fill_color="#a6cee3")
renderer.selection_glyph = selected_patches

# Event
renderer.data_source.on_change("selected", my_tap_handler)

#######################################
# Set up layouts and add to document
curdoc().add_root(row(p, width=800))

推荐答案

您可以使用Javascript进行选择,但是如果您确实想使用Python进行选择,请参见以下示例:

You can do the selection in Javascript, but if you really want to do this in Python, here is an example:

import os
from bokeh.models import ColumnDataSource, Patches, CustomJS
from bokeh.plotting import figure
from bokeh.layouts import row
from bokeh.io import output_file, curdoc
import pandas as pd

def app(doc):
    x = [[1,2,4], [3,5,6], [7,9,7], [5,7,6]]
    y = [[4,2,1], [6,5,8], [3,9,6], [2,2,1]]
    group = ['A', 'A', 'B', 'B']
    id = [0,1,2,3]

    df = pd.DataFrame(data=dict(x=x, y=y, group=group, id=id))
    source = ColumnDataSource(df)

    p = figure(tools="tap")

    renderer = p.patches('x', 'y', source=source)

    def my_tap_handler(attr,old,new):
        indices = source.selected.indices
        if len(indices) == 1:
            group = source.data["group"][indices[0]]
            new_indices = [i for i, g in enumerate(source.data["group"]) if g == group]
            if new_indices != indices:
                source.selected = Selection(indices=new_indices)

    selected_patches = Patches(fill_color="#a6cee3")
    renderer.selection_glyph = selected_patches
    source.on_change("selected", my_tap_handler)

    doc.add_root(row(p, width=800))

show(app)

这篇关于散景服务器-如何在回调函数中操作选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆