Bokeh Server callback from tools

前端 未结 3 1221
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 19:53

I\'m kinda new on Python, and currently working on a interactive plot visualization using Bokeh where I need to show multiple related charts. To accomplish this i\'m using b

3条回答
  •  醉酒成梦
    2021-01-02 20:19

    Since bokeh 0.13 the solution proposed by @Karel_Marik a needs small tweak:

    from bokeh.plotting import figure, curdoc
    from bokeh.layouts import column
    from bokeh.models import ColumnDataSource
    
    TOOLS = ["tap"]
    p = figure(title="Some Figure", tools=TOOLS)
    
    source = ColumnDataSource(dict(
        x=[[1, 3, 2], [3, 4, 6, 6]],
        y=[[2, 1, 4], [4, 7, 8, 5]],
        name=['A', 'B'],color=["firebrick", "navy"],
        alpha=[0.8,0.3],line_width=[3,3]))
    
    pglyph = p.patches('x', 'y', color="color", alpha="alpha",
                       line_width="line_width", source=source)
    
    def callback(attr, old, new):
        # The index of the selected glyph is : new['1d']['indices'][0]
        selections = new['1d']['indices']
        print("Number of selections:{}".format(len(selections)))
        for index in selections:
            patch_name =  source.data['name'][index]
            print("TapTool callback executed on Patch {}".format(patch_name))
    

    pglyph.data_source.on_change('selected',callback)

    pglyph.selected.on_change('indices', callback)
    
    curdoc().add_root(column(p))
    

    I hope this helps somebody.

提交回复
热议问题