[IPython-User] Reusable notebooks

Thomas Kluyver takowl@gmail....
Mon Jul 23 15:00:34 CDT 2012


Hi Adam,

This is a topic that's of interest to many of us working on IPython -
how can we combine our quick, exploratory style of coding with
something lasting that can be verified and replicated. We don't claim
to have all the answers, but we think the notebook is a very useful
step in that direction.

On 23 July 2012 20:37, Adam Hughes <hughesadam87@gmail.com> wrote:
> I created a notebook that takes in a datafile and does analysis to it based
> on my very customized needs.  I would not share this with anyone; however, I
> do want to reuse it for several different datafiles.  What I'm doing now is
> manually changing the name of the datafile at the top of the notebook and
> then just doing shift+enter to rerun everything one cell at a time.
>
> My question is how would I change this so that the notebook can be run all
> at once with the datafile as an input.  Basically, how can I turn this into
> a standalone script that is run from the command line for example?  Is this
> possible in IPython?  If so, can you guys direct me to some examples or
> discussion where it was already covered?  I'm having some trouble finding my
> away around notebook resources; they seem scattered around the web a bit.

I would suggest two possible approaches:

- Staying in Python, refactor your data analysis so that calling a
single function runs all the steps. Then running it for several
datafiles is as simple as writing a for loop at the bottom of the
notebook.
- Save the notebook as a Python script (either via the 'File >
Download as' menu, or start the notebook server with the --script
option to get a .py file whenever you save a notebook). Then you can
open it in an editor and adjust it from interactive-use code to
something that you can run from the command line.

I've used both of these approaches myself - what makes sense will
depend on the kind of analysis you're doing, and what sort of workflow
you want to fit it into - e.g. I have a Makefile to control parts of
my data analysis, which lends itself to a script & file based
approach.

Best wishes,
Thomas


More information about the IPython-User mailing list