1

我正在尝试在 jupyter 笔记本的一个单元格中使用 PointDrawTool 在散景图中绘制两个点,然后在另一个单元格中使用绘制点的坐标。

我从这个例子开始:

https://docs.bokeh.org/en/latest/docs/user_guide/examples/tools_point_draw.html

...并稍作修改以在笔记本内输出:

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


p = figure(
           title='scope')
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
#p.line(range(len(y)),y)

show(Column(p, table))

我在图中画了两个点。

在另一个笔记本单元格中:

len(source.data["x"]) 

它返回 3。换句话说,我可以访问初始化“源”的三个点,但不能访问之后绘制的任何点。

有任何想法吗?

4

1 回答 1

0

如果您想要完全双向同步和通信,则需要嵌入 Bokeh 服务器应用程序。像上面所做的那样使用普通的 Bokeh 对象进行调用show只会导致 Bokeh JS 内容的一次性输出,而不会与 Python 进行进一步的通信。嵌入 Bokeh Server 应用程序通常涉及定义一个函数,该函数modify_doc(doc)可以在调用时创建新的 bokeh 服务器会话show(modify_doc)。您可以在此处查看带有更多说明的完整示例笔记本:

https://github.com/bokeh/bokeh/blob/0.13.0/examples/howto/server_embed/notebook_embed.ipynb

对于您的代码,更新后的工作版本如下所示:

在此处输入图像描述

请务必注意:应用程序的任何 Bokeh 对象(例如数据源)只能在modify_doc. 您通常不能直接在modify_doc. 到应用程序“外部”的所有同步都必须是明确的。例如,如果需要,您可以在数据源上定义一个回调,以modify_doc更新另一个单元格中的一些可变数据结构。

于 2018-08-13T21:28:10.210 回答