Bokeh Plots and Kyso

Use this snippet to make bokeh plots work seamlessly in Jupyter and Kyso.

To make bokeh plots work inside Jupyter and render on Kyso, we need to save the plot to a virtual html file and then show it in the notebook:

from bokeh.plotting import figure, output_file, save
from IPython.display import IFrame
from IPython.core.display import display, HTML
import tempfile

def bokeh_show(plot):
    tmp_output_filename = tempfile.NamedTemporaryFile(suffix='.html').name
    output_file(tmp_output_filename)
    save(plot)

    f = open(tmp_output_filename, "r")
    display(HTML(f.read()))

Then we can use this function bokeh_show to show any plots we create:

p = figure(title="Bokeh test", plot_width=300, plot_height=300)
p.circle([1, 2], [3, 4])

bokeh_show(p)

[1] https://deepnote.com/@jz/Bokeh-in-Deepnote-V6xAdXsXSTKYrEE84782qg

[2] https://stackoverflow.com/questions/31562898/bokeh-save-plot-as-html-but-dont-show-it/45598392#45598392

Last updated