#!/usr/bin/env python # coding: utf-8 # In[ ]: get_ipython().run_line_magic('load_ext', 'load_style') get_ipython().run_line_magic('load_style', 'talk.css') # # [runipy](https://github.com/paulgb/runipy) #
# ATTENTION: If you have an old installation of **Runipy**, it is very unlikely to work with the new (format v4) IPython notebook (latest version 3.0 of IPython). # # I recommend that you install the latest development version of **runipy** from the [github repository](https://github.com/paulgb/runipy) with the following: # # ``` # pip install https://github.com/paulgb/runipy/archive/master.zip # ``` # # Another solution (for IPython version 3) is to use **nbconvert**, which now comes with an option to run a notebook in place and save the outputs in the same notebook, the (rather verbose) syntax is: # # # ``` # ᐅ ipython nbconvert notebook_to_execute.ipynb --ExecutePreprocessor.enabled=True --to notebook --output notebook_to_execute.ipynb # ``` # # However it does not seem to handle passing environment variables as **runipy** does ... # # see [http://ipython.org/ipython-doc/dev/whatsnew/version3.html](http://ipython.org/ipython-doc/dev/whatsnew/version3.html) for the list of changes in the version 3.0 of IPython and the IPython notebook, see in particular the [backwards incompatible changes](http://ipython.org/ipython-doc/dev/whatsnew/version3.html#backwards-incompatible-changes) section. # #
# The following comes from the doc of **Runipy** and goes through its major use cases # The [IPython notebook](http://ipython.org/notebook.html) 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. # # ### Requirements # # ``runipy`` currently supports IPython versions 2.3.x and the current # development version of 3.x. # # ### 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 # # **NOTE**: *The notebook seems to be automatically converted back to version 3 if it was in version 4.* # # To save the notebook output as a *new* notebook, run: # # $ runipy MyNotebook.ipynb OutputNotebook.ipynb # # To run a ``.ipynb`` file and generate 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!') # # ### Stdin / Stdout # # ``runipy`` can read stdin and stdout and sit in a UNIX pipeline: # # :: # # $ runipy --stdout < MyNotebook.ipynb > OutputNotebook.ipynb # # $ cat MyNotebook.ipynb | runipy --stdout > OutputNotebook.ipynb # # ### 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) # # 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 (False by default). If True, # exceptions will be ignored and the notebook will continue to execute # cells after encountering an exception. The second argument is # progress\_callback, which must be either None or 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 #