Bokeh server - How to manipulate a selection in a callback function

◇◆丶佛笑我妖孽 提交于 2019-12-01 08:28:44

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!