How to Run an .Ipynb Jupyter Notebook from Terminal

How to run an .ipynb Jupyter Notebook from terminal?

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

How do I launch jupyter notebook from my terminal?

Jupyter Notebooks allow you to open IPYNB notebooks in the location you prefer. I generally recommend the following:

  1. First create a folder at your preferred destination
  2. Then go to terminal/cmd prompt and navigate to your above made destination
  3. Once in the destination then type in jupyter notebook

This will then automatically trigger the browser and open the Home tab, you can start a new notebook with your preferred kernel, rename and save the notebook, which it saves at the location where you navigated through the terminal

This helps you organize your relevant/related notebooks in the same place in a structured manner.

Example shown below:

enter image description here

How to run a Jupyter notebook .ipynb in python file .py?

You can convert the .ipynb file to a plain .py file using any of the methods mentioned here -

How do I convert a IPython Notebook into a Python file via commandline?

If, like me, you don't have access to the command line tools required - you can extract the source code from the .ipynb file as it is mostly a json file

import json
with open('somefile.ipynb') as f:
nb_content = json.load(f)

for cell in nb_content['cells']:
print(*cell['source'])

That should print out the whole source code to your terminal

Can I run Jupyter notebook cells in commandline?

You can use runipy to do this.

runipy will run all cells in a notebook. If an error occurs, the process will stop.

$ pip install runipy

$ runipy MyNotebook.ipynb

There are also commands for saving the output file as a notebook or an html report:

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

$ runipy MyNotebook.ipynb --html report.html



Related Topics



Leave a reply



Submit