Blogging from IPython

:author: Adam :tags: python, blogging, meta :date: 2013-09-22

There have been many other posts about blogging in ipython, but they all included more overhead than I really wanted to deal with.

Instead, I've gone directly to the source and used nbconvert in ipython 2.0 to convert my notebooks to rst, then put them through pelican.

This post outlines my workflow.

First, and most inconvenient, it is necessary to head any blog post with metadata. I don't have any convenient workflow to deal with that, I just write it in by hand in a raw text box (ctrl-m, t) as you can see above. Daniel Rodriguez took a different approach using the ipython metadata directly and an ipython plugin, but I didn't want to have to worry about installing plugins and I am definitely worried about breaking notebooks by messing with the metadata as the ipy devs give some fairly strict warnings.

Next, the ipynb -> rst step is fairly straightforward. I modified the rst template because I don't want to see the In[#]: and Out[#]: prefixes around my code. I also use .. code-block:: rather than .. code::.

This very simple function below makes sure that my local rst.tbl file is seen before ipython's. Their templates are in IPython/nbconvert/templates. Note that you could normally use Jinja2 templating and "Extend" their template, but I wanted to remove rather than extend.

[ Source ]

In [ ]:
from IPython.nbconvert.exporters import RSTExporter

def export(nbname, outfilename=None):
    exportRST = RSTExporter()
    # exclude default paths
    exportRST.template_path = ['.','/Users/adam/repos/blog'] 

    (body,resources) = exportRST.from_filename(nbname)

    if outfilename is None:
        outfilename = nbname.replace("ipynb","rst")

    with open(outfilename,'w') as f:
        f.write(body)

    return body,resources

I then run the script from the command line: ~/virtual-ipydev/bin/ipython ./nbconverter.py content/GenerateCVExample.ipynb

I'm using a virtual environment with ipython 2 installed because I'm not yet ready to make the jump to the dev version of ipython (though these days, with Travis and Jenkins around, it's probably safe to assume the dev version won't break anything).

With this rst file generated, the only remaining step is pelican, which only requires a make github command in the blog directory. Installing & setting up pelican is reasonably easy, but not the topic of this post.

I've made my own custom pelican theme, so I generally need to update the theme before building:

pelican-themes --upgrade /Users/adam/repos/pelican-themes/mine && make github