5

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?

agconti
  • 17,780
  • 15
  • 80
  • 114

1 Answers1

11

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.

agconti
  • 17,780
  • 15
  • 80
  • 114
  • 3
    Also don't forget to add `--append` or `-a`, if you want to append multiple cells to the same file. In this case, you can just add `%%writefile filename.py -a` at the top of every cell that you want like to add to the file. For more info, type `%%writefile?` – Aziz Alto Sep 15 '14 at 20:57