Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to save a notebook (like clicking the "disk" icon in the toolbar) via a code block. Reason: I'd like to use nbconvert via a %%bash magic to save HTML and slide versions, but that converts what's on disk, not what's running.

I'm not looking to do this every time I save (for which I see some config that can be set up), but only selectively for some of my notebooks. I'm looking to be able to "Run All" and have the last few cells create the additional files reflecting the fresh data on screen at the moment.

I've tried %save and %notebook magics, but neither does quite what I'm looking for (the best I can tell).

Is there any way to do this?

like image 290
kpmartin Avatar asked Jul 07 '17 02:07

kpmartin


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 I save an 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.

How do I export a Jupyter notebook to a Python script?

Open the jupyter notebook that you want to convert. Navigate into the 'File' menu and select 'Download as'. The more options will be displayed in the form of a list where you will click on the 'Python (. py)' option.

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.


1 Answers

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.

like image 54
Louise Davies Avatar answered Nov 15 '22 11:11

Louise Davies