Posting Jupyter Notebooks to Blogs

It is nice to be able to publish Jupyter notebooks to straight to Github. I love looking through François Chollet's work with Jupyter Notebooks and Keras. For example, this page: https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/5.4-visualizing-what-convnets-learn.ipynb

At the same time, in a blog it's nice to be able to talk about smaller snippets of code.
The best way to do this is to use the nbconvert function that is installed with jupyter notebooks.

You can convert a notebook with this command line snippet:

jupyter nbconvert --to html --template basic .\protobufs.ipynb

the --template basic argument will strip the html headers off your file, which you will have to do manually if you just use the File->Save functionality inside a Jupyter notebook session.

Grab the generated .html file and add something like this to the top to style things to your liking:

<style type="text/css">
.highlight{background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .1em;padding:0em .5em;border-radius: 4px;}
.k{color: #338822; font-weight: bold;}
.kn{color: #338822; font-weight: bold;}
.mi{color: #000000;}
.o{color: #000000;}
.ow{color: #BA22FF;  font-weight: bold;}
.nb{color: #338822;}
.n{color: #000000;}
.s{color: #cc2222;}
.se{color: #cc2222; font-weight: bold;}
.si{color: #C06688; font-weight: bold;}
.nn{color: #4D00FF; font-weight: bold;}
</style>

Then paste it in to your blog. Simple.

Comments