You can easily write some utility to convert Jupyter notebook (or Zeppelin notebook) to script and run the script in Python without having to change a single line of code, with all the output automatically redirected to an html file.

Detection of the run context can be automated. You can easily detect whether you are running the code in script mode, Zeppelin, Jupyter or an IDE. The HTML output file for script run can be controlled by an env variable with a suitable default value, using the calling file name (that one can get from the Python stack) and a timestamp.

The notebook conversion simply needs to insert a mylib.showCodeBlock(…) after each Jupyter/Zeppelin code cell. One can use Google prettify to render the code blocks when the showCodeBlock() is executed. Some Python stack navigation is necessary to do so — not that difficult (you need to find the calling line in the file and the file from which the line was called when running as script, while keeping track of the last line of the previous block rendered — you shoud also drop the showCodeBlock() line from the code rendering).

One can also render all the markdown as HTML blocks. The results is a nice clean HTML version of the notebook output with code and documentation.

In doing so one needs to wrap up all outputs (print, figures) to redirect to the HTML file. Maybe one could do that by writing an HTML back-end but an easy solution is to develop a library that supports all major graphical outputs — it’s not that difficult. matplotlib, plotly, seaborn, bokeh, SHAP, pivottables, etc can all be nicely rendered in HTML by using an explicit mylib.show(…) instead of the Jupyter default display on last object in a code block.

By doing so you can also add different visualisation options for dataframe, using table widgets. It’s nice and let’s you interact with the data in the HTML.

You can also redirect print and handle issues like df.info() (with prints to stdout by default and needs to be redirected). You may also overwrite the default print to print to the html file.

At the end of the day it makes moving from prototyping in Jupyter to scripts in production very easy — your run have full graphical support and rendered code blocks in the HTML output. It also allow you to run your prototyping code with different set-ups while still working on the notebook. Such a library is the ‘missing tool’.

Written by

Engineer, Data Scientist, Flâneur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store