Distribute a slideshow from IPython notebook - jupyter-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/

Related

VScode markdown preview image size control - pandoc compatible?

Is there some VScode extension that allows image size control in this form?
![caption](image.png){ width=whatever }
That is the form that is used to get Pandoc to control image size in its output. In this case I'd like to use VSCode - one of my favorite tools - to compose markdown destined for docx output. I've got the Pandoc part working, I'd just like be able to get the previewing to work better in VScode. There might be an extension that does this, but there are zillions of markdown extension for VSCode so who knows.
An alternative would be if there custom css code that would do this, but my css knowledge is not sufficient to know if this is even possible.

How can I make a presentable PDF from a 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

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.

Embedding image in ipython notebook for distribution

I have an ipython notebook with an embedded image from my local drive. I was expecting it to be embedded in the JSON along with the output of code cells, but when I distributed the notebook, the image did not appear to users. What is the recommended way (or ways) to embed an image in a Notebook, so that it doesn't disappear if users rerun code cells, clear cell output, etc.?
The notebook system caches images included with ![label](image.png), but they last only until the python "kernel" serving the notebook is restarted. If I rename the image file on disk, I can close and reopen the notebook and it still shows the image; but it disappears when I restart the kernel.
Edit: If I generate an image as code cell output and then export the notebook to html, the image is embedded in the html as encoded data. Surely there must be a way to hook into this functionality and load the output into a markdown (or better yet "raw nbconvert") cell?
from IPython.display import Image
Image(filename='imagename.png')
will be exported (with ipython nbconvert) to html that contains the following:
<div class="output_png output_subarea output_execute_result">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA...
</div>
However, even when I manually embedded this snippet into a markdown cell, I couldn't get the image to display. What am I doing wrong?
Update (2020)
Apparently, the problem has (finally!) been addressed in the newer notebook / Jupyter versions: as of 2018 (thanks for the link #Wayne), the html sanitizer will accept an embedded html image, as in <img src="data:image/png;base64,iV...> . Markdown image syntax also accepts images as embedded data, so there are two ways to do this. Details in these helpful answers:
markdown image syntax (answer by #id01)
html element syntax (in answer by #tel -- note that it works now!)
Are you happy to use an extra code cell to display the image? If so, use this:
from IPython.display import Image
Image(filename="example.png")
The output cell will have the raw image data embedded in the .ipynb file so you can share it and the image will be retained.
Note that the Image class also has a url keyword, but this will only link to the image unless you also specify embed=True (see the documentation for details). So it's safer to use the filename keyword unless you are referring to an image on a remote server.
I'm not sure if there is an easy solution if you require the image to be included in a Markdown cell, i.e. without a separate code cell to generate the embedded image data. You may be able to use the python markdown extension which allows dynamically displaying the contents of Python variables in markdown cells. However, the extension generates the markdown cells dynamically, so in order to retain the output when sharing the notebook you will need to run ipython nbconvert --to notebook original_notebook.ipynb --output preprocessed_notebook using the preprocessor pymdpreprocessor.py as mentioned in the section "Installation". The generated notebook then has the data embedded in the markdown cell as an HTML tag of the form <img src="data:image/png;base64,..."> so you can delete the corresponding code cell from preprocessed_notebook.ipynb. Unfortunately, when I tried this the contents of the <img> tag weren't actually displayed in the browser, so not sure if this is a viable solution. :-/
A different option would be to use the Image class in a code cell to generate the image as above, and then use nbconvert with a custom template to remove code input cells from the notebook. See this thread for details. However, this will strip all code cells from the converted notebook, so it may not be what you want.
The reason why the
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnAAAAFgCAYAAAA...
tag doesn't do anything when you put it in a markdown cell is because IPython uses an HTML sanitizer (something called Google Caja) that screens out this type of tag (and many others) before it can be rendered.
The HTML sanitizer in IPython can be completely disabled by adding the following line to your custom.js file (usually located at ~/.ipython/profile_default/static/custom/custom.js):
iPython.security.sanitize_html = function (html) { return html; };
It's not a great solution though, as it does create a security risk, and it doesn't really help that much with distribution.
Postscript:
The ability to render base64 encoded strings as images != obvious security concern, so there should be a way for the Caja people to eventually allow this sort of thing through (although the related feature request ticket was first opened back in 2012, so don't hold your breath).
I figured out that replacing the image URL in the ![name](image) with a base64 URL, similar to the ones found above, can embed an image in a markdown container.
Example markdown:
![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)
If using the IPython HTML() function to output raw HTML, you can embed a linked image in base64 inside an <img> tag using the following method:
import base64
import requests
from IPython.core.display import HTML
def embedded_image(url):
response = requests.get(url)
uri = ("data:" +
response.headers['Content-Type'] + ";" +
"base64," + str(base64.b64encode(response.content).decode('utf-8')))
return uri
# Here is a small example. When you export the notebook as HTML,
# the image will be embedded in the HTML file
html = f'<img src="{embedded_image("https://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg")}" />'
HTML(html)
UPDATE: As pointed out by #alexis, this doesn't actually answer the question correctly, this will not allow users to re-run cells and have images persist (this solution only allows one to embed the images into exports).
As of Jupyter Notebook 5, you can attach image data to cells, and refer to them from the cell via attachment:<image-file-name>. See the menu Edit > Insert Image, or use drag and drop.
Unfortunately, when converting notebooks with attached (embedded) images to HTML, those images will not show up.
To get them into the HTML code, you can use (for instance) nbtoolbelt.
It will replace those attachment: references by data: with the image data embedded in the img tag.

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