Python

How to convert Jupyter notebook to python

If you are working as a data analyst or data scientist, Jupyter Notebooks are great for performing experiments and building reports. During the experimentations, you need to turn your Jupyter notebook activities into the python code script or some other formats. In this case, the ‘nbconvert’ is the most popular tool through which you can easily convert your jupyter notebooks into the python script using the command line.

The nbconvert is an open-source software project that not only turns the Jupyter notebook into a python script; you can also generate familiar formats such as HTML, pdf, latex, and markdown. The documentation of ‘nbconvert’ enables you to display information in user-friendly formats like pdf that will help you to collaborate with others who may not use the notebook in their research. You can easily share content with many web users and publish research using Latex.

This guide will give you brief details about converting the Jupyter notebook to a python file.

Convert Jupyter Notebook (.ipynb) to Python (.py) using Terminal

You can convert one or multiple Jupyter notebooks using the nbconvert package. First, install the ‘nbconvert’ and ‘jupyter’ libraries. So, install these packages using the command prompt or terminal by typing the following commands:

> pip install jupyter

> pip install nbconvert

If these packages are already installed then, you can skip the above commands.

Syntax

You can use the ‘nbconvert’ library using the following syntax:

$ jupyter nbconvert --to OPTIONS FileName.ipynb

This will convert the jupyter notebook to different familiar formats that you specify in OPTIONS.

Convert a single Jupyter Notebook to python

If you want to convert your single notebook to an executable Python script (.py), you can easily do this task using the ‘nbconvert’ package. For example, we have created a file named ‘testnotebook.ipynb’, and we want to convert this file to python (.py). So, the following command helps you to convert from jupyter notebook (.ipynb) to python (.py).

> jupyter nbconvert testnotebook.ipynb --to python

C:\Users\DELL\OneDrive\Desktop\pythonProject1 – concat.py 2022-01-26 17.18.25.png

You can also use the ‘–to script’ that converts the jupyter notebook into an executable python or another language script depending on the kernel.

> jupyter nbconvert testnotebook.ipynb --to script

C:\Users\DELL\OneDrive\Desktop\python script.png

This command writes bytes or characters from a notebook (.ipynb) to a python file.

Convert multiple jupyter notebooks to python

You can also convert multiple notebooks using the ‘nbconvert’ library. For example, we have more than one notebook such as ‘testnotebook.ipynb’, ‘testnotebook1.ipynb’, and ‘testnotebook2.ipynb’ that we want to convert into python (.py). In this case, you can use the following command:

> jupyter nbconvert testnotebook.ipynb testnotebook1.ipynb testnotebook2.ipynb --to python

C:\Users\DELL\OneDrive\Desktop\pythonProject1 – concat.py 2022-01-26 17.22.29.png

This command will convert all notebooks to python (.py) files one by one by displaying file names.

Using the (*) asterisk symbol, you can also convert multiple notebooks:

> jupyter nbconvert testnotebook*.ipynb --to python

C:\Users\DELL\OneDrive\Desktop\pythonProject1 – concat.py 2022-01-26 17.20.56.png

This command converts all notebooks into python files.

You can convert multiple notebooks into executable python script files.

> jupyter nbconvert testnotebook*.ipynb --to script

C:\Users\DELL\OneDrive\Desktop\pythonProject1 – concat.py 2022-01-26 17.24.15.png

How to convert a Jupyter notebook to python using GUI?

The simplest way to convert the notebook (.ipynb) to python (.py) is using the Jupyter GUI environment. Open the jupyter notebook that you want to convert. Navigate into the ‘File’ menu and select ‘Download as’. The more options will be displayed in the form of a list where you will click on the ‘Python (.py)’ option.

C:\Users\DELL\OneDrive\Desktop\Untitled - Paint 2022-01-26 18.29.37.png

This file will download on your system.

Conclusion

We discussed how to convert a jupyter notebook (.ipynb) to python (.py). We can easily convert multiple notebooks to python or python scripts using the ‘nbconvert’ library. We have also seen how to download the notebook (.ipynb) with the python (.py) extension. You can also convert your jupyter notebook to other formats using the basic syntax.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she's working as a Freelancer & Technical writer. She's a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.