The Python Oracle

How to run an .ipynb Jupyter Notebook from terminal?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles

--

Chapters
00:00 Question
00:43 Accepted answer (Score 155)
01:05 Answer 2 (Score 224)
01:46 Answer 3 (Score 51)
02:04 Answer 4 (Score 42)
02:22 Thank you

--

Full question
https://stackoverflow.com/questions/3554...

Accepted answer links:
https://github.com/jupyter/nbconvert
[mistune]: https://pypi.org/project/mistune

Answer 4 links:
[image]: https://i.stack.imgur.com/c1Dvz.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #jupyternotebook #ipython #nbconvert

#avk47



ANSWER 1

Score 248


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>



ACCEPTED ANSWER

Score 174


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



ANSWER 3

Score 71


In your Terminal run ipython:

ipython

then locate your script and put there:

%run your_script.ipynb



ANSWER 4

Score 43


You can export all your code from .ipynb and save it as a .py script. Then you can run the script in your terminal.

code export sample

Hope it helps.