Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running an IPython/Jupyter notebook non-interactively

Does anyone know if it is possible to run an IPython/Jupyter notebook non-interactively from the command line and have the resulting .ipynb file saved with the results of the run. If it isn't already possible, how hard would it be to implement with phantomJS, something to turn the kernel on and off, and something to turn the web server on and off?

To be more specific, let's assume I already have a notebook original.ipynb and I want to rerun all cells in that notebook and save the results in a new notebook new.ipynb, but do this with one single command on the command line without requiring interaction either in the browser or to close the kernel or web server, and assuming no kernel or web server is already running.

example command:

$ ipython notebook run original.ipynb --output=new.ipynb

like image 929
davidshinn Avatar asked Jul 28 '13 05:07

davidshinn


People also ask

How do I run a Jupyter Notebook in IPython?

In the Notebook Dashboard navigate to find the notebook: clicking on its name will open it in a new browser tab. Click on the menu Help -> User Interface Tour for an overview of the Jupyter Notebook App user interface. You can run the notebook document step-by-step (one cell a time) by pressing shift + enter.

Are Jupyter notebooks interactive?

This library allows us to turn Jupyter Notebooks from static documents into interactive dashboards, perfect for exploring and visualizing data. You can view a completely interactive running notebook with the widgets in this article on mybinder by clicking the image below.

Can you run Jupyter Notebook locally?

(If you don't understand this yet, don't worry — the important point is just that although Jupyter Notebooks opens in your browser, it's being hosted and run on your local machine.

Can you use Jupyter notebooks offline?

Yes, you don't need an internet connection to run jupyter notebook because it runs on the localhost.


1 Answers

Yes it is possible, and easy, it will (mostly) be in IPython core for 2.0, I would suggest looking at those examples for now.

[edit]

$ jupyter nbconvert --to notebook --execute original.ipynb --output=new.ipynb 

It is now in Jupyter NbConvert. NbConvert comes with a bunch of Preprocessors that are disabled by default, two of them (ClearOutputPreprocessor and ExecutePreprocessor) are of interest. You can either enabled them in your (local|global) config file(s) via c.<PreprocessorName>.enabled=True (Uppercase that's python), or on the command line with --ExecutePreprocessor.enabled=True keep the rest of the command as usual.

The --ExecutePreprocessor.enabled=True has convenient --execute alias that can be used on recent version of NbConvert. It can be combine with --inplace if desired

For example, convert to html after running the notebook headless :

$ jupyter nbconvert --to=html --execute RunMe.ipynb

converting to PDF after stripping outputs

$ ipython nbconvert --to=pdf --ClearOutputPreprocessor.enabled=True RunMe.ipynb

This (of course) does work with non-python kernels by spawning a <insert-your-language-here> kernel, if you set --profile=<your fav profile>. The conversion can be really long as it needs to rerun the notebook. You can do notebook to notebook conversion with the --to=notebook option.

There are various other options (timeout, allow errors, ...) that might need to be set/unset depending on use case. See documentation and of course jupyter nbconvert --help, --help-all, or nbconvert online documentation for more information.

like image 183
Matt Avatar answered Oct 07 '22 09:10

Matt