How to show plots as png inside a notebook?

I am using Bokeh plots inside a (classical) Jupyter notebook and that works fine.

However, when converting the notebook to pdf or showing the notebook directly from github, none of the plots are shown.

I previously used the “plotly” library instead of Bokeh and there it was possible to
choose if the plot is shown as an interactive graph (which also cannot get converted to pdf or shown in github) or instead render the plot as png, svg, or some other static image inside the notebook. In that case the plot is visible after conversion or when viewing the notebook directly from github.

I could not find a similar option for Bokeh. Am I missing something? Is it possible with Bokeh to render a plot as a static image inline in a Jupyter notebook?

You’ll need to use IPython.display as demonstrated in this notebook:

https://github.com/bokeh/tutorial/blob/main/notebooks/13_exporting_embedding.ipynb

So this basically means it is not possible for bokeh to produce the image data in memory / in the notebook only since with display method, each plot needs to get stored as a file somewhere first, right?
Which means also that the client code needs to somehow take care of cleaning up all the files when the notebook is exited or the program terminates?

This looks a bit clumsy to me TBH, it should not be hard, I think, for bokeh to produce the image data on the fly and send it to the output in a panel or the notebook without the necessity to store it in a file first? This is something that works quite simply in Plotly because with any invocation of show() one can specify the renderer for this, e.g. show(renderer=“png”) or show(renderer=“notebook”) which makes it easy to control how this is done for the entire notebook and e.g. choose a different renderer when the notebook gets converted to html or pdf

So this basically means it is not possible for bokeh to produce the image data in memory

I didn’t say that, but you also didn’t specify what exactly you were after, so I gave you the simplest most common answer. You can also look at the lower level get_screenshot_as_png function that returns a PIL.Image. I’m not sure what Jupyter APIs if any are needed to display a PIL.Image inline in notebooks, so I can’t advise on that, but I would imagine it is possible.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.