Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save an IPython notebook programmatically from within itself?

I would like to leave an IPython notebook running to do some computation + show some visuals.

Once the IPython notebook has finished, I want the last cell in the IPython notebook to programmatically save the IPython notebook. Then I want to copy the notebook (with all output) to another directory to keep a record of results.

The copying bit I can code up easily, but I am not sure how to get an IPython notebook to programatically save itself? Is this possible? Thanks in advance!

like image 334
applecider Avatar asked Aug 26 '15 21:08

applecider


People also ask

How do I save my Jupyter Notebook automatically?

You can use this Jupyter extension By default, a Jupyter Notebook saves your work every 2 minutes, and if you want to change this time interval you can do so by using the %autosave n magic command; where n is the number of seconds, and if n=0 this means no autosaving.

How do you save in IPython notebook?

Saving your edits is simple. There is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved. It's important to realize that you will only be saving edits you've made to the text sections and to the coding windows.

How do you save a Jupyter Notebook with code and output?

Double-check the settings at "Settings" -> "Site" and make sure "New notebooks use private outputs (omit outputs when saving)" is disabled. Similarly, check also "Edit" -> "Notebook settings" and make sure "Omit code cell output when saving this notebook" is disabled.

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


2 Answers

I am taking @Taar's comment and making it an actual answer since it worked for the original person who asked the question and for myself.

from IPython.display import display, Javascript
display(Javascript('IPython.notebook.save_checkpoint();'))

This will create checkpoints - same thing as CTRL-s.

Note: in Jupyter, CTRL-s triggers an async process and the file save is actually completed only a few seconds later. If you want a blocking save operation in a notebook, use this little function (file_path is the path to the notebook file):

import time
from IPython.display import display, Javascript
import hashlib

def save_notebook(file_path):
    start_md5 = hashlib.md5(open(file_path,'rb').read()).hexdigest()
    display(Javascript('IPython.notebook.save_checkpoint();'))
    current_md5 = start_md5
    
    while start_md5 == current_md5:
        time.sleep(1)
        current_md5 = hashlib.md5(open(file_path,'rb').read()).hexdigest()
like image 144
Raphvanns Avatar answered Oct 11 '22 20:10

Raphvanns


The ipython magic command %notebook will help you here. It is shown on this page (search for %notebook).

To save your current notebook history to the file "foo.ipynb" just enter:

%notebook -e foo.ipynb

At the point you want it to happen

like image 5
or1426 Avatar answered Oct 11 '22 21:10

or1426