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!
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.
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.
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.
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.
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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With