Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save by cell and not by line #: IPython %save magic: Is there a way?

I'm creating a Django tutorial in an IPython Notebook and I want to use the %save magic to save .py files to create / edit / advance a website as the tutorial progresses. The problem is the %save magic works by specifying which lines to save. Undoubtedly, the line number will change when users execute the cells before the file out of order or multiple times.

I'd like to work like this:

some .py file imported into the notebook.
# do all sorts of website things
%save -f this.py file

where it would save the contents of the cell without needing the line number. Is there any way to do this with the existing functionality of the %save magic?

like image 649
agconti Avatar asked Dec 27 '22 02:12

agconti


1 Answers

After talking with the IPython dev team minrk found an answer:

%%writefile filename.py

will write everything below it in the cell to filename.py. link to the converastion.

like image 196
agconti Avatar answered Dec 28 '22 16:12

agconti