Skip to main content

Run IPython notebooks from the command line

Project description

The 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. runipy lets you run such programs directly, without first converting to a pure Python script.

  • Report generation: runipy can 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, runipy lets you automate that pipeline without losing the notebook formatting.

Installation

The easiest way to install runipy is with pip:

$ pip install runipy

Command-line use

To run a .ipynb file as a script, run:

$ runipy MyNotebook.ipynb

To save the output of each cell back to the notebook file, run:

$ runipy -o MyNotebook.ipynb

To save the notebook output as a new notebook, run:

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

To run a .ipynb file and genereate an HTML report, run:

$ runipy MyNotebook.ipynb --html report.html

Passing Arguments

You 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.ipynb

Then in the notebook, to access myvar:

from os import environ
myvar = environ['myvar']

environ is just a dict, so you can use .get() to fall back on a default value:

from os import environ
myvar = environ.get('myvar', 'default!')

Programmatic use

It 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 enable pylab with:

r = NotebookRunner(notebook, pylab=True)

Credit

Portions of the code are based on code by Min RK

Thanks to Kyle Kelley, Nitin Madnani, George Titsworth, Thomas Robitaille, Andrey Tatarinov, Matthew Brett, and Adam Haney for patches, documentation fixes, and suggestions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

runipy-0.0.8.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file runipy-0.0.8.tar.gz.

File metadata

  • Download URL: runipy-0.0.8.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for runipy-0.0.8.tar.gz
Algorithm Hash digest
SHA256 9d3524d463bfdfe0262a3bd6f93f754548a66d8cba3ff33c7900c3e8ea7a4c81
MD5 467cb44b95e0313cc860811de4de6bbd
BLAKE2b-256 a840aa742ab5ce3b2c0f9b853416d1cbeea801c3d505366af2cd4b5dfa41aa6c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page