Can I run Jupyter notebook cells in commandline?

PythonCommand LineIpythonJupyter

Python Problem Overview


I am deploying a Python package, and I would like to run a simple test to see if all cells in my notebook will run without errors. I would like to test this via commandline as I have issues running a notebook in virtualenv. Is there are simple command-line way to test this?


Note to the moderator: this question has been marked as a duplicate of How to run an .ipynb Jupyter Notebook from terminal? . However, this question was posted (asked Feb 18 '16 at 2:49) several days before that one (asked Feb 22 '16 at 3:35). At most, that post might be marked as a duplicate, and if deemed so, an appropriate action would be to merge the two questions, maintaining this, the original, as the master.

However, these questions may not be duplicates (the intent of the other author is unclear). Regardless, this question and it's answers specifically address executing cells within a jupyter notebook from the terminal, not simply converting notebooks to python files.

Python Solutions


Solution 1 - Python

nbconvert (a jupyter tool for notebook conversion) allows you to do this without any extra packages:

Just go to your terminal and type

> $ jupyter nbconvert --to notebook --inplace --execute mynotebook.ipynb

Source

(Thanks Stephan for suggesting the --inplace flag)

NOTE: This said, I'd try to convert everything you need to a proper script. Jupyter notebooks are thought to use for exploring and sharing results, and not as a replacement of traditional programs.

Solution 2 - Python

You can use runipy to do this.

runipy will run all cells in a notebook. If an error occurs, the process will stop.

> $ pip install runipy > > $ runipy MyNotebook.ipynb

There are also commands for saving the output file as a notebook or an html report:

> $ runipy MyNotebook.ipynb OutputNotebook.ipynb > > $ runipy MyNotebook.ipynb --html report.html

Solution 3 - Python

You can also try papermill which allows you to execute notebooks from command line, and also pass parameters:

For example:

$ papermill mynotebook.ipynb mynotebook_output.ipynb -p start "2017-11-01" -p end "2017-11-30"

You can also run it without passing any parameter.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpylangView Question on Stackoverflow
Solution 1 - PythonfiniteautomataView Answer on Stackoverflow
Solution 2 - PythonRajeshMView Answer on Stackoverflow
Solution 3 - PythonGiacomoView Answer on Stackoverflow