Get modifed data from PointDrawTool

Hi,

How to get modified data after a PointDrawTool usage ?

I can see new data from a CustomJS callback but cannot figure out how to get them in python for a later use.

Here is my code:

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

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

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

code = """
console.log(source.data);
"""
callback = CustomJS(args={'source': source}, code=code)

renderer = p.scatter(x='x', y='y', source=source, size=20)

draw_tool = PointDrawTool(renderers=[renderer], empty_value='black')
p.add_tools(draw_tool)
p.toolbar.active_tap = draw_tool
source.js_on_change('change:data', callback)

show(p)

Any help welcome.

@PBrockmann The PointDrawTool updates the ColumnDataSource for the configured glyph renderer, so the way to get the data would be to attach a Python callbacks to that data source, something like:

source.on_change("data", my_callback)

However, as is always the case, it is only possible to use real Python (i.e. on_change) callbacks in the context of a Bokeh server application. It is possible to embed Bokeh server applications in Jupyter notebooks as demonstrated here:

bokeh/notebook_embed.ipynb at 2.3.0 · bokeh/bokeh · GitHub

Otherwise, if you just call show(plot) you are generating standalone output that has no connection to any Python process.

1 Like