Jupyter bokeh animation with push_notebook

I am not able to get the various push_notebook examples to update when run as .ipynb files from VSCode. They work fine when run from a jupyter notebook. Below is a simple standard example that runs in a Jupyter notebook but not in VSCode. I need to do Bokeh animation from within a VSCode development environment. Isn’t this supposed to work as a cell in an .ipynb file executed in VSCode?

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.layouts import layout
from bokeh.io import push_notebook
import numpy as np
from time import sleep

# Output to Jupyter Notebook

# Prepare some data
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))

# Create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# Add a line renderer with legend and line thickness
p.line('x', 'y', source=source, legend_label="Temp.", line_width=2)

# Show the results
handle = show(p, notebook_handle=True)

# Update the plot
def update_plot(n_steps, sleep_time):
    for i in range(n_steps):
        y = np.sin(x + i/10.0)
        source.data = dict(x=x, y=y)

update_plot(100, 0.1)

Isn’t this supposed to work as a cell in an .ipynb file executed in VSCode?

The notebook in VSCode is not the standard Jupyter software. It has been modified by Microsoft to suit their own requirements, e.g around security or isolation or telemetry. Obviously we have zero control over these modifications, so in the strictest sense, we can’t make any guarantees whatsoever about executing in a VSCode environment.

In the past when there have been issues, on at least some occasions we were able to get the attention of interested Microsoft employees willing to push on things internally on their end to help find resolutions. But that was a few years ago at least, I don’t know if there are new problems that have cropped up since then.

You didn’t mention: have you installed the Jupyter Bokeh extension? Things like push_notebook definitely will not work in VSCode without the extension, since VScode does not allow JavaScript execution in output cells last I heard. If you have not installed it, then try that first. If you do have the the extension installed, then all I can suggest is filing an issue (on the extension repo linked above, not the main repo).

Otherwise my only other suggestion would be to try embedding a Bokeh server app inside the notebook instead of using push_notebook.

Thanks, Bryan. Yes I have the Jupyter Bokeh extension installed. I have been beating my brains out on this one and have exhausted all leads. I would have thought that Bokeh animation is a big deal feature, so are you saying that the right way to do this and what most others do is embed a Bokeh server?

It is, in general, but obviously we have zero control over the specific case where MS make changes to their custom version of Jupyter that might affect Bokeh. I hope you can appreciate that?

so are you saying that the right way to do this and what most others do is embed a Bokeh server?

I’m suggesting you should give a quick experiment a try. IIRC the issue that we got help with from MS awhile back was specifically for the embedded Bokeh server case, so that may be more relevant even today. In general the server is a more robust, supported, and featureful successor to push_notebook which was, more or less, a very early hack from circa 2013.

You might also take this question over to the Holoviz Discourse. They are much more heavily involved in Jupyter support for Bokeh due to their need to support Holoviz in Jupyter. They might have more ideas or information (I am not a heavy Jupyter user by any stretch).

I do completely. As you suggested, I posted as an issue #199 in the Jupyter Bokeh extension repo.

I’m just trying to stay in the VSCode environment to interactively develop a live document which has to be able to continuously refresh some cells in place. I’m concluding that I must be going about this the wrong way.

Thanks so much.

1 Like