Baalth
Baalth

Reputation: 23

Parsing a Jupyter Notebook file with code automatically to a .py file

Assume you have a .ipynb file with code cells and normal text for a proyect. I've been looking for ways to extract "code" cells directly to a .py file so i can mess with it and upload it on my standard format to Github.

I've tried parsing with awk, but due to the format having " in front of code lines and ASCII formatting, i'm out of clues. Are there any libraries, or ways to accomplish this without having to copypaste every code cell from the Notebook to my .py file? As i have one file for each proyect, feels dull to do this every time.

Thanks in advance!

Upvotes: 1

Views: 1587

Answers (1)

jfbeltran
jfbeltran

Reputation: 1818

You should be able to use the nbconvert command for both ipython and jupyter:

ipython nbconvert --to=python [YOUR_NOTEBOOK].ipynb

or

jupyter nbconvert --to=python [YOUR_NOTEBOOK].ipynb

If you're doing this for a lot of notebooks you can pass wildcard into the argument! The line below will convert every ipynb file in a folder to .py scripts!

jupyter nbconvert --to=python /SOME/PATH/my_notebooks/*.ipynb

Upvotes: 1

Related Questions