最近,Bokeh已添加了多手势edit tools。例如,使用下面的脚本,我可以使用PointDrawTool以交互方式在jupyter笔记本中绘制点。我的问题是,如何获取我生成或编辑成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)
答案 0 :(得分:1)
使用这种显示情节的方法无法在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)