Reputation: 8846
I have some code in a .ipynb file and got it to the point where I don't really need the "interactive" feature of IPython Notebook. I would like to just run it straight from a Mac Terminal Command Line.
Basically, if this were just a .py file, I believe I could just do python filename.py
from the command line. Is there something similar for a .ipynb file?
Upvotes: 299
Views: 521782
Reputation: 1
You can easily convert
filename.ipynb
into a Python file (filename.py) using this command:
jupyter nbconvert --to python --execute filename.ipynb
but why not take advantage of the IPython command line? With a single command, you can effortlessly run your Jupyter Notebook without needing to convert it. Simply use the following command:
ipython filename.ipynb
IPython provides a wide range of features to help you improve the process's efficiency. Visit the official IPython documentation to find all that it can do.
Upvotes: 0
Reputation: 1775
There is now a jupyter execute
subcommand that will execute a notebook.
jupyter execute notebook.ipynb
More on this command can be found in the Jupyter documentation.
Upvotes: 18
Reputation: 5859
I've made some research on this topic and wrote an article on 4 ways to run Jupyter Notebook in command line below is summary of my findings.
nbconvert
The nbconvert package is already installed in Jupyter Notebook. It can be used to execute notebook. Additionally it has many features:
Example notebook execution:
jupyter nbconvert --execute --to notebook --allow-errors your-notebook.ipynb
The above command will output your-notebook.nbconvert.ipynb
file and will execute all cells even with errors.
papermill
The papermill allows you to parametrize notebook. You can define variables as parameters (with cell tags).
Example command:
papermill -p name Piotrek your-notebook.ipynb output-notebook.ipynb
Example notebook with injected parameters:
.py
scriptThere is an option to manually download notebook as .py
script:
After download you can add execution rights to the file and run it as a command line script.
jupytext
The jupytext package allows you to synchronize .ipynb
file with .py
file. You don't need to manually convert notebook to script.
Upvotes: 7
Reputation: 117
In a batch file paste the below. Use ipython to run .ipynb files.
@echo on
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
ipython "path\test.ipynb"
pause
Upvotes: -1
Reputation: 730
You can also use jupytext
https://jupytext.readthedocs.io/en/latest/index.html.
This allows you to pair your notebook. So for each ipynb
file you have a .py
file as well with some comments. The .py
file can be executed as usual.
You can enjoy benefits of two worlds with the cost of one extra file though.
Oh, and by the way if you are using version control you can only commit .py
files with a nice diff instead of the ugly .ipynb
diffs.
(The syntax in the .py
files is similar to Databricks notebooks iy you are familiar with them...)
Upvotes: 0
Reputation: 5722
Using ipython:
ipython --TerminalIPythonApp.file_to_run=<notebook>.ipynb
Normally, I would prefer this option as it is really self-describing. If you prefer to use less characters, use:
ipython -c "%run <notebook>.ipynb"
which is basically what Keto already suggested (unfortunately a little bit hidden) as a comment.
Upvotes: 34
Reputation: 559
I had the same problem and I found papermill. The advantages against the others solutions is that you can see the results while the notebook is running. I find this feature interesting when the notebook takes very long. It is very easy to use:
pip install papermill
papermill notebook.ipynb output.ipynb
It has also, other handy options as saving the output file to Amazon S3, Google Cloud, etc. See the page for more information.
Upvotes: 7
Reputation: 71
From the terminal run
jupyter nbconvert --execute --to notebook --inplace --allow-errors --ExecutePreprocessor.timeout=-1 my_nb.ipynb
The default timeout is 30 seconds. -1 removes the restriction.
If you wish to save the output notebook to a new notebook you can use the flag --output my_new_nb.ipynb
Upvotes: 2
Reputation: 160
You can also use the boar
package to run your notebook within a python code.
from boar.running import run_notebook
outputs = run_notebook("nb.ipynb")
If you update your notebook, you won't have to convert it again to a python file.
More information at:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
Upvotes: 2
Reputation: 7054
From the command line you can convert a notebook to python with this command:
jupyter nbconvert --to python nb.ipynb
https://github.com/jupyter/nbconvert
You may have to install the python mistune package:
sudo pip install -U mistune
Upvotes: 180
Reputation: 729
In your Terminal run ipython:
ipython
then locate your script and put there:
%run your_script.ipynb
Upvotes: 72
Reputation: 950
In my case, the command that best suited me was:
jupyter nbconvert --execute --clear-output <notebook>.ipynb
Why? This command does not create extra files (just like a .py
file) and the output of the cells is overwritten everytime the notebook is executed.
If you run:
jupyter nbconvert --help
--clear-output Clear output of current file and save in place, overwriting the existing notebook.
Upvotes: 12
Reputation: 2132
Update with quoted comment by author for better visibility:
Author's note "This project started before Jupyter's execute API, which is now the recommended way to run notebooks from the command-line. Consider runipy deprecated and unmaintained." – Sebastian Palma
Install runipy library that allows running your code on terminal
pip install runipy
After just compiler your code:
runipy <YourNotebookName>.ipynb
You can try cronjob as well. All information is here
Upvotes: 5
Reputation: 317
For new version instead of:
ipython nbconvert --to python <YourNotebook>.ipynb
You can use jupyter instend of ipython:
jupyter nbconvert --to python <YourNotebook>.ipynb
Upvotes: 10
Reputation: 38638
nbconvert allows you to run notebooks with the --execute
flag:
jupyter nbconvert --execute <notebook>
If you want to run a notebook and produce a new notebook, you can add --to notebook
:
jupyter nbconvert --execute --to notebook <notebook>
Or if you want to replace the existing notebook with the new output:
jupyter nbconvert --execute --to notebook --inplace <notebook>
Since that's a really long command, you can use an alias:
alias nbx="jupyter nbconvert --execute --to notebook"
nbx [--inplace] <notebook>
Upvotes: 252
Reputation: 2729
You can export all your code from .ipynb
and save it as a .py
script. Then you can run the script in your terminal.
Hope it helps.
Upvotes: 46