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 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:
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).
You can also use the ‘–to script’ that converts the jupyter notebook into an executable python or another language script depending on the kernel.
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:
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:
This command converts all notebooks into python files.
You can convert multiple notebooks into executable python script files.
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.
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.