Bokeh - 使用编辑工具时获取更新的数据

Bokeh - get updated data when using Edit Tools

最近,多手势 edit tools 已添加到 Bokeh。例如,使用下面的脚本,我可以使用 PointDrawTool 在 jupyter notebook 中交互式地绘制点。我的问题是,如何获取我生成或编辑到 numpy 数组或类似数据结构中的点的更新数据?

from bokeh.plotting import figure, output_file, show, Column
from bokeh.models import DataTable, TableColumn, PointDrawTool, ColumnDataSource
from bokeh.io import output_notebook

# Direct output to notebook
output_notebook()

p = figure(x_range=(0, 10), y_range=(0, 10), tools=[],
           title='Point Draw Tool')
p.background_fill_color = 'lightgrey'

source = ColumnDataSource({
    'x': [1, 5, 9], 'y': [1, 5, 9], 'color': ['red', 'green', 'yellow']
})

renderer = p.scatter(x='x', y='y', source=source, color='color', size=10)
columns = [TableColumn(field="x", title="x"),
           TableColumn(field="y", title="y"),
           TableColumn(field='color', title='color')]
table = DataTable(source=source, columns=columns, editable=True, height=200)

draw_tool = PointDrawTool(renderers=[renderer], empty_value='black')
p.add_tools(draw_tool)
p.toolbar.active_tap = draw_tool

handle = show(Column(p, table), notebook_handle=True)

使用这种显示情节的方法不提供 Python 和 JS 之间的同步。要解决此问题,您可以使用 bookeh 服务器,如 here 所述。通常你使用 commnad:

bokeh serve --show myapp.py

然后你就可以把这个应用程序嵌入到你的jupyter中了。对我来说这很不方便所以我开始寻找其他解决方案。

可以从 jupyter 笔记本 运行 bookeh 应用,你可以找到示例 here

您的问题的示例代码如下所示:

from bokeh.plotting import figure, output_notebook, show, Column
from bokeh.models import DataTable, TableColumn, PointDrawTool, ColumnDataSource
output_notebook()

def modify_doc(doc): 
    p = figure(x_range=(0, 10), y_range=(0, 10), tools=[],
           title='Point Draw Tool')
    p.background_fill_color = 'lightgrey'
    source = ColumnDataSource({
        'x': [1, 5, 9], 'y': [1, 5, 9], 'color': ['red', 'green', 'yellow']
    })
    renderer = p.scatter(x='x', y='y', source=source, color='color', size=10)
    columns = [TableColumn(field="x", title="x"),
               TableColumn(field="y", title="y"),
               TableColumn(field='color', title='color')]
    table = DataTable(source=source, columns=columns, editable=True, height=200)

    draw_tool = PointDrawTool(renderers=[renderer], empty_value='black')
    p.add_tools(draw_tool)
    p.toolbar.active_tap = draw_tool

    doc.add_root(Column(p, table))

show(modify_doc)