Torbjorn Zetterlund

Sat 05 2014
Image

Convert a IPython Notebook into a Python file via command line

by bernt & torsten

There are several ways to convert, start a notebook with –script flag will save .py file alongside .ipynb on every save. You can also (as ipynb is JSON), load it, loop through it and eval code cell in the current namespace.

If you don’t want to output a Python script every time you save, or you don’t want to restart the IPython kernel:

On the command line, you can use nbconvert:

$ ipython nbconvert --to=python [YOUR_NOTEBOOK].ipynb

As a bit of a hack, you can even call the above command in an IPython notebook by pre-pending! (used for any command-line argument). Inside a notebook:

!ipython nbconvert --to=python config_template.ipynb

Share: