Skip to content

Instantly share code, notes, and snippets.

@rossant
Last active August 29, 2015 14:12
Show Gist options
  • Save rossant/46f721556da5aee3988b to your computer and use it in GitHub Desktop.
Save rossant/46f721556da5aee3988b to your computer and use it in GitHub Desktop.
IPython notebook ==> Python function

Notebooks are great for interactive use, not so much for being reused in an automatic fashion. How to make a notebook reusable (for example in an analysis pipeline)?

Write your notebook 'mynotebook.ipynb' as usual. Then add a special comment in some cells:

### input
x = 0
y = 0

The cells with this comment work normally in the notebook. They let you initialize variables as you would normally do in a notebook. But in addition to that, these cells also serve to specify some variables as arguments to a reusable function, with default values.

...

At the end of the notebook, add another special comment to one or several cells.

### output
z

Typically, this is where you display the end result.

That's it! You've given all the necessary information to convert your interactive notebook into a reusable Python function. You can use it in a Python script (for example, a pipeline script) as this:

import nbfun
f = nbfun.load('mynotebook.ipynb')
z = f(x=1, y=2)

This nbfun module doesn't exist yet but it should be easy to do.

The runipy module implements a similar idea.

@mpelko
Copy link

mpelko commented Jan 4, 2015

Hmmm ... I am trying to come up with the case where I would want a reusable analysis function in a notebook. Typically I would prefer to put reusable ones in a .py file (I normally have analysis.py or analysis_functions.py files around).

The way I see it the main benefit of the proposed solution is to skip the step of porting the analysis function from the notebook (presuming this is where you developed it) to the .py file. Am I missing something?

@rossant
Copy link
Author

rossant commented Jan 4, 2015

.py files are definitely better for reusability. But you typically create your functions incrementally with a lot of trials and errors, and the notebook is great for that. The proposed solution just saves the burden of porting manually the code into a .py file and encapsulating the code into a function. This manual process would only take a couple of minutes, but if you integrate your notebook in a pipeline, you may need to repeat this dozens of times, as soon as you need to tweak something in your notebook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment