1

有谁知道如何将变量从 bokehjs 回调函数传递给 python?我正在使用烧瓶框架开发一个网络应用程序,我试图从我的网络应用程序的散景图中选择几个点并获取 x,y 坐标。我希望下面的 select_points 函数返回这些坐标,并在该函数之外使用变量(在我的主代码上)。请帮我。

def select_points(data_x, svg_y):
    from bokeh.models import CustomJS, ColumnDataSource, TapTool
    from bokeh.plotting import figure
    from bokeh.embed import components

    source = ColumnDataSource(data=dict(x=data_x, y=svg_y))

    source2 = ColumnDataSource(data = dict(x = [], y = []))

    callback = source.selected.js_on_change('indices', CustomJS(args=dict(source=source, source2=source2), code="""
        var inds = cb_obj.indices;
        var d1 = source.data;
        var d2 = source2.data;
        d2['x'] = []
        d2['y'] = []
        for (var i = 0; i < inds.length; i++) {
            d2['x'].push(d1['x'][inds[i]])
            d2['y'].push(d1['y'][inds[i]])
        }
        source2.data = d2
    """)
    )

    taptool = TapTool(callback=callback)
    p = figure(tools=[taptool], title="Press shift + cursor click 10 times to select data points")
    p.circle('x', 'y', source=source, alpha=0.6)

    script, div = components(p)
    select_points.kwargs = {'script': script, 'div': div}
    select_points.kwargs['title'] = 'bokeh-with-flask'

    return source2.data['x'], source2.data['y']
4

0 回答 0