Search code examples
jupyter-notebookbokehpandas-bokeh

Bokeh import json_item serialized JSON in Jupyter


Cross-post on Bokeh forums: https://discourse.bokeh.org/t/display-json-item-serialized-json-in-jupyter-notebook/7245/3

I've serialized my Bokeh plot to json using bokeh.embed.json_item, as described in the documentation: https://docs.bokeh.org/en/latest/docs/reference/embed.html#bokeh.embed.json_item

I now want to load it again in Python (Jupyter notebook). How do I do so? The documentation only mentions loading it using JavaScript and embedding it on a webpage: https://docs.bokeh.org/en/latest/docs/user_guide/embed.html

One way to do it is by displaying an HTML object in the IPython notebook as follows:

from IPython.core.display import display, HTML
html_plot = f'''
<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://cdn.bokeh.org/bokeh/release/bokeh-2.2.3.min.js"
        crossorigin="anonymous"></script>
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.2.3.min.js"
        crossorigin="anonymous"></script>
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.2.3.min.js"
        crossorigin="anonymous"></script>
</head>
<body>
  <div id="myplot"></div>
  <script>
  item = {plot_json}
  Bokeh.embed.embed_item(item, "myplot");
  </script>
</body>
'''
display(HTML(html_plot))

However, this does not get me back the bokeh Figure object which I can manipulate.


Solution

  • As of Bokeh 2.3 re-ingesting the exported JSON back in to Python is still an open issue.