Update image data in Bokeh plot in Notebook

I’m trying to rapidly update image data in a Bokeh figure in the notebook.

I make a figure and ColumnDataSouce like so

from bokeh.plotting import ColumnDataSource, figure, output_notebook, show


import numpy as np
img = np.random.random((1000, 1000))

fig = figure(title="foo")
source = ColumnDataSource({'value': [img]})
fig.image('value', source=source, x=0, y=0, dw=64, dh=64)

But then when I update the ColumnDataSource my output cell doesn’t change

source.data.update({"value": [np.ones((1000, 1000))]})

If I show the figure again then I get a new output cell with the appropriate updates, but I would ideally like to just keep pushing to the same output cell.

Any tips?

You would need to call push_notebook to explicitly synchronize the JS side with the Python changes.

Thanks Bryan!

OK, I have this working in the classic notebook with the following changes:


handle = show(fig, notebook_handle=True)


However it doesn’t seem to update in JupyterLab. Looking through these docs I make sure that I have the Bokeh JupyterLab extension installed

(dev) ngvpn01-171-128:~ mrocklin$ jupyter labextension install @bokeh/jupyter_bokeh
Building jupyterlab assets (build:dev:minimize)

But still things don’t seem to update well.

@mrocklin I just tested the example notebooks with Bokeh 1.4.0 and the latest extension and they are functioning as expected. Is your problem with only your code, or do you have issues with the linked notebooks as well?