【问题标题】:Bokeh - get updated data when using Edit Tools散景 - 使用编辑工具时获取更新的数据
【发布时间】:2018-10-10 16:41:22
【问题描述】:

最近,多手势 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)

【问题讨论】:

    标签: plot bokeh interactive


    【解决方案1】:

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

    bokeh serve --show myapp.py
    

    然后您可以将此应用程序嵌入到您的 jupyter 中。对我来说这很不方便,所以我开始寻找其他解决方案。

    可以从 jupyter notebook 运行 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多