Convert .ipynb to .py

Jupyter notebook automation
15 Aug 2020

Convert .ipynb to .py

Posted By

Automating python jupyter notebooks (.ipynb) are essential when it come to create a time based executions for your notebooks. Let us see how to convert .ipynb to .py

The Jupyter Notebook is an open source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. Jupyter Notebook is maintained by the people at Project Jupyter

Especially when it comes to automating data collection which is a period like daily, weekly, monthly or time-based jobs it comes very important to automate the notebooks.

how to convert .ipynb to .py

To automate the notebooks , first the notebooks should be converted to python and use scheduler ( e.g Windows Scheduler for local or cloud scheduling solution link seek well ) to schedule job execution.

There are primarily two major methods to convert python notebooks

Method 1 : Using Ipython

IPython is an alternative Python interpreter. It is an interactive shell used for computing in Python. It provides many more useful features over the more popular default Python interpreter

Install ipython libraries (Of course, you can skip if already installed.)

> pip install ipython

> pip install nbconvert

Convert single file

> ipython nbconvert — to script abc.ipynb

You can have

Convert multi files

> ipython nbconvert — to script abc.ipynb def.ipynb,

> ipython nbconvert — to script *.ipynb,

Method 2 : Using jupyter

The nbconvert tool, jupyter nbconvert, converts notebooks to various other formats via Jinja templates. The nbconvert tool allows you to convert an .ipynb notebook file into various static formats as below

  • HTML
  • LaTeX
  • PDF
  • Reveal JS
  • Markdown (md)
  • ReStructured Text (rst)
  • executable script

This method is recommended as using ‘ipython’ is to deprecated

> jupyter nbconvert — to script ‘my-notebook.ipynb’

Leave a Reply