How can I make a presentable PDF from a Jupyter Notebook? - jupyter-notebook

I know that I can click on File -> Print Preview and let my browser save the result as a PDF. But that PDF will contain quite a bit of code.
Can I hide parts from the print preview or make a PDF with another tool?
Ideally, there would be magic functions:
%hide: Hide the cell and its output
%hide-code: Hide only the code of the cell, but show the output of the code

Print to PDF in the browser
In JupyterLab, you can hide the cell's input and output by clicking the side bar as demonstrated in the doc. After hiding, you can use the browser's printing menu to generate a PDF. I've tested it on FireFox and expect similar result on Chrome or Edge.
Export to PDF with nbconvert
If you aim at a native conversion from ipynb format to pdf, the nice old nbconvert tool is your friend. It usually comes together with Jupyter and can be invoked conveniently in the menu of JupyterLab File->Export Notebook As...->PDF or, in the classic Jupyter Notebook interface, File->Download as->PDF (via LaTeX).
Hiding input/output in cells can be realized by setting the cell's metadata hide_input=true and installing a nbextension as discussed in issue #155.
Print Preview menu in the classical Jupyter
Finally to your question
Can I hide parts from the print preview or make a PDF with another tool?
The "print preview" button in the classical Jupyter interface is a shortcut for calling nbconvert to generate a HTML file and redirect your browser to it. Therefore, similar configuration for nbconvert to hide input as discussed above (in issue #155) can be used. This feature is implemented by jupyter_contrib_nbextensions bundle and explained in the doc.

As the above-mentioned answers do not work for me; I found another solution:
I use an additional package notebook-as-pdf.
Read the tutorial here. It uses Chromium and is the only approach that worked for me so far reliably. The "print preview" does still not work. But it produces a clean PDF with no code input.
I do:
pip install -U notebook-as-pdf
pyppeteer-install
jupyter nbconvert --to PDFviaHTML --TemplateExporter.exclude_input=True PATH_TO_YOUR_FILE.ipynb
What did NOT work for me:
The solution above.
All the scripts mentioned in those answers

Related

embedding image into jupyter notebook and exporting to HTML

I am running Python 3.7 on Windows using pycharm. I have a jupyter notebook and I would like to embed an image into the notebook. I know all the ways of doing standard embedding with markdown language, BUT ideally what I want is:
a. Embed the image via markdown language, i.e. the notebook cell is in 'markdown' state, not 'Code' state, AND ALSO
b. Have it able to export to HTML and retain that image in the HTML file. i.e. on the notebook. I want to click File -> Download as -> HTML (.html), save the notebook file in .html format, and then when I send it to my buddy, the image that I attached is in the notebook, and he sees it.
I know i could do this in a cell ('code'):
from IPython.display import Image
Image(filename="myfile.jpg")
but I do not want to use 'Code', since when I send to my buddy, he will see the In [] code statement and the Out [] of the image in the notebook html file.
Note: This would be an image that was on my laptop that I would want in the html formatted exported notebook. It is NOT on the web where he could refer to it with a www type statement. Unless I'm crazy, there is no way to do this with markdown command in a cell, the only way to do it (with the image embedded 'permanently' into the .html format of the notebook), would be via a cell that was in 'Code' celltype.
When you use a code cell to show an image and then export the notebook to an HTML file, the image is converted to Base64 and the code directly used in the src attribute of the <img> tag. You can apply the same procedure with images included in markdown cells.
First, encode your image as Base64, e.g. by using one of the online enocders.
Create a markdown cell and include an <img> tag which uses your Base64 code, e.g.:
<img src="data:image/png;base64,CODE_FOLLOWS_HERE" />
Evaluate the cell and you should already see your image.
If you now export your notebook to HTML, the image should be included in the file the same way as images from code cells.
The only disadvantage with this approach is that your markdown cell gets cluttered with the (probably long) Base64 code. However, this is manageable by e.g. using a markdown cell dedicated solely to the image without other content.
You can install the Unofficial Jupyter Notebook Extensions.
It has some interesting extensions (e.g. spell checker, collapsible headings, ...). One of the extensions is Export HTML With Embedded Images which exactly does what you want.
To install Nbextensions using pip do the following:
$ pip install jupyter_contrib_nbextensions
$ pip install jupyter_nbextensions_configurator
$ jupyter contrib nbextension install --user
$ jupyter nbextensions_configurator enable --user
Then you will see in your Jupyter homepage a new tab (Nbextensions), where you can enable and configure different extension.
After enabling the "Export HTML With Embedded Images", you will see the corresponding option in the "File-Download as" menu.
My complete solution is based on Milania and
encoding-an-image-file-with-base64
how-to-base64-encode-an-image-using-python
BytesIO.getvalue
the code
import base64, io, IPython
from PIL import Image as PILImage
image = PILImage.open(image_path)
output = io.BytesIO()
image.save(output, format='PNG')
encoded_string = base64.b64encode(output.getvalue()).decode()
html = '<img src="data:image/png;base64,{}"/>'.format(encoded_string)
IPython.display.HTML(html)
For me, on Visual Studio Code, something like this did the trick (in a markdown cell, as you requested, and a image that you want to embed in your notebook and further be exported to the html output):
<figure>
<img src="./notebook_img/diptera_taxat_yes_no.jpg" width="200"/>
<figcaption>Limit the search on Diptera</figcaption>
</figure>
Where the image is located in "./notebook_img" relative to the location of the notebook (in this sense, the notebook is located in .)
Your buddy will not see the code from above when reading the HTML exported file, so that should satisfy the requested need as far as I understand. He will also not need the folder "notebook_img".

How to configure "Download As..."

After a day of research I managed to do a custom template for nbconvert that does what I needed to do (hide the input cells as part of a pdf conversion). However this only works in the command line. I'd like to be able to do this by chosing from the export menu, since exporting with or without output is something I'll switch frequenly.
I did some more research and found out that by adding
c.LatexExporter.template_file = 'noinput.tplx'
To my jupyter_notebook_config.py file should update the "Download As..." PDF with latex option, however it doesnt seem to do anything. Maybe there are some additional settings I should activate in the configuration file? Documentation is rather unclear in how this particular menu works.
Thanks for any help.
Check out this answer. When you use nbconvert from the command line does it use your 'noinput.tplx' template by default?

Is there a way to print a jupyter/ipython notebook slide presentation?

Is there a way to print out a slide deck of a jupyter/ipython notebook slides? Is it possible to do from the nbviewer site (http://nbviewer.ipython.org) ?
I know that I can print a pdf of my notebook, but when I do, it doesn't have the same page breaks and doesn't skip all the code that I would like skipped (for example, the libraries I've imported aren't necessary). I'd like to have it as a backup or a printable handout.
You can try this:
jupyter nbconvert --to slides --post serve /path/to/your/notebook.ipynb
This should fire up your browser and serve the presentation (e.g at http://127.0.0.1:8000/<some-title>.slides.html#/)
change the url to
http://127.0.0.1:8000/<some-title>.slides.html?print-pdf
If you now open the print dialog from your browser, the slides should have the right formatting.
Instead of sending to a printer you should be able to choose to write to a pdf file from the printer menu.
I tested this in chrome on OSX. I assume it works on all systems, but I did not test.

Distribute a slideshow from IPython notebook

so I am adoring the new IPython notebook slideshow feature, however I could not figure out how to distribute such a slideshow in a userfriendly (I am no HTML/JS guy) way.
My usecase is:
I have a somewhat messy notebook which I want to filter by tagging cells as slides/skip/- etc.
In an optimal world there would be a fire-and-forget 'give me a pdf' button somewhere.
So I did already view the slides locally via ipython nbconvert ... --to slides -- post serve
But how do I distribute that to others? Can I get a pdf from such a slideshow easily (I do not care about transition animations etc.)
I hope this is developed further, great features so far!
Currently, there is only a hackish way, see GitHub Issue.
You might also try Slideviewer (in contrast to NBviewer).
$ ipython nbconvert ... --to slides (no serve option necessary) create a standalone html file you should be able to mail, or whatever.
The skip/- logic can be applied to pdf generation too, you just have to write your own extended template (which is not that hard, wild guess ~20 lines)
You can print it as a pdf file from Chrome.
Add "?print-pdf" at the end of your URL.e.g: 127.0.0.1:8000/index.html?print-pdf
Select print menu from Chrome.
Select Save As pdf, then print it out.
buddy, I'm sure this is what you want
https://github.com/damianavila/RISE
for more details, check here
https://conference.scipy.org/scipy2014/schedule/presentation/1718/

Is it possible to exclude some cells from the ipython notebook when using NBConvert

Is there a method to exclude some cells from the NBconvert process
For instance. An embedded video is cool when running to HTML, but when converting the HTML to PDF it creates a problem. So I want to exclude it in some instances.
Found a nice workaround for this, using the 'slides' option of nbconvert:
In your iPython notebook under "Cell Toolbar" select "Slideshow"
Then in the top right of the cells that you don't want to show select Slide Type "skip"
Now run python nbconvert your_notebook.ipynb --to slides
Instead of serving the slide, just open the resulting html in a browser.
And.. It doesn't contain the slides you told it to skip!
Hope this helps.

Resources