Package: runipy
runipy: run IPython as a scriptThe IPython notebook provides an interactive interface to a Python interpreter.Literate programming: the IPython notebook is an ideal format for
writing “literate” programs, in which the code is part of a larger multi-media
document.runipylets you run such programs directly, without first
converting to a pure Python script.Report generation:runipycan run the notebook and convert it into HTML
in one go, making it an easy way to automate reports.Data pipeline: if you use IPython notebooks to create a data pipeline,runipylets you automate that pipeline without losing the notebook
formatting.Requirementsrunipycurrently supports IPython versions 2.4.x, 3.2.x and the current development
version of 4.x.InstallationThe easiest way to installrunipyis withpip:$ pip install runipyCommand-line useTo run a.ipynbfile as a script, run:$ runipy MyNotebook.ipynbTo save the output of each cell back to the notebook file, run:$ runipy -o MyNotebook.ipynbTo save the notebook output as anewnotebook, run:$ runipy MyNotebook.ipynb OutputNotebook.ipynbTo run a.ipynbfile and generate anHTMLreport, run:$ runipy MyNotebook.ipynb --html report.htmlPassing ArgumentsYou can pass arguments to the notebook through environment variables.
The use of environment variables is OS- and shell- dependent, but in a
typical UNIX-like environment they can be passed on the command line
before the program name:$ myvar=value runipy MyNotebook.ipynbHere is one way this can be done from pure python:from os import environ
from subprocess import call

environ['myvar'] = 'value'
call(["runipy", "MyNotebook.ipynb"])Then in the notebook, to access myvar:from os import environ
myvar = environ['myvar']environis just adict, so you can use.get()to fall back on
a default value:from os import environ
myvar = environ.get('myvar', 'default!')Stdin / Stdoutrunipycan read stdin and stdout and sit in a UNIX pipeline:$ runipy --stdout < MyNotebook.ipynb > OutputNotebook.ipynb

$ cat MyNotebook.ipynb | runipy --stdout > OutputNotebook.ipynbProgrammatic useIt is also possible to run IPython notebooks from Python, using:from runipy.notebook_runner import NotebookRunner
from IPython.nbformat.current import read

notebook = read(open("MyNotebook.ipynb"), 'json')
r = NotebookRunner(notebook)
r.run_notebook()and you can enablepylabwith:r = NotebookRunner(notebook, pylab=True)The notebook is stored in the object and can be saved using:from IPython.nbformat.current import write
write(r.nb, open("MyOtherNotebook.ipynb", 'w'), 'json')run_notebook()takes two optional arguments. The first,skip_exceptions,
takes a boolean value (Falseby default). IfTrue, exceptions will be ignored
and the notebook will continue to execute cells after encountering an exception.
The second argument isprogress_callback, which must be eitherNoneor a
function that takes one argument. This function is called after execution of
each cell with the 0-based index of the cell just evaluated. This can be useful
for tracking progress of long-running notebooks.CreditPortions of the code are based on code byMin RKThanks to Kyle Kelley, Nitin Madnani, George Titsworth, Thomas Robitaille,
Andrey Tatarinov, Matthew Brett, Adam Haney, Nathan Goldbaum, Adam Ginsburg,
Gustavo Bragança, Tobias Brandt, Andrea Zonca, Aaron O’Leary, Simon Guillot,
Fernando Correia, Takashi Nishibayashi, Simon Conseil, and Thomas French
for patches, documentation fixes, and suggestions.