Can I run Jupyter notebook cells in commandline?

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.


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.


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


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.


As others have mentioned, you can use nbconvert, papermill, or the newest jupyter run. However, they are not well-suited for large projects where you want to break down the logic into multiple steps.

Ploomber allows you to build pipelines where each task can be a notebook, script, or function. After execution, each task generates an output notebook that you can use to review results:

Here's how it looks like:

tasks:
  # also works with .ipynb
  - source: 1-get.py
    product:
      nb: output/1-get.ipynb
      data: output/data.csv
    
  - source: 2-clean.py
    product:
      nb: output/2-clean.ipynb
      data: output/clean.csv
    
  - source: 3-plot.py
    product: output/3-plot.ipynb

To execute the workflow:

ploomber build

It can also setup the virtualenv automatically, assuming you have a requirements.txt:

ploomber install

ploomber build