Jupyter and Bokeh: workaround for exporting bokeh plots when exporting Jupyter notebook to pdf

做~自己de王妃 提交于 2019-12-13 04:08:35

问题


I was hoping some heavy Jupyter and bokeh users might have a workaround for this. So I have a bunch of bokeh plots in my Jupyter notebook and I want to export the notebook to pdf. In the Jupyter Notebook file menu there is an option to download a notebook as a pdf. Also--and my preferred route--is to use nbconvert.

The problem is that the bokeh plots are not exported into the final pdf file. Bokeh does not export its plots to a format that is caught by Latex to compile the pdf. In constrast, a package like matplotlib will generate png files for each plot, and then those plots are linked in the latex to generate the pdf.

Right now when I export the notebook, all of the text comes out in the pdf but none of the plots come through. The plots come in if I export to an html file--but of course the plots spill off the page if I try and print the document.

Has anyone found a decent workaround for this? The users I work with still want to be able to print out documents and mark them up.


回答1:


Standard interactive Bokeh plots are actually collections of JSON, together with a JavaScript library (BokehJS) that renders the JSON as the desired plot in a browser. Since PDF documents do not execute JavaScript, it will never be possible to embed standard Bokeh plots in PDF form.

However, Bokeh can also export static versions of plots as PNG or SVG, which can be embedded in PDFs. See the section Exporting Plots in the User's guide. You will first need to install some optional dependencies (phantomJS, selenium and pillow) and then to export the plot will be something like this:

export_png(plot, filename="plot.png")

Then, specifically in the notebook, you can display images inline with something like:

from IPython.display import Image
Image('plot.png')


来源:https://stackoverflow.com/questions/48231910/jupyter-and-bokeh-workaround-for-exporting-bokeh-plots-when-exporting-jupyter-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!