How to run an .ipynb Jupyter Notebook from terminal?
To run a Jupyter Notebook from the terminal, utilize this command:
This one-liner runs my_notebook.ipynb
and dumps the result in executed_notebook.ipynb
. Don't forget you'll need the Jupyter suite and the trusty nbconvert tool installed.
In other words, this is the ctrl-alt-del
of the Jupyter world; you'll get your notebook running without ever touching the mouse!
Running Notebooks in Terminal: A Deep Dive
The nbconvert Way
If the Jupyter GUI is not your cup of tea, nbconvert
got your back! Run your notebook, convert it and even make coffee (well, not really, but wouldn't it be great?) using the classic command:
You know the deal - swap <format>
with notebook
or python
, and <notebook>
with your file, like so:
That's it! Your notebook morphed into a Python script and ran faster than Bolt on the Olympics.
Making Changes Stick with --inplace
Want to keep the output in the same notebook? Say no more:
This hocus-pocus
replaces the original notebook with the new executed one. Abracadabra!
Straight to the Point with IPython
IPython lets you score the goal without all the dribbling:
Or, you could stick to the classic move inside the IPython shell:
And your notebook will run as if it was prom night!
Ring the Alias for Backup
Doing this too often? Go DRY! Set an alias in your .bashrc
or equivalent:
After this, just type run_notebook your_notebook.ipynb
and you're golden!
Troubleshooting and Prerequisites
Before trying to run these power plays, get your ducks in a row. You might need mistune
for nbconvert
to play nice:
More problems? Swing by the nbconvert GitHub repository for the latest gossip.
When .ipynb Becomes .py
Sometimes, your notebook needs a costume change. When integrating with a deeper workflow or setting up continuous integration services, transforming it into a .py can be a true-life saver. Use Jupyter's own tools or nbconvert
to get this done quickly.
Tips, Tricks and Potential Pitfalls
Configuration Files for Smooth Sailing
To make your routine simpler, bake your commands into a jupyter_nbconvert_config.py
. Define default conversion types and outputs in the configuration file, so you can keep your commands short and sweet.
Error Handling Like a Boss
Running non-interactive execution? You'll need a plan for when things go down. Use --ExecutePreprocessor.allow_errors=True
to keep the party going even when you hit a snag.
Continuous Integration and Notebooks
Jupyter notebooks and CI pipelines go together like French fries and milkshakes! Convert your .ipynb
to .py
for easy integration with your CI tools.
Regular Execution with Cron
Combine nbconvert
and cron for regular updates. If you're generating reports, working with live data, or just plain forgetful, this trick's for you!
Parameterized Execution with Papermill
Want to take your execution to the next level? Try papermill
on for size! This tool lets you inject parameters and execute notebooks, ideal if you need to punch out multiple reports with different datasets.
Was this article helpful?