The world’s leading publication for data science, AI, and ML professionals.

Self-contained reports from Jupyter Notebooks

Make them interactive and with hideable code cells

Introduction

You’ve just written an amazing Jupyter Notebook, and you’d like to send it to your coworkers. Asking them to install Jupyter isn’t an option, and neither is asking IT for a server on which to host your page. What do you do?

I’ll show you how to export your notebook as a self-contained html report which anyone can open in their browser. I’ll start with the simplest possible example of how to export an html report, then I’ll show how to hide the input cells, and finally I’ll show how to toggle showing/hiding code cells.

Source material can be found here.

Hello world example

Let’s start with the simplest possible example – we have a "hello world" notebook and we’d like to export it as a self-contained html report. We can do this by running

If look into the build directory and open the generated html file in the browser, we see

This is a self-contained file which you can send anyone as an email attachment and they’ll be able to open it in their browser!

Fibonacci example

Now suppose we have a notebook which calculates the first 100 Fibonacci numbers and plots them. If our end goal is to send a report to a non-technical coworker, we may want to exclude any code cells from our report and only keep the output and markdown.

We can do with with the --no-input tag: if we run

then we get a self-contained report, with an interactive plot, no code cells, and which anyone can open in their browser!

Fibonacci example – toggle showing/hiding code cells

In the previous example, all the code cells were hidden from the report. But what if we would still like to give our end-user the ability to selectively show or hide them? This requires a bit of a hack.

  • step 2: edit this cell’s metadata so it includes the tags remove-input. You can edit a cell’s metadata by clicking on the property inspector in the left-hand side pane:
  • step 3: if there are any other cells whose input (or output) you want to remain always hidden, add remove-input (resp. remove-output) to their metadata;
  • step 4: from the terminal, run

    Finally, you’ll end up with a toggle button with which you can selectively show or hide your code cells!

Conclusion

I’ve showed you how to export a self-contained, interactive report with hideable code cells from a Jupyter Notebook. This is a great solution if you need to share a Jupyter Notebook with someone without having to ask them to install any extra tools. Source material can be found here.


Originally published at https://marcogorelli.github.io/.


Related Articles