Working in the Notebook¶
To display Bokeh plots inline in a classic Jupyter notebooks, use the
output_notebook() function from bokeh.io instead of (or in addition to)
output_file() function we have seen previously. No other modifications
are required. When
show() is called, the plot will be displayed inline in
the next notebook output cell. You can see a Jupyter screenshot below:
Multiple plots can be displayed in a single notebook output cell by calling
show() multiple times in the input cell. The plots will be displayed in order.
In order to embed Bokeh plots inside of JupyterLab, you need to install
the “jupyterlab_bokeh” JupyterLab extension. This can be done by running
jupyter labextension install jupyterlab_bokeh.
Depending on the version of the Notebook in use, it may be necessary to “trust” the notebook in order for Bokeh plots to re-render when the notebook is closed and subsequently re-opened. The “Trust Notebook” option is typically located under the “File” menu:
It is possible to use the Jupyter notebook in conjunction with Reveal.js to generate slideshows from notebook cell content. It is also possible to include standalone (i.e. non-server) Bokeh plots in such sideshows, however some steps must be followed for output to correctly display. Primarily:
The cell containing
output_notebook must not be skipped.
The rendered cell output of the
output_notebook call is responsible
for making sure the BokehJS library is loaded. Without that, Bokeh plots
cannot function. If this cell type is marked “skip” then BokehJS will
not be loaded, and Bokeh plots will not display. An alternative, if you
wish to hide this cell, is to mark it as the “notes” slide type.
It is possible to update a previously shown plot in-place. When the argument
notebook_handle=True is passed to
show() then a handle object is returned.
This handle object can be used with the
push_notebook() function to update
the plot with any recent changes to plots properties, data source values, etc.
This notebook handle functionality is only supported in classic Jupyter
notebooks and is not implemented in JupyterLab or Zeppelin yet.
The following screenshots walk through the basic usage of notebook handles.
- First, import standard functions, as well as
- Next, create some plots, and make sure to pass
- Looking at the handle, see that it is associated with the output cell
Inthat was just displayed:
- Now, update any properties of the plot, then call
push_notebook()with the handle:
- After doing so, note that the earlier output cell for
Inhas changed (without being re-executed)
More detailed demonstrations of using notebook handles can be found in the following example notebooks:
It is possible to drive updates to Bokeh plots using Jupyter notebook widgets,
known as interactors. The key doing this is the
described above. Typically it is called in the update callback for the
interactors, to update the plot from widget values. A screenshot of the
examples/howto/notebook_comms/Jupyter Interactors.ipynb example
notebook is shown below: