How can I save a Jupyter Notebook (iPython notebook) programmatically?

A hacky way to do it is using the %%javascript notebook magic to run the command

require(["base/js/namespace"],function(Jupyter) {
    Jupyter.notebook.save_checkpoint();
});

This accesses the global notebook instance (when on the notebook page this is the current notebook) and manually triggers a save and checkpoint.


Here is a reusable version of @louise-davies answer in the form of a python function;

from IPython.display import Javascript

script = '''
require(["base/js/namespace"],function(Jupyter) {
    Jupyter.notebook.save_checkpoint();
});
'''

def notebook_save():
    Javascript(script)
    print('This notebook has been saved')

UPDATE:

I think the previous solution was broken but recent updates to jupyter notebook. It appears that Javascript cannot be invoked inside of a function. However it still works if called outside the context of a function.

from IPython.display import Javascript

script = '''
require(["base/js/namespace"],function(Jupyter) {
    Jupyter.notebook.save_checkpoint();
});
'''
Javascript(script)