Normally people run jupyter notebook via browser, but in some situation, we will need to run it from terminal, for example, when running the script takes long time.
This post introduces how to run a jupyter notebook script from terminal.
runipy can do this.
runipy will run all cells in a notebook. If an error occurs, the process will stop.
- Install runipy package
$ pip3 install runipy # for python 3.x $ pip install runipy # for python 2.x
- runipy command-line usages
- 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 generate an HTML report, run:
$ runipy MyNotebook.ipynb --html report.html
The latest versions of jupyter comes with the
nbconvert command tool for notebook conversion allows us to do this without any extra packages.
Just go to your terminal and type:
$ jupyter nbconvert --to notebook --execute mynotebook.ipynb --output mynotebook.ipynb
This will open the notebook, execute it, capture new output, and save the result in
mynotebook.nbconvert.ipynb. By default,
nbconvert will abort conversion if any exceptions occur during execution of a cell. If you specify
--allow-errors (in addition to the
--execute flag) then conversion will continue and the output from any exception will be included in the cell output.
if you meet this error,
raise exception(“Cell execution timed out”)
$ jupyter nbconvert --to notebook --execute --allow-errors --ExecutePreprocessor.timeout=180 mynotebook.ipynb
You can use the –inplace flag as well:
$ jupyter nbconvert --to notebook --execute --inplace mynotebook.ipynb
check here for more (updated) usages about nbconvert jupyter command tool.