Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw code from a Jupyter notebook?

It would be really handy to have a button to copy raw code from a Jupyter notebook to the clipboard so it can be dumped into a text file, .py/.R script, or straight into the terminal (a button like github has to copy to clipboard)

Does a button (or even a chrome add on) exist?

Obviously you could copy/paste sections manually into a txt file, but that's laborious for long notebooks/markdowns

like image 885
stevec Avatar asked Mar 24 '18 00:03

stevec


3 Answers

Yes,there is a button:

File > Download as > Python (.py)

like image 200
Guillaume Avatar answered Oct 20 '22 23:10

Guillaume


There is a more handy way for Mac OS users.

By installing an Automator script converting the ipynb file to py first, then automatically copy the content to the clipboard with one right-click and select the script in the Quick Action part.

Github link

enter image description here

like image 25
JsW Avatar answered Oct 20 '22 22:10

JsW


You can use the magic command %%writefile to save as py file

%%writefile "directory/to/file/filename.py"

However, you should place this command at the top of the cell, otherwise it will throw an error.

like image 31
Natheer Alabsi Avatar answered Oct 20 '22 22:10

Natheer Alabsi