How to run a Jupyter Notebook .ipynb file from terminal or cmd prompt ?

This tutorial guides you on how to run a Jupyter Notebook .ipynb file from terminal or cmd prompt. In our exercise I will teach you how to run a notebook file from Windows command prompt.

Run a Jupyter Notebook .ipynb file from terminal or cmd prompt

Let’s say you have installed Jupyter Notebook and created a notebook file with some code in a .ipynb file. And after some time you really don’t want the interactive feature of Jupyter Notebook and you wanted to run that code straightaway from the command prompt using Python.

Basically, you wanted to run jupyter notebook .ipynb file from terminal or cmd prompt directly. Then you are at the right place. Let’s see how to do that.

Firstly, you need to convert the jupyter notebook file which is in the format .ipynb to .py format using the jupyter nbconvert tool.

Jupyter nbconvert usage

The nbconvert tool, jupyter nbconvert converts notebook file to various other formats using jinja templates.

From the command line, you need to use the following command structure to convert Jupyter Notebook file .ipynb to a different format.

jupyter nbconvert --to <output format> <input notebook>

Whereas, the <output format> is the desired output format. And the <input notebook> is the jupyter notbook filename.

For example, to convert .ipynb file to .py (Python) file you need to run the following command.

> jupyter nbconvert --to python MyFirstNotebook.ipynb
[NbConvertApp] Converting notebook MyFirstNotebook.ipynb to python
[NbConvertApp] Writing 1461 bytes to MyFirstNotebook.py

Verify whether .py file is created in your working directory.

> dir
 Volume in drive C is Acer
 Volume Serial Number is BE62-23C2

 Directory of C:\Users990\Documents\sneppets\Workspaces\Python\Examples

14-04-2021  15:04    <DIR>          .
14-04-2021  15:04    <DIR>          ..
14-04-2021  14:42    <DIR>          .ipynb_checkpoints
14-04-2021  11:58                21 hello.py
14-04-2021  13:13    <DIR>          img
14-04-2021  15:04           141,807 MyFirstNotebook.ipynb
14-04-2021  14:34               591 MyFirstNotebook.py
               4 File(s)        142,491 bytes
               4 Dir(s)  346,255,458,304 bytes free

For example, MyFirstNotebook file’s screenshot for your reference.

Finally, you could run a jupyter notebook .ipynb file from command prompt using the converted .py file as shown below.

> python MyFirstNotebook.py
Hello World
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>

That’s it. Hope it helped 🙂

You’ll also like:

References

guest
0 Comments
Inline Feedbacks
View all comments