Customizing Jupyter Notebooks for Presentations and Reports

Jupyter Notebooks are ideal for data analysis when using Python. They allow you to analyze the data, while at the same time writing documentation. The data processing and report writing become intertwined: while you explore and analyze the data, you already have a draft of your report at hand.

Unfortunately, Jupyter Notebooks are not too appealing as a report or for presentations right out of the box. Especially lengthy code blocks disrupt the story you want to tell. However, Jupyter Notebooks allow for a lot of customization. Here, I will show you some basic tweeks in order to make Jupyter Notebooks ready for screen presentations and exported (PDF) reports.

For this tutorial I assume you are using Python 3.x and Jupyter Notebooks through an Anaconda installation. If you don’t have an Anaconda installation, feel free to read my tutorial on how to install Anaconda on your system (no root rights required).

Hide/Show all Code-Cells

For presentations, either via video conference or live, people might not be interested in your implementation details but in the results. Therefore hiding the code input cells is a convenient feature. This can be achieved by adding some custom JavaScript code.

Under $USER/.jupyter/custom add a custom.js file:

$([IPython.events]).on('notebook_loaded.Notebook', function(){
    IPython.toolbar.add_buttons_group([
        {
        'label': 'hide/show code',
        'icon': 'icon-refresh',
        'callback': function(){$('.input').slideToggle()}
        }
    ]);
});

Now you will find a small button labeled “hide/show code” at the top of your notebook, allowing you to toggle the visibility of the code cells. Markdown cells are not affected.

Export to PDF

Exporting to a PDF is very convenient when you want to share your results as a report e.g. by email. In order to export to PDF, Jupyter needs Xelatex (see documentation). Install recomended packages for Texlive + Xelatex usage for Jupyter under Ubuntu/Debian:

sudo apt-get install texlive-xetex
sudo apt-get install texlive-fonts-recommended 
sudo apt-get install texlive-generic-recommended

In case you can not install packages (as you may not be root), it is also very easy to install TexLive as a normal user in a directory of your choice. Just download the archive from the TexLive webpage and follow the instructions in the TexLive quick installation guide.

Restart the Jupyter Notebook and you should be able to export to PDF. Unfortunately, the exported files show all the code, as well as the in- and output prompts.

Tweeking exported files

If you want to send around a report, the code and prompts are not very nice. In order to hide all code in exported documents, and get rid of the input/output prompts (“In [1]:” … “Out [1]:” …), add the following lines to the Jupyter configuration file. First, create the configuration file from your shell:

jupyter notebook --generate-config

then edit the file $USER/.jupyter/jupyter_notebook_config.py and add the following lines (e.g. at the end of the file):

c.TemplateExporter.exclude_input = True
c.TemplateExporter.exclude_input_prompt = True
c.TemplateExporter.exclude_output_prompt = True

After restarting the Jupyter Notebook, whenever you export your notebook, the code and the in-/output prompts will be removed and only the output will be displayed.

Python code in markdown cells

In order to write proper documentation it is possible run inline Python code in markdown cells. This allows you to write text and include e.g. the values from variables right in the text, without typing the values manually.

This can be achieved using the python-markdown extension. Install it using pip and activate the according Jupyter Notebook extension:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable python-markdown/main

In order to be able to run inline python code, you have to trust the notebook (File->Trust Notebook). When you now write text in a markdown cell, you can also run python code within double curly braces {{print('Hello World')}}, or simply display a value from a variable like {{my_python_variable}}. For details, see the documentation.

Finally, we also want the inline code to be computed when the notebook is exported. Add the following line to the end of your Jupyter configuration file ($USER/.jupyter/jupyter_notebook_config.py):

c.Exporter.preprocessors = ['jupyter_contrib_nbextensions.nbconvert_support.pre_pymarkdown.PyMarkdownPreprocessor']

This assumes that the Jupyter Notebook extensions are installed in the site-packages directory, which itself should be included in your $PYTHONPATH – this should be the case if you followed the instructions above.