★ wanayoo — archive 1999 https://github.com/ipython/ipython/pull/3158/Nouvelle recherche | Portail wanayoo
Skip to content
This repository

IPEP 15: autosave the notebook #3158

Merged
merged 24 commits into from about 1 year ago

4 participants

Min RK drevicko Brian E. Granger Fernando Perez
Min RK
Owner
minrk commented

See IPEP 15 for details of the proposal.

This is not quite complete, the most glaring omission is, of course, the autosave timer itself, but that's pretty trivial. The checkpointing machinery is fairly complete, and ready to discuss / review.

The gist: autosave will do a real save periodically. To address the undesirable effects of true autosave, user-initiated saves create checkpoints, which can be restored. In this preliminary implementation, the file-based notebook manager only supports one checkpoint.

Min RK
Owner
minrk commented

Okay, this actually autosaves now. The timer follows the following logic:

  • autosave will never happen more frequently than every 30 seconds
  • if saves take more than 3 seconds, autosave will fire every 10x the duration of a save (i.e. if save takes 6 seconds, it will be every 60 seconds, etc.)
Min RK
Owner
minrk commented

For linking, this will close #1378 and #3153.

drevicko

I think I get the logic behind firing every 10x the duration of the save (if it takes more than 3s), but feel it could do with a moments discussion... It might be good to put an upper limit on the time between saves - perhaps 5 minutes??

If for some bizarre reason, a particular save takes, say, 20s, then it'll be 3 mins 20secs before the next one. This can happen for example when the users client computer has a process that temporarily fills up it's memory, or perhaps the connection gets clogged by a flatmate downloading something huge or a cafe enforcing a rate limit.

In such cases, it makes sense to reduce the load of autosaves on resources. However the flipside is that the user may be surprised that the notebook wasn't saved. The 20s -> 3mins20secs is perhaps ok, but what if a save takes a reallly long time, say 3 minutes?

The situations in which I can imagine a save taking a long time:

  • very laggy connection
  • huge notebook
  • very laggy client computer
  • very laggy server
Min RK
Owner
minrk commented

It's a tricky choice. I would say that it's the right decision in an extremely low bandwidth case to have quite infrequent saves - even as infrequent as half an hour, if it really does take three minutes (most likely on a weak cellular connection or similar). The user is informed when the autosave frequency changes, so they will see "autosaving every 1800s" in the notification area if a save takes three minutes, and the 'last saved' area still has the last saved time stamp, etc.

drevicko

Yep, fair enough. And the user can always elect to manually save if they see that auto-saves are far apart. Does it detect how long the most recent save took? Or is it the most recent auto-save? My thinking is a situation where a save is unusually slow for a temporary reason - if the user does a manual save a little later that is quick, perhaps the autosave machinery should notice.

As a matter of interest, is the notebook sent to the server as some sort of diff or as full text? I'm guessing full text. (I guess I'm thinking of google docs again - I believe they have a clever engine for that - I wonder if it can be borrowed?)

Brian E. Granger
Owner

Right now we send the entire notebook each time. In the future we plan on sending just the diffs, but that is going to take quite a bit more work.

IPython/frontend/html/notebook/notebookapp.py
... ...
@@ -104,7 +105,11 @@
104 105
 _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
105 106
 _kernel_action_regex = r"(?P<action>restart|interrupt)"
106 107
 _notebook_id_regex = r"(?P<notebook_id>\w+-\w+-\w+-\w+-\w+)"
  108
+<<<<<<< HEAD
2
Brian E. Granger Owner
ellisonbg added a note

Opps...

Brian E. Granger Owner
ellisonbg added a note

Conflict markers got committed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/notebookapp.py
... ...
@@ -105,6 +106,7 @@
105 106
 _kernel_action_regex = r"(?P<action>restart|interrupt)"
106 107
 _notebook_id_regex = r"(?P<notebook_id>\w+-\w+-\w+-\w+-\w+)"
107 108
 _notebook_name_regex = r"(?P<notebook_name>.+\.ipynb)"
  109
+_checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
2
Brian E. Granger Owner
ellisonbg added a note

Will this regular expression cover other backends like git sufficiently?

Min RK Owner
minrk added a note

I think so, I'll have to make sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/html/notebook/handlers.py
((6 lines not shown))
  689
+    SUPPORTED_METHODS = ('GET', 'POST')
  690
+    
  691
+    @web.authenticated
  692
+    def get(self, notebook_id):
  693
+        """get lists checkpoints for a notebook"""
  694
+        nbm = self.application.notebook_manager
  695
+        checkpoints = nbm.list_checkpoints(notebook_id)
  696
+        data = jsonapi.dumps(checkpoints, default=date_default)
  697
+        self.finish(data)
  698
+    
  699
+    @web.authenticated
  700
+    def post(self, notebook_id):
  701
+        """post creates a new checkpoint"""
  702
+        nbm = self.application.notebook_manager
  703
+        checkpoint = nbm.create_checkpoint(notebook_id)
  704
+        data = jsonapi.dumps(checkpoint, default=date_default)
1
Brian E. Granger Owner
ellisonbg added a note

Before returning this method should set the Location header to point the the URL of the newly created checkpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/filenbmanager.py
((12 lines not shown))
  55
+    
  56
+    def _checkpoint_dir_changed(self, name, old, new):
  57
+        """do a bit of validation of the checkpoint dir"""
  58
+        if not os.path.isabs(new):
  59
+            # If we receive a non-absolute path, make it absolute.
  60
+            abs_new = os.path.abspath(new)
  61
+            self.checkpoint_dir = abs_new
  62
+            return
  63
+        if os.path.exists(new) and not os.path.isdir(new):
  64
+            raise TraitError("checkpoint dir %r is not a directory" % new)
  65
+        if not os.path.exists(new):
  66
+            self.log.info("Creating checkpoint dir %s", new)
  67
+            try:
  68
+                os.mkdir(new)
  69
+            except:
  70
+                raise TraitError("Couldn't create checkpoint dir %r" % new)
2
Brian E. Granger Owner
ellisonbg added a note

Is TraitError the right thing to raise for errors in this method? Do we do this elsewhere rather than just letting it raise the underlying exception?

Min RK Owner
minrk added a note

This is copied verbatim from notebook-dir, we raise TraitError when trait values are invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/filenbmanager.py
((25 lines not shown))
  279
+        stats = os.stat(path)
  280
+        last_modified = datetime.datetime.utcfromtimestamp(stats.st_mtime)
  281
+        info = dict(
  282
+            checkpoint_id = checkpoint_id,
  283
+            last_modified = last_modified,
  284
+        )
  285
+        
  286
+        return info
  287
+        
  288
+    # public checkpoint API
  289
+    
  290
+    def create_checkpoint(self, notebook_id):
  291
+        """Create a checkpoint from the current state of a notebook"""
  292
+        nb_path = self.get_path(notebook_id)
  293
+        # only the one checkpoint ID:
  294
+        checkpoint_id = "checkpoint"
1
Brian E. Granger Owner
ellisonbg added a note

I actually think we should fully implement multiple checkpoints on the server side with real checkpoint uuids. For now, I think it is reasonable if the frontend can only restore to the previous checkpoint, but there is no reason to not do the full thing on the server. I think this will allow us to better test out the ideas that will lead up to a git backed checkpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/filenbmanager.py
((46 lines not shown))
194 300
         
  301
+        # return the checkpoint info
  302
+        return self.get_checkpoint_info(notebook_id, checkpoint_id)
  303
+    
  304
+    def list_checkpoints(self, notebook_id):
  305
+        """list the checkpoints for a given notebook
  306
+        
  307
+        This notebook manager currently only supports one checkpoint per notebook.
  308
+        """
  309
+        checkpoint_id = "checkpoint"
2
Brian E. Granger Owner
ellisonbg added a note

Let's implement multiple checkpoints here as wel. Part of the reason I want us to do this now is it will help us think through how we will track the ordering of the checkpoints. Raw uuids are not ordered. We need to figure out a good way of overlaying an ordering on the checkpoints that is preserved across server restarts (IOW we can't just use an in memory ordered list of uuids).

Brian E. Granger Owner
ellisonbg added a note

Also, this ordering question is another area where we have to think about how this relates to git integration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/kernel/zmq/zmqshell.py
... ...
@@ -444,6 +445,29 @@ def qtconsole(self, arg_s):
444 445
         except Exception as e:
445 446
             error("Could not start qtconsole: %r" % e)
446 447
             return
  448
+    
  449
+    @line_magic
  450
+    def autosave(self, arg_s):
2
Brian E. Granger Owner
ellisonbg added a note

Very clever! This actually makes me think that we could use this approach to allow the JS side of things to be configured through Python side magics. I love it!

Min RK Owner
minrk added a note

glad you like it - I've been using this magic from my extensions repo for several months.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger
Owner

Done reviewing the server side of things. Overall the code is really clean and nice. I really like this design and its server side implementation a lot. Great work! Now onto the frontend side...

Brian E. Granger
Owner

The save button in the main toolbar still has a hover help text of "Save". This should be "Checkpoint" and maybe we should change the icon?

Brian E. Granger
Owner

Ahh, I see that "Save" still exists in the UI. I think this will confuse users. What do you think about only having "Checkpoint" be visible to the users. IOW, make save an internal detail...?

Brian E. Granger
Owner

Hmm, I am not seeing the behavior I was expecting in the UI. Not sure if it is broken or I just had false expectations of how it would work.

IPython/kernel/zmq/zmqshell.py
... ...
@@ -444,6 +445,29 @@ def qtconsole(self, arg_s):
444 445
         except Exception as e:
445 446
             error("Could not start qtconsole: %r" % e)
446 447
             return
  448
+    
  449
+    @line_magic
  450
+    def autosave(self, arg_s):
  451
+        """Set the lower-limit for the autosave frequency in the notebook (in seconds).
3
Brian E. Granger Owner
ellisonbg added a note

Do you mean upper limit here?

Brian E. Granger Owner
ellisonbg added a note

I actually think we should say "Set the autosave interval in the notebook (in seconds)." here.

Min RK Owner
minrk added a note

done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/kernel/zmq/zmqshell.py
((5 lines not shown))
  449
+    @line_magic
  450
+    def autosave(self, arg_s):
  451
+        """Set the lower-limit for the autosave frequency in the notebook (in seconds).
  452
+        
  453
+        The default value is 120, or two minutes.
  454
+        ``%autosave 0`` will disable autosave.
  455
+        """
  456
+        
  457
+        try:
  458
+            interval = int(arg_s)
  459
+        except ValueError:
  460
+            raise UsageError("%%autosave requires an integer, got %r" % arg_s)
  461
+        
  462
+        # javascript wants milliseconds
  463
+        milliseconds = 1000 * interval
  464
+        display(Javascript("IPython.notebook.autosave_notebook(%i)" % milliseconds),
1
Brian E. Granger Owner
ellisonbg added a note

Looks like the autosave_notebook method no longer exists...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

In case anyone really cares, we had an extensive discussion of this during a public Google hangout. We'll do more of those, open to anyone for participation, for issues that require high-bandwidth communication.

Brian E. Granger
Owner

Right now, the user invoked actions in the UI are for "Save and Checkpoint". But the "Last saved" date/time in the page header is for the last autosaved time. This is a bit confusing. In some cases, I think that users will want to see the time of last checkpoint instead. Thoughts?

Min RK
Owner
minrk commented

I can change it to the last checkpoint easily enough. Do we want to keep UI for the last autosave as well?

Min RK
Owner
minrk commented

Also, do we want a visual indication of the 'dirty' flag?

Brian E. Granger
Owner

While Google Docs uses a very different model of autosave than we have here, I feel like their messages about the save status are quite useful. You quickly see the save status of the document. I do think it would be useful to tell the user that there are unsaved changes, and tell them when the autosave has been done. That will put peoples mind at ease. Nto sure how to present all of that information though.

Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/static/js/notebook.js
((92 lines not shown))
  1889
+     * @param {jqXHR} xhr jQuery Ajax object
  1890
+     * @param {String} status Description of response status
  1891
+     * @param {String} error_msg HTTP error message
  1892
+     */
  1893
+    Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
  1894
+        $([IPython.events]).trigger('checkpoint_failed.Notebook');
  1895
+    };
  1896
+    
  1897
+    Notebook.prototype.restore_checkpoint_dialog = function () {
  1898
+        var that = this;
  1899
+        var checkpoint = this.last_checkpoint;
  1900
+        if ( ! checkpoint ) {
  1901
+            console.log("restore dialog, but no checkpoint to restore to!");
  1902
+            return;
  1903
+        }
  1904
+        var dialog = $('<div/>').append(
1
Brian E. Granger Owner
ellisonbg added a note

I think I would make these different paragraphs a single one, or put spaces between them. Otherwise it just looks like poorly justified text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/html/notebook/filenbmanager.py
((6 lines not shown))
139 177
         path = self.get_path_by_name(new_name)
140 178
         try:
  179
+            self.log.debug("Writing notebook %s", path)
1
Brian E. Granger Owner
ellisonbg added a note

Let's make sure to use the "official" language in all of the log.debug messages: "Autosave" and "Checkpoint", rather than "Writing" which is ambiguous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/html/notebook/filenbmanager.py
((20 lines not shown))
163 213
                     os.unlink(old_pypath)
164  
-            self.mapping[notebook_id] = new_name
165  
-            self.rev_mapping[new_name] = notebook_id
166  
-            del self.rev_mapping[old_name]
167  
-        
  214
+            
  215
+            # rename checkpoints to follow file
  216
+            self.log.debug("%s", old_checkpoints)
2
Brian E. Granger Owner
ellisonbg added a note

Maybe a more specific log message here...

Min RK Owner
minrk added a note

just a debug message here, removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
IPython/frontend/html/notebook/static/js/menubar.js
((14 lines not shown))
224 236
     };
225 237
 
  238
+    MenuBar.prototype.update_restore_checkpoint = function(checkpoint) {
  239
+        if (!checkpoint) {
  240
+            this.element.find("#restore_checkpoint")
  241
+                .addClass('ui-state-disabled')
  242
+                .off('click')
  243
+                .find('a').text("Revert");
  244
+            return;
  245
+        };
  246
+        var d = new Date(checkpoint.last_modified);
  247
+        this.element.find("#restore_checkpoint")
  248
+            .removeClass('ui-state-disabled')
  249
+            .off('click')
  250
+            .click(function () {
  251
+                IPython.notebook.restore_checkpoint_dialog();
  252
+            }).find('a').html("Revert to: <br/>" + d.format("mmm dd HH:MM:ss"));
2
Brian E. Granger Owner
ellisonbg added a note

Any clever ideas on how to better display the "Revert to: date" without the awkward line break?

Min RK Owner
minrk added a note

only options are much wider menu or submenu - a submenu is how we will have to do it for multiple checkpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/static/js/notebook.js
... ...
@@ -39,6 +39,12 @@ var IPython = (function (IPython) {
39 39
         this.paste_enabled = false;
40 40
         this.dirty = false;
41 41
         this.metadata = {};
  42
+        this._checkpoint_after_save = false;
1
Brian E. Granger Owner
ellisonbg added a note

Can we put the checkpoint config in the savewidget. I have thought about eventually making that the reference object that talks to the notebook/checkpoint web services. Would be nice to have it decoupled from notebook.js entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/static/js/notebook.js
... ...
@@ -1550,6 +1557,33 @@ var IPython = (function (IPython) {
1550 1557
     };
1551 1558
 
1552 1559
     /**
  1560
+     * Start an autosave timer, for periodically saving the notebook.
  1561
+     * 
  1562
+     * @method set_autosave_interval
  1563
+     * @param {Integer} interval the autosave interval in milliseconds
  1564
+     */
  1565
+    Notebook.prototype.set_autosave_interval = function (interval) {
1
Brian E. Granger Owner
ellisonbg added a note

Would it make sense to move this method to the savewidget as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger ellisonbg commented on the diff
IPython/frontend/html/notebook/static/js/notebook.js
((33 lines not shown))
1589 1633
     };
1590  
-
  1634
+    
  1635
+    /**
  1636
+     * update the autosave interval based on how long the last save took
  1637
+     * 
  1638
+     * @method _update_autosave_interval
  1639
+     * @param {Integer} timestamp when the save request started
  1640
+     */
  1641
+    Notebook.prototype._update_autosave_interval = function (start) {
1
Brian E. Granger Owner
ellisonbg added a note

Move to savewidget?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian E. Granger
Owner

OK I have a few inline comment that need to be addressed. The biggest thing on the JS side of the code is that we are slowly getting more save/checkpoint related code spreadout over the notebook object and the savewidget. I would prefer to have a single object that wraps the notebooks/checkpoints web services and provides a clean JS API for talking to them that notebook (and other apps) can use. I think the name "savewidget" is the wrong name for this object, but maybe lets see if we can clean this up a bit...

Brian E. Granger
Owner

Deleting a notebook doesn't remove its checkpoint. Is this deliberate?

Brian E. Granger
Owner

Here is a screenshot of the current information this PR provides about the autosave and checkpoint times:

screen shot 2013-05-01 at 2 16 52 pm

This provides the information we want (I like that part of it), but I am not sure about the presentation. Maybe a slightly more symmetric presentation:

Checkpointed: mmm dd HH:MM | Autosaved: mmm dd HH:MM

Brian E. Granger
Owner

I see that rename does an autosave+checkpoint. I was initially surprised by that. But I guess a rename conceptually implies a checkpoint...

IPython/frontend/html/notebook/filenbmanager.py
((44 lines not shown))
175 232
         os.unlink(path)
  233
+        
  234
+        # clear checkpoints
2
Brian E. Granger Owner
ellisonbg added a note

I don't think this logic is right - doesn't seem to delete the checkpoints when deleting a notebook.

Min RK Owner
minrk added a note

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Min RK minrk closed this
Min RK
Owner
minrk commented

Deleting a notebook doesn't remove its checkpoint. Is this deliberate?

Nope, just a bug.

Min RK minrk reopened this
Min RK
Owner
minrk commented

I see that rename does an autosave+checkpoint. I was initially surprised by that. But I guess a rename conceptually implies a checkpoint...

No, it shouldn't. Fixed.

minrk added some commits
Min RK minrk add checkpoint API to FileNBManager 076f226
Min RK minrk add notebook checkpoint handler b68ea6d
Min RK minrk add ModifyCheckpoints handler …
separates requests that change specified checkpoint IDs from those that do not.
c8b6955
Min RK minrk expose notebook checkpoints in html/js …
- cmd-S does a plain save
- ^M-s does checkpoint
- add save checkpoint to File menu
- save button in bar does checkpoint
bc87e4c
Min RK minrk checkpoint info is a dict …
two keys: checkpoint_id and last_modified
80b5d18
Min RK minrk add some checkpoint messages to the notification area 92341f3
Min RK minrk add Revert to the menu bar 609c814
Min RK minrk trigger autosave on tab/window close …
instead of "please don't leave!"
5ce631b
Min RK minrk add autosave timer …
autosave interval is tuned based on the duration of saves.
Autosave will never happen more frequently than every 30 seconds,
and if saves take more than 3 seconds, autosave will fire every 10x the duration of the save (i.e. if save takes 6 seconds, it will be every 60 seconds, etc.)
757bf96
Min RK minrk only autosave when dirty 7a392f6
Min RK minrk set default autosave interval to two minutes 511bb2f
Min RK minrk s/autosave_notebook/set_autosave_interval/ a188c64
Min RK minrk *all* user-initiated saves create a checkpoint 65bc9e8
Min RK minrk add %autosave magic from autosave extension eb50507
Min RK minrk only save on close if autosaving 27b173a
Min RK minrk sync with previous handler changes …
use IPythonHandler, avoid self.application
47e762b
Min RK minrk Save and Checkpoint 156594a
Min RK minrk ignore checkpoints 4c11197
Min RK minrk fix Location headers ba358e3
Min RK minrk add checkpoint status to notebook header …
autosave is also there, but once the dirty flag is an event,
this should stop having the date, and just become 'autosaved / unsaved changes'
7cababc
Min RK minrk autosave docstring a38e0d4
Min RK minrk deleting a notebook deletes its checkpoints …
also cleanup a few log messages
d4fedf6
Min RK minrk rename shouldn't create a checkpoint c801089
Min RK minrk restore checkpoints in a sub-list …
minor styling update to the restore dialog as well
0c16365
Fernando Perez
Owner
fperez commented

My only concern at this point is purely cosmetic: with so much infromation printed about the checkpoint/save times, it doesn't take a very long notebook title before all that rolls over in a pretty ugly way:

image

But improving this would really require rethinking the layout of the UI deeper. I think we'll need to do that, but I don't think it's wise to hold this important and complex PR any further just on that. Thoughts?

Min RK
Owner
minrk commented

It will be shorter when the dirty event is introduced, at which point the 'autosaved: DATE' will simply be autosaved or unsaved changes, depending on whether the current view of the file is in sync with the autosaved version, but not short enough to really alleviate the problem. It won't solve the real issue of the fact that the save widget as it is now is just much too wide (and this PR makes it wider). I think revisiting how the save widget looks is definitely something we have to do, but I think it can be separate.

Fernando Perez
Owner
fperez commented

Sounds good. Merging now, then! This was a long and painful one, kudos for a fantastic job, and I'm glad we suffered through the long and detailed discussion period. While in the end the chosen implementation isn't anything fancy or complex, I like that we took the time to dig at length into all the caveats and possible problems. Thanks!!

Fernando Perez fperez merged commit b69eb67 into from
Fernando Perez fperez closed this
Min RK minrk deleted the branch
Min RK minrk restored the branch
Takafumi Arakaki tkf referenced this pull request in tkf/emacs-ipython-notebook
Open

Implement autosave (aka IPEP 15) #116

Min RK minrk deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 24 unique commits by 1 author.

May 02, 2013
Min RK minrk add checkpoint API to FileNBManager 076f226
Min RK minrk add notebook checkpoint handler b68ea6d
Min RK minrk add ModifyCheckpoints handler …
separates requests that change specified checkpoint IDs from those that do not.
c8b6955
Min RK minrk expose notebook checkpoints in html/js …
- cmd-S does a plain save
- ^M-s does checkpoint
- add save checkpoint to File menu
- save button in bar does checkpoint
bc87e4c
Min RK minrk checkpoint info is a dict …
two keys: checkpoint_id and last_modified
80b5d18
Min RK minrk add some checkpoint messages to the notification area 92341f3
Min RK minrk add Revert to the menu bar 609c814
Min RK minrk trigger autosave on tab/window close …
instead of "please don't leave!"
5ce631b
Min RK minrk add autosave timer …
autosave interval is tuned based on the duration of saves.
Autosave will never happen more frequently than every 30 seconds,
and if saves take more than 3 seconds, autosave will fire every 10x the duration of the save (i.e. if save takes 6 seconds, it will be every 60 seconds, etc.)
757bf96
Min RK minrk only autosave when dirty 7a392f6
Min RK minrk set default autosave interval to two minutes 511bb2f
Min RK minrk s/autosave_notebook/set_autosave_interval/ a188c64
Min RK minrk *all* user-initiated saves create a checkpoint 65bc9e8
Min RK minrk add %autosave magic from autosave extension eb50507
Min RK minrk only save on close if autosaving 27b173a
Min RK minrk sync with previous handler changes …
use IPythonHandler, avoid self.application
47e762b
Min RK minrk Save and Checkpoint 156594a
Min RK minrk ignore checkpoints 4c11197
Min RK minrk fix Location headers ba358e3
Min RK minrk add checkpoint status to notebook header …
autosave is also there, but once the dirty flag is an event,
this should stop having the date, and just become 'autosaved / unsaved changes'
7cababc
Min RK minrk autosave docstring a38e0d4
Min RK minrk deleting a notebook deletes its checkpoints …
also cleanup a few log messages
d4fedf6
Min RK minrk rename shouldn't create a checkpoint c801089
Min RK minrk restore checkpoints in a sub-list …
minor styling update to the restore dialog as well
0c16365
This page is out of date. Refresh to see the latest.
1  .gitignore
@@ -12,4 +12,5 @@ build
12 12
 *.egg-info
13 13
 *~
14 14
 *.bak
  15
+.ipynb_checkpoints
15 16
 .tox
192 IPython/frontend/html/notebook/filenbmanager.py
@@ -20,6 +20,7 @@
20 20
 import io
21 21
 import os
22 22
 import glob
  23
+import shutil
23 24
 
24 25
 from tornado import web
25 26
 
@@ -43,11 +44,36 @@ class FileNotebookManager(NotebookManager):
43 44
         """
44 45
     )
45 46
     
  47
+    checkpoint_dir = Unicode(config=True,
  48
+        help="""The location in which to keep notebook checkpoints
  49
+        
  50
+        By default, it is notebook-dir/.ipynb_checkpoints
  51
+        """
  52
+    )
  53
+    def _checkpoint_dir_default(self):
  54
+        return os.path.join(self.notebook_dir, '.ipynb_checkpoints')
  55
+    
  56
+    def _checkpoint_dir_changed(self, name, old, new):
  57
+        """do a bit of validation of the checkpoint dir"""
  58
+        if not os.path.isabs(new):
  59
+            # If we receive a non-absolute path, make it absolute.
  60
+            abs_new = os.path.abspath(new)
  61
+            self.checkpoint_dir = abs_new
  62
+            return
  63
+        if os.path.exists(new) and not os.path.isdir(new):
  64
+            raise TraitError("checkpoint dir %r is not a directory" % new)
  65
+        if not os.path.exists(new):
  66
+            self.log.info("Creating checkpoint dir %s", new)
  67
+            try:
  68
+                os.mkdir(new)
  69
+            except:
  70
+                raise TraitError("Couldn't create checkpoint dir %r" % new)
  71
+    
46 72
     filename_ext = Unicode(u'.ipynb')
47 73
 
48 74
     # Map notebook names to notebook_ids
49 75
     rev_mapping = Dict()
50  
-
  76
+    
51 77
     def get_notebook_names(self):
52 78
         """List all notebook names in the notebook dir."""
53 79
         names = glob.glob(os.path.join(self.notebook_dir,
@@ -89,26 +115,28 @@ def notebook_exists(self, notebook_id):
89 115
             return False
90 116
         path = self.get_path_by_name(self.mapping[notebook_id])
91 117
         return os.path.isfile(path)
92  
-
93  
-    def find_path(self, notebook_id):
94  
-        """Return a full path to a notebook given its notebook_id."""
  118
+    
  119
+    def get_name(self, notebook_id):
  120
+        """get a notebook name, raising 404 if not found"""
95 121
         try:
96 122
             name = self.mapping[notebook_id]
97 123
         except KeyError:
98 124
             raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
  125
+        return name
  126
+
  127
+    def get_path(self, notebook_id):
  128
+        """Return a full path to a notebook given its notebook_id."""
  129
+        name = self.get_name(notebook_id)
99 130
         return self.get_path_by_name(name)
100 131
 
101 132
     def get_path_by_name(self, name):
102 133
         """Return a full path to a notebook given its name."""
103 134
         filename = name + self.filename_ext
104 135
         path = os.path.join(self.notebook_dir, filename)
105  
-        return path       
  136
+        return path
106 137
 
107  
-    def read_notebook_object(self, notebook_id):
108  
-        """Get the NotebookNode representation of a notebook by notebook_id."""
109  
-        path = self.find_path(notebook_id)
110  
-        if not os.path.isfile(path):
111  
-            raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
  138
+    def read_notebook_object_from_path(self, path):
  139
+        """read a notebook object from a path"""
112 140
         info = os.stat(path)
113 141
         last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
114 142
         with open(path,'r') as f:
@@ -116,12 +144,20 @@ def read_notebook_object(self, notebook_id):
116 144
             try:
117 145
                 # v1 and v2 and json in the .ipynb files.
118 146
                 nb = current.reads(s, u'json')
119  
-            except:
120  
-                raise web.HTTPError(500, u'Unreadable JSON notebook.')
  147
+            except Exception as e:
  148
+                raise web.HTTPError(500, u'Unreadable JSON notebook: %s' % e)
  149
+        return last_modified, nb
  150
+    
  151
+    def read_notebook_object(self, notebook_id):
  152
+        """Get the Notebook representation of a notebook by notebook_id."""
  153
+        path = self.get_path(notebook_id)
  154
+        if not os.path.isfile(path):
  155
+            raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
  156
+        last_modified, nb = self.read_notebook_object_from_path(path)
121 157
         # Always use the filename as the notebook name.
122 158
         nb.metadata.name = os.path.splitext(os.path.basename(path))[0]
123 159
         return last_modified, nb
124  
-
  160
+    
125 161
     def write_notebook_object(self, nb, notebook_id=None):
126 162
         """Save an existing notebook object by notebook_id."""
127 163
         try:
@@ -136,16 +172,20 @@ def write_notebook_object(self, nb, notebook_id=None):
136 172
             raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
137 173
 
138 174
         old_name = self.mapping[notebook_id]
  175
+        old_checkpoints = self.list_checkpoints(notebook_id)
  176
+        
139 177
         path = self.get_path_by_name(new_name)
140 178
         try:
  179
+            self.log.debug("Autosaving notebook %s", path)
141 180
             with open(path,'w') as f:
142 181
                 current.write(nb, f, u'json')
143 182
         except Exception as e:
144  
-            raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)
  183
+            raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s' % e)
145 184
 
146 185
         # save .py script as well
147 186
         if self.save_script:
148 187
             pypath = os.path.splitext(path)[0] + '.py'
  188
+            self.log.debug("Writing script %s", pypath)
149 189
             try:
150 190
                 with io.open(pypath,'w', encoding='utf-8') as f:
151 191
                     current.write(nb, f, u'py')
@@ -154,25 +194,52 @@ def write_notebook_object(self, nb, notebook_id=None):
154 194
         
155 195
         # remove old files if the name changed
156 196
         if old_name != new_name:
  197
+            # update mapping
  198
+            self.mapping[notebook_id] = new_name
  199
+            self.rev_mapping[new_name] = notebook_id
  200
+            del self.rev_mapping[old_name]
  201
+            
  202
+            # remove renamed original, if it exists
157 203
             old_path = self.get_path_by_name(old_name)
158 204
             if os.path.isfile(old_path):
  205
+                self.log.debug("unlinking notebook %s", old_path)
159 206
                 os.unlink(old_path)
  207
+            
  208
+            # cleanup old script, if it exists
160 209
             if self.save_script:
161 210
                 old_pypath = os.path.splitext(old_path)[0] + '.py'
162 211
                 if os.path.isfile(old_pypath):
  212
+                    self.log.debug("unlinking script %s", old_pypath)
163 213
                     os.unlink(old_pypath)
164  
-            self.mapping[notebook_id] = new_name
165  
-            self.rev_mapping[new_name] = notebook_id
166  
-            del self.rev_mapping[old_name]
167  
-        
  214
+            
  215
+            # rename checkpoints to follow file
  216
+            for cp in old_checkpoints:
  217
+                checkpoint_id = cp['checkpoint_id']
  218
+                old_cp_path = self.get_checkpoint_path_by_name(old_name, checkpoint_id)
  219
+                new_cp_path = self.get_checkpoint_path_by_name(new_name, checkpoint_id)
  220
+                if os.path.isfile(old_cp_path):
  221
+                    self.log.debug("renaming checkpoint %s -> %s", old_cp_path, new_cp_path)
  222
+                    os.rename(old_cp_path, new_cp_path)
  223
+            
168 224
         return notebook_id
169 225
 
170 226
     def delete_notebook(self, notebook_id):
171 227
         """Delete notebook by notebook_id."""
172  
-        path = self.find_path(notebook_id)
173  
-        if not os.path.isfile(path):
  228
+        nb_path = self.get_path(notebook_id)
  229
+        if not os.path.isfile(nb_path):
174 230
             raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
175  
-        os.unlink(path)
  231
+        
  232
+        # clear checkpoints
  233
+        for checkpoint in self.list_checkpoints(notebook_id):
  234
+            checkpoint_id = checkpoint['checkpoint_id']
  235
+            path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  236
+            self.log.debug(path)
  237
+            if os.path.isfile(path):
  238
+                self.log.debug("unlinking checkpoint %s", path)
  239
+                os.unlink(path)
  240
+        
  241
+        self.log.debug("unlinking notebook %s", nb_path)
  242
+        os.unlink(nb_path)
176 243
         self.delete_notebook_id(notebook_id)
177 244
 
178 245
     def increment_filename(self, basename):
@@ -191,6 +258,89 @@ def increment_filename(self, basename):
191 258
             else:
192 259
                 i = i+1
193 260
         return name
  261
+    
  262
+    # Checkpoint-related utilities
  263
+    
  264
+    def get_checkpoint_path_by_name(self, name, checkpoint_id):
  265
+        """Return a full path to a notebook checkpoint, given its name and checkpoint id."""
  266
+        filename = "{name}-{checkpoint_id}{ext}".format(
  267
+            name=name,
  268
+            checkpoint_id=checkpoint_id,
  269
+            ext=self.filename_ext,
  270
+        )
  271
+        path = os.path.join(self.checkpoint_dir, filename)
  272
+        return path
  273
+    
  274
+    def get_checkpoint_path(self, notebook_id, checkpoint_id):
  275
+        """find the path to a checkpoint"""
  276
+        name = self.get_name(notebook_id)
  277
+        return self.get_checkpoint_path_by_name(name, checkpoint_id)
  278
+    
  279
+    def get_checkpoint_info(self, notebook_id, checkpoint_id):
  280
+        """construct the info dict for a given checkpoint"""
  281
+        path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  282
+        stats = os.stat(path)
  283
+        last_modified = datetime.datetime.utcfromtimestamp(stats.st_mtime)
  284
+        info = dict(
  285
+            checkpoint_id = checkpoint_id,
  286
+            last_modified = last_modified,
  287
+        )
  288
+        
  289
+        return info
  290
+        
  291
+    # public checkpoint API
  292
+    
  293
+    def create_checkpoint(self, notebook_id):
  294
+        """Create a checkpoint from the current state of a notebook"""
  295
+        nb_path = self.get_path(notebook_id)
  296
+        # only the one checkpoint ID:
  297
+        checkpoint_id = "checkpoint"
  298
+        cp_path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  299
+        self.log.debug("creating checkpoint for notebook %s", notebook_id)
  300
+        if not os.path.exists(self.checkpoint_dir):
  301
+            os.mkdir(self.checkpoint_dir)
  302
+        shutil.copy2(nb_path, cp_path)
194 303
         
  304
+        # return the checkpoint info
  305
+        return self.get_checkpoint_info(notebook_id, checkpoint_id)
  306
+    
  307
+    def list_checkpoints(self, notebook_id):
  308
+        """list the checkpoints for a given notebook
  309
+        
  310
+        This notebook manager currently only supports one checkpoint per notebook.
  311
+        """
  312
+        checkpoint_id = "checkpoint"
  313
+        path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  314
+        if not os.path.exists(path):
  315
+            return []
  316
+        else:
  317
+            return [self.get_checkpoint_info(notebook_id, checkpoint_id)]
  318
+        
  319
+    
  320
+    def restore_checkpoint(self, notebook_id, checkpoint_id):
  321
+        """restore a notebook to a checkpointed state"""
  322
+        self.log.info("restoring Notebook %s from checkpoint %s", notebook_id, checkpoint_id)
  323
+        nb_path = self.get_path(notebook_id)
  324
+        cp_path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  325
+        if not os.path.isfile(cp_path):
  326
+            self.log.debug("checkpoint file does not exist: %s", cp_path)
  327
+            raise web.HTTPError(404,
  328
+                u'Notebook checkpoint does not exist: %s-%s' % (notebook_id, checkpoint_id)
  329
+            )
  330
+        # ensure notebook is readable (never restore from an unreadable notebook)
  331
+        last_modified, nb = self.read_notebook_object_from_path(cp_path)
  332
+        shutil.copy2(cp_path, nb_path)
  333
+        self.log.debug("copying %s -> %s", cp_path, nb_path)
  334
+    
  335
+    def delete_checkpoint(self, notebook_id, checkpoint_id):
  336
+        """delete a notebook's checkpoint"""
  337
+        path = self.get_checkpoint_path(notebook_id, checkpoint_id)
  338
+        if not os.path.isfile(path):
  339
+            raise web.HTTPError(404,
  340
+                u'Notebook checkpoint does not exist: %s-%s' % (notebook_id, checkpoint_id)
  341
+            )
  342
+        self.log.debug("unlinking %s", path)
  343
+        os.unlink(path)
  344
+    
195 345
     def info_string(self):
196 346
         return "Serving notebooks from local directory: %s" % self.notebook_dir
52 IPython/frontend/html/notebook/handlers.py
@@ -399,7 +399,7 @@ def post(self):
399 399
         notebook_id = self.get_argument('notebook', default=None)
400 400
         kernel_id = km.start_kernel(notebook_id, cwd=nbm.notebook_dir)
401 401
         data = {'ws_url':self.ws_url,'kernel_id':kernel_id}
402  
-        self.set_header('Location', '/'+kernel_id)
  402
+        self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id))
403 403
         self.finish(jsonapi.dumps(data))
404 404
 
405 405
 
@@ -426,7 +426,7 @@ def post(self, kernel_id, action):
426 426
         if action =/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%3D&y=1999 'restart':
427 427
             km.restart_kernel(kernel_id)
428 428
             data = {'ws_url':self.ws_url, 'kernel_id':kernel_id}
429  
-            self.set_header('Location', '/'+kernel_id)
  429
+            self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id))
430 430
             self.write(jsonapi.dumps(data))
431 431
         self.finish()
432 432
 
@@ -641,7 +641,7 @@ def post(self):
641 641
             notebook_id = nbm.save_new_notebook(body, name=name, format=format)
642 642
         else:
643 643
             notebook_id = nbm.new_notebook()
644  
-        self.set_header('Location', '/'+notebook_id)
  644
+        self.set_header('Location', '{0}notebooks/{1}'.format(self.base_project_url, notebook_id))
645 645
         self.finish(jsonapi.dumps(notebook_id))
646 646
 
647 647
 
@@ -680,6 +680,52 @@ def delete(self, notebook_id):
680 680
         self.finish()
681 681
 
682 682
 
  683
+class NotebookCheckpointsHandler(IPythonHandler):
  684
+    
  685
+    SUPPORTED_METHODS = ('GET', 'POST')
  686
+    
  687
+    @web.authenticated
  688
+    def get(self, notebook_id):
  689
+        """get lists checkpoints for a notebook"""
  690
+        nbm = self.notebook_manager
  691
+        checkpoints = nbm.list_checkpoints(notebook_id)
  692
+        data = jsonapi.dumps(checkpoints, default=date_default)
  693
+        self.finish(data)
  694
+    
  695
+    @web.authenticated
  696
+    def post(self, notebook_id):
  697
+        """post creates a new checkpoint"""
  698
+        nbm = self.notebook_manager
  699
+        checkpoint = nbm.create_checkpoint(notebook_id)
  700
+        data = jsonapi.dumps(checkpoint, default=date_default)
  701
+        self.set_header('Location', '{0}notebooks/{1}/checkpoints/{2}'.format(
  702
+            self.base_project_url, notebook_id, checkpoint['checkpoint_id']
  703
+        ))
  704
+        
  705
+        self.finish(data)
  706
+
  707
+
  708
+class ModifyNotebookCheckpointsHandler(IPythonHandler):
  709
+    
  710
+    SUPPORTED_METHODS = ('POST', 'DELETE')
  711
+    
  712
+    @web.authenticated
  713
+    def post(self, notebook_id, checkpoint_id):
  714
+        """post restores a notebook from a checkpoint"""
  715
+        nbm = self.notebook_manager
  716
+        nbm.restore_checkpoint(notebook_id, checkpoint_id)
  717
+        self.set_status(204)
  718
+        self.finish()
  719
+    
  720
+    @web.authenticated
  721
+    def delete(self, notebook_id, checkpoint_id):
  722
+        """delete clears a checkpoint for a given notebook"""
  723
+        nbm = self.notebook_manager
  724
+        nbm.delte_checkpoint(notebook_id, checkpoint_id)
  725
+        self.set_status(204)
  726
+        self.finish()
  727
+
  728
+
683 729
 class NotebookCopyHandler(IPythonHandler):
684 730
 
685 731
     @web.authenticated
23 IPython/frontend/html/notebook/nbmanager.py
@@ -36,7 +36,7 @@ class NotebookManager(LoggingConfigurable):
36 36
     # 1. Where the notebooks are stored if FileNotebookManager is used.
37 37
     # 2. The cwd of the kernel for a project.
38 38
     # Right now we use this attribute in a number of different places and
39  
-    # we are going to have to disentagle all of this. 
  39
+    # we are going to have to disentangle all of this.
40 40
     notebook_dir = Unicode(os.getcwdu(), config=True, help="""
41 41
         The directory to use for notebooks.
42 42
     """)
@@ -205,7 +205,28 @@ def copy_notebook(self, notebook_id):
205 205
         nb.metadata.name = name
206 206
         notebook_id = self.write_notebook_object(nb)
207 207
         return notebook_id
  208
+    
  209
+    # Checkpoint-related
  210
+    
  211
+    def create_checkpoint(self, notebook_id):
  212
+        """Create a checkpoint of the current state of a notebook
  213
+        
  214
+        Returns a checkpoint_id for the new checkpoint.
  215
+        """
  216
+        raise NotImplementedError("must be implemented in a subclass")
  217
+    
  218
+    def list_checkpoints(self, notebook_id):
  219
+        """Return a list of checkpoints for a given notebook"""
  220
+        return []
  221
+    
  222
+    def restore_checkpoint(self, notebook_id, checkpoint_id):
  223
+        """Restore a notebook from one of its checkpoints"""
  224
+        raise NotImplementedError("must be implemented in a subclass")
208 225
 
  226
+    def delete_checkpoint(self, notebook_id, checkpoint_id):
  227
+        """delete a checkpoint for a notebook"""
  228
+        raise NotImplementedError("must be implemented in a subclass")
  229
+    
209 230
     def log_info(self):
210 231
         self.log.info(self.info_string())
211 232
     
10 IPython/frontend/html/notebook/notebookapp.py
@@ -68,8 +68,9 @@
68 68
     ProjectDashboardHandler, NewHandler, NamedNotebookHandler,
69 69
     MainKernelHandler, KernelHandler, KernelActionHandler, IOPubHandler, StdinHandler,
70 70
     ShellHandler, NotebookRootHandler, NotebookHandler, NotebookCopyHandler,
71  
-    AuthenticatedFileHandler, MainClusterHandler, ClusterProfileHandler, 
72  
-    ClusterActionHandler, FileFindHandler, NotebookRedirectHandler,
  71
+    NotebookRedirectHandler, NotebookCheckpointsHandler, ModifyNotebookCheckpointsHandler,
  72
+    AuthenticatedFileHandler, FileFindHandler,
  73
+    MainClusterHandler, ClusterProfileHandler, ClusterActionHandler, 
73 74
 )
74 75
 from .nbmanager import NotebookManager
75 76
 from .filenbmanager import FileNotebookManager
@@ -104,6 +105,7 @@
104 105
 _kernel_action_regex = r"(?P<action>restart|interrupt)"
105 106
 _notebook_id_regex = r"(?P<notebook_id>\w+-\w+-\w+-\w+-\w+)"
106 107
 _notebook_name_regex = r"(?P<notebook_name>.+\.ipynb)"
  108
+_checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)"
107 109
 _profile_regex = r"(?P<profile>[^\/]+)" # there is almost no text that is invalid
108 110
 _cluster_action_regex = r"(?P<action>start|stop)"
109 111
 
@@ -161,6 +163,10 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager,
161 163
             (r"/kernels/%s/stdin" % _kernel_id_regex, StdinHandler),
162 164
             (r"/notebooks", NotebookRootHandler),
163 165
             (r"/notebooks/%s" % _notebook_id_regex, NotebookHandler),
  166
+            (r"/notebooks/%s/checkpoints" % _notebook_id_regex, NotebookCheckpointsHandler),
  167
+            (r"/notebooks/%s/checkpoints/%s" % (_notebook_id_regex, _checkpoint_id_regex),
  168
+                ModifyNotebookCheckpointsHandler
  169
+            ),
164 170
             (r"/files/(.*)", AuthenticatedFileHandler, {'path' : notebook_manager.notebook_dir}),
165 171
             (r"/clusters", MainClusterHandler),
166 172
             (r"/clusters/%s/%s" % (_profile_regex, _cluster_action_regex), ClusterActionHandler),
2  IPython/frontend/html/notebook/static/css/style.min.css
@@ -870,6 +870,7 @@ body{background-color:#ffffff;}
870 870
 body.notebook_app{overflow:hidden;}
871 871
 blockquote{border-left:4px solid #DDD;padding:0 15px;color:#777;}
872 872
 span#save_widget{padding:5px;margin:0px 0px 0px 300px;display:inline-block;}
  873
+span#checkpoint_status span#autosave_status{font-size:small;}
873 874
 span#notebook_name{height:1em;line-height:1em;padding:3px;border:none;font-size:146.5%;}
874 875
 .ui-menubar-item .ui-button .ui-button-text{padding:0.4em 1.0em;font-size:100%;}
875 876
 .ui-menu{-webkit-box-shadow:0px 6px 10px -1px #adadad;-moz-box-shadow:0px 6px 10px -1px #adadad;box-shadow:0px 6px 10px -1px #adadad;}
@@ -942,6 +943,7 @@ a.heading-anchor:link,a.heading-anchor:visited{text-decoration:none;color:inheri
942 943
 div.raw_input{padding-top:0px;padding-bottom:0px;height:1em;line-height:1em;font-family:monospace;}
943 944
 span.input_prompt{font-family:inherit;}
944 945
 input.raw_input{font-family:inherit;font-size:inherit;color:inherit;width:auto;margin:-2px 0px 0px 1px;padding-left:1px;padding-top:2px;height:1em;}
  946
+p.p-space{margin-bottom:10px;}
945 947
 .rendered_html{color:black;}.rendered_html em{font-style:italic;}
946 948
 .rendered_html strong{font-weight:bold;}
947 949
 .rendered_html u{text-decoration:underline;}
4 IPython/frontend/html/notebook/static/js/maintoolbar.js
@@ -25,10 +25,10 @@ var IPython = (function (IPython) {
25 25
         this.add_buttons_group([
26 26
                 {
27 27
                     id : 'save_b',
28  
-                    label : 'Save',
  28
+                    label : 'Save and Checkpoint',
29 29
                     icon : 'ui-icon-disk',
30 30
                     callback : function () {
31  
-                        IPython.notebook.save_notebook();
  31
+                        IPython.notebook.save_checkpoint();
32 32
                         }
33 33
                 }
34 34
             ]);
40 IPython/frontend/html/notebook/static/js/menubar.js
@@ -60,6 +60,7 @@ var IPython = (function (IPython) {
60 60
                 IPython.notebook.select(i);
61 61
             }
62 62
         });
  63
+        this.element.find("#restore_checkpoint").find("ul").find("li").hide();
63 64
     };
64 65
 
65 66
 
@@ -81,8 +82,10 @@ var IPython = (function (IPython) {
81 82
             window.open(url,'_blank');
82 83
             return false;
83 84
         });
84  
-        this.element.find('#save_notebook').click(function () {
85  
-            IPython.notebook.save_notebook();
  85
+        this.element.find('#save_checkpoint').click(function () {
  86
+            IPython.notebook.save_checkpoint();
  87
+        });
  88
+        this.element.find('#restore_checkpoint').click(function () {
86 89
         });
87 90
         this.element.find('#download_ipynb').click(function () {
88 91
             var notebook_id = IPython.notebook.get_notebook_id();
@@ -218,8 +221,41 @@ var IPython = (function (IPython) {
218 221
         this.element.find('#keyboard_shortcuts').click(function () {
219 222
             IPython.quick_help.show_keyboard_shortcuts();
220 223
         });
  224
+        
  225
+        this.update_restore_checkpoint(null);
  226
+        
  227
+        $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
  228
+            that.update_restore_checkpoint(data);
  229
+        });
  230
+        
  231
+        $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
  232
+            that.update_restore_checkpoint([data]);
  233
+        });
221 234
     };
222 235
 
  236
+    MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
  237
+        if (! checkpoints) {
  238
+            checkpoints = [];
  239
+        };
  240
+        this.element.find("#restore_checkpoint").find("ul").find("li").each(function(i) {
  241
+            var li = $(this);
  242
+            var a = li.find("a");
  243
+            a.off("click");
  244
+            if (checkpoints.length <= i) {
  245
+                li.hide();
  246
+                return;
  247
+            } else {
  248
+                li.show();
  249
+            };
  250
+            var checkpoint = checkpoints[i];
  251
+            var d = new Date(checkpoint.last_modified);
  252
+            li.find('a').text(
  253
+                d.format("mmm dd HH:MM:ss")
  254
+            ).click(function () {
  255
+                IPython.notebook.restore_checkpoint_dialog(checkpoint);
  256
+            });
  257
+        });
  258
+    };
223 259
 
224 260
     IPython.MenuBar = MenuBar;
225 261
 
310 IPython/frontend/html/notebook/static/js/notebook.js
@@ -39,6 +39,12 @@ var IPython = (function (IPython) {
39 39
         this.paste_enabled = false;
40 40
         this.dirty = false;
41 41
         this.metadata = {};
  42
+        this._checkpoint_after_save = false;
  43
+        this.last_checkpoint = null;
  44
+        this.autosave_interval = 0;
  45
+        this.autosave_timer = null;
  46
+        // autosave *at most* every two minutes
  47
+        this.minimum_autosave_interval = 120000;
42 48
         // single worksheet for now
43 49
         this.worksheet_metadata = {};
44 50
         this.control_key_active = false;
@@ -124,7 +130,7 @@ var IPython = (function (IPython) {
124 130
             // Save (CTRL+S) or (AppleKey+S)
125 131
             //metaKey = applekey on mac
126 132
             if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
127  
-                that.save_notebook();
  133
+                that.save_checkpoint();
128 134
                 event.preventDefault();
129 135
                 return false;
130 136
             } else if (event.which === key.ESC) {
@@ -250,7 +256,7 @@ var IPython = (function (IPython) {
250 256
                 return false;
251 257
             } else if (event.which === 83 && that.control_key_active) {
252 258
                 // Save notebook = s
253  
-                that.save_notebook();
  259
+                that.save_checkpoint();
254 260
                 that.control_key_active = false;
255 261
                 return false;
256 262
             } else if (event.which === 74 && that.control_key_active) {
@@ -336,8 +342,9 @@ var IPython = (function (IPython) {
336 342
             if (kill_kernel) {
337 343
                 that.kernel.kill();
338 344
             }
339  
-            if (that.dirty && ! that.read_only) {
340  
-                return "You have unsaved changes that will be lost if you leave this page.";
  345
+            // if we are autosaving, trigger an autosave on nav-away
  346
+            if (that.dirty && that.autosave_interval && ! that.read_only) {
  347
+                that.save_notebook();
341 348
             };
342 349
             // Null is the *only* return value that will make the browser not
343 350
             // pop up the "don't leave" dialog.
@@ -1550,6 +1557,33 @@ var IPython = (function (IPython) {
1550 1557
     };
1551 1558
 
1552 1559
     /**
  1560
+     * Start an autosave timer, for periodically saving the notebook.
  1561
+     * 
  1562
+     * @method set_autosave_interval
  1563
+     * @param {Integer} interval the autosave interval in milliseconds
  1564
+     */
  1565
+    Notebook.prototype.set_autosave_interval = function (interval) {
  1566
+        var that = this;
  1567
+        // clear previous interval, so we don't get simultaneous timers
  1568
+        if (this.autosave_timer) {
  1569
+            clearInterval(this.autosave_timer);
  1570
+        }
  1571
+        
  1572
+        this.autosave_interval = this.minimum_autosave_interval = interval;
  1573
+        if (interval) {
  1574
+            this.autosave_timer = setInterval(function() {
  1575
+                if (that.dirty) {
  1576
+                    that.save_notebook();
  1577
+                }
  1578
+            }, interval);
  1579
+            $([IPython.events]).trigger("autosave_enabled.Notebook", interval);
  1580
+        } else {
  1581
+            this.autosave_timer = null;
  1582
+            $([IPython.events]).trigger("autosave_disabled.Notebook");
  1583
+        };
  1584
+    };
  1585
+    
  1586
+    /**
1553 1587
      * Save this notebook on the server.
1554 1588
      * 
1555 1589
      * @method save_notebook
@@ -1560,6 +1594,10 @@ var IPython = (function (IPython) {
1560 1594
         data.metadata.name = this.notebook_name;
1561 1595
         data.nbformat = this.nbformat;
1562 1596
         data.nbformat_minor = this.nbformat_minor;
  1597
+        
  1598
+        // time the ajax call for autosave tuning purposes.
  1599
+        var start =  new Date().getTime();
  1600
+
1563 1601
         // We do the call with settings so we can set cache to false.
1564 1602
         var settings = {
1565 1603
             processData : false,
@@ -1567,27 +1605,53 @@ var IPython = (function (IPython) {
1567 1605
             type : "PUT",
1568 1606
             data : JSON.stringify(data),
1569 1607
             headers : {'Content-Type': 'application/json'},
1570  
-            success : $.proxy(this.save_notebook_success,this),
1571  
-            error : $.proxy(this.save_notebook_error,this)
  1608
+            success : $.proxy(this.save_notebook_success, this, start),
  1609
+            error : $.proxy(this.save_notebook_error, this)
1572 1610
         };
1573 1611
         $([IPython.events]).trigger('notebook_saving.Notebook');
1574 1612
         var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
1575 1613
         $.ajax(url, settings);
1576 1614
     };
1577  
-
  1615
+    
1578 1616
     /**
1579 1617
      * Success callback for saving a notebook.
1580 1618
      * 
1581 1619
      * @method save_notebook_success
  1620
+     * @param {Integer} start the time when the save request started
1582 1621
      * @param {Object} data JSON representation of a notebook
1583 1622
      * @param {String} status Description of response status
1584 1623
      * @param {jqXHR} xhr jQuery Ajax object
1585 1624
      */
1586  
-    Notebook.prototype.save_notebook_success = function (data, status, xhr) {
  1625
+    Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
1587 1626
         this.dirty = false;
1588 1627
         $([IPython.events]).trigger('notebook_saved.Notebook');
  1628
+        this._update_autosave_interval(start);
  1629
+        if (this._checkpoint_after_save) {
  1630
+            this.create_checkpoint();
  1631
+            this._checkpoint_after_save = false;
  1632
+        };
1589 1633
     };
1590  
-
  1634
+    
  1635
+    /**
  1636
+     * update the autosave interval based on how long the last save took
  1637
+     * 
  1638
+     * @method _update_autosave_interval
  1639
+     * @param {Integer} timestamp when the save request started
  1640
+     */
  1641
+    Notebook.prototype._update_autosave_interval = function (start) {
  1642
+        var duration = (new Date().getTime() - start);
  1643
+        if (this.autosave_interval) {
  1644
+            // new save interval: higher of 10x save duration or parameter (default 30 seconds)
  1645
+            var interval = Math.max(10 * duration, this.minimum_autosave_interval);
  1646
+            // round to 10 seconds, otherwise we will be setting a new interval too often
  1647
+            interval = 10000 * Math.round(interval / 10000);
  1648
+            // set new interval, if it's changed
  1649
+            if (interval != this.autosave_interval) {
  1650
+                this.set_autosave_interval(interval);
  1651
+            }
  1652
+        }
  1653
+    };
  1654
+    
1591 1655
     /**
1592 1656
      * Failure callback for saving a notebook.
1593 1657
      * 
@@ -1599,7 +1663,7 @@ var IPython = (function (IPython) {
1599 1663
     Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1600 1664
         $([IPython.events]).trigger('notebook_save_failed.Notebook');
1601 1665
     };
1602  
-
  1666
+    
1603 1667
     /**
1604 1668
      * Request a notebook's data from the server.
1605 1669
      * 
@@ -1690,10 +1754,13 @@ var IPython = (function (IPython) {
1690 1754
             });
1691 1755
 
1692 1756
         }
  1757
+        
1693 1758
         // Create the kernel after the notebook is completely loaded to prevent
1694 1759
         // code execution upon loading, which is a security risk.
1695 1760
         if (! this.read_only) {
1696 1761
             this.start_kernel();
  1762
+            // load our checkpoint list
  1763
+            IPython.notebook.list_checkpoints();
1697 1764
         }
1698 1765
         $([IPython.events]).trigger('notebook_loaded.Notebook');
1699 1766
     };
@@ -1731,6 +1798,229 @@ var IPython = (function (IPython) {
1731 1798
         }
1732 1799
     }
1733 1800
 
  1801
+    /*********************  checkpoint-related  *********************/
  1802
+    
  1803
+    /**
  1804
+     * Save the notebook then immediately create a checkpoint.
  1805
+     * 
  1806
+     * @method save_checkpoint
  1807
+     */
  1808
+    Notebook.prototype.save_checkpoint = function () {
  1809
+        this._checkpoint_after_save = true;
  1810
+        this.save_notebook();
  1811
+    };
  1812
+    
  1813
+    /**
  1814
+     * List checkpoints for this notebook.
  1815
+     * 
  1816
+     * @method list_checkpoint
  1817
+     */
  1818
+    Notebook.prototype.list_checkpoints = function () {
  1819
+        var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
  1820
+        $.get(url).done(
  1821
+            $.proxy(this.list_checkpoints_success, this)
  1822
+        ).fail(
  1823
+            $.proxy(this.list_checkpoints_error, this)
  1824
+        );
  1825
+    };
  1826
+
  1827
+    /**
  1828
+     * Success callback for listing checkpoints.
  1829
+     * 
  1830
+     * @method list_checkpoint_success
  1831
+     * @param {Object} data JSON representation of a checkpoint
  1832
+     * @param {String} status Description of response status
  1833
+     * @param {jqXHR} xhr jQuery Ajax object
  1834
+     */
  1835
+    Notebook.prototype.list_checkpoints_success = function (data, status, xhr) {
  1836
+        var data = $.parseJSON(data);
  1837
+        if (data.length) {
  1838
+            this.last_checkpoint = data[0];
  1839
+        } else {
  1840
+            this.last_checkpoint = null;
  1841
+        }
  1842
+        $([IPython.events]).trigger('checkpoints_listed.Notebook', [data]);
  1843
+    };
  1844
+
  1845
+    /**
  1846
+     * Failure callback for listing a checkpoint.
  1847
+     * 
  1848
+     * @method list_checkpoint_error
  1849
+     * @param {jqXHR} xhr jQuery Ajax object
  1850
+     * @param {String} status Description of response status
  1851
+     * @param {String} error_msg HTTP error message
  1852
+     */
  1853
+    Notebook.prototype.list_checkpoints_error = function (xhr, status, error_msg) {
  1854
+        $([IPython.events]).trigger('list_checkpoints_failed.Notebook');
  1855
+    };
  1856
+    
  1857
+    /**
  1858
+     * Create a checkpoint of this notebook on the server from the most recent save.
  1859
+     * 
  1860
+     * @method create_checkpoint
  1861
+     */
  1862
+    Notebook.prototype.create_checkpoint = function () {
  1863
+        var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints';
  1864
+        $.post(url).done(
  1865
+            $.proxy(this.create_checkpoint_success, this)
  1866
+        ).fail(
  1867
+            $.proxy(this.create_checkpoint_error, this)
  1868
+        );
  1869
+    };
  1870
+
  1871
+    /**
  1872
+     * Success callback for creating a checkpoint.
  1873
+     * 
  1874
+     * @method create_checkpoint_success
  1875
+     * @param {Object} data JSON representation of a checkpoint
  1876
+     * @param {String} status Description of response status
  1877
+     * @param {jqXHR} xhr jQuery Ajax object
  1878
+     */
  1879
+    Notebook.prototype.create_checkpoint_success = function (data, status, xhr) {
  1880
+        var data = $.parseJSON(data);
  1881
+        this.last_checkpoint = data;
  1882
+        $([IPython.events]).trigger('checkpoint_created.Notebook', data);
  1883
+    };
  1884
+
  1885
+    /**
  1886
+     * Failure callback for creating a checkpoint.
  1887
+     * 
  1888
+     * @method create_checkpoint_error
  1889
+     * @param {jqXHR} xhr jQuery Ajax object
  1890
+     * @param {String} status Description of response status
  1891
+     * @param {String} error_msg HTTP error message
  1892
+     */
  1893
+    Notebook.prototype.create_checkpoint_error = function (xhr, status, error_msg) {
  1894
+        $([IPython.events]).trigger('checkpoint_failed.Notebook');
  1895
+    };
  1896
+    
  1897
+    Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
  1898
+        var that = this;
  1899
+        var checkpoint = checkpoint || this.last_checkpoint;
  1900
+        if ( ! checkpoint ) {
  1901
+            console.log("restore dialog, but no checkpoint to restore to!");
  1902
+            return;
  1903
+        }
  1904
+        var dialog = $('<div/>').append(
  1905
+            $('<p/>').addClass("p-space").text(
  1906
+                "Are you sure you want to revert the notebook to " +
  1907
+                "the latest checkpoint?"
  1908
+            ).append(
  1909
+                $("<strong/>").text(
  1910
+                    " This cannot be undone."
  1911
+                )
  1912
+            )
  1913
+        ).append(
  1914
+            $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
  1915
+        ).append(
  1916
+            $('<p/>').addClass("p-space").text(
  1917
+                Date(checkpoint.last_modified)
  1918
+            ).css("text-align", "center")
  1919
+        );
  1920
+        
  1921
+        $(document).append(dialog);
  1922
+        
  1923
+        dialog.dialog({
  1924
+            resizable: false,
  1925
+            modal: true,
  1926
+            title: "Revert notebook to checkpoint",
  1927
+            closeText: '',
  1928
+            buttons : {
  1929
+                "Revert": function () {
  1930
+                    that.restore_checkpoint(checkpoint.checkpoint_id);
  1931
+                    $(this).dialog('close');
  1932
+                },
  1933
+                "Cancel": function () {
  1934
+                    $(this).dialog('close');
  1935
+                }
  1936
+            },
  1937
+            width: 400
  1938
+        });
  1939
+    }
  1940
+    
  1941
+    /**
  1942
+     * Restore the notebook to a checkpoint state.
  1943
+     * 
  1944
+     * @method restore_checkpoint
  1945
+     * @param {String} checkpoint ID
  1946
+     */
  1947
+    Notebook.prototype.restore_checkpoint = function (checkpoint) {
  1948
+        $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
  1949
+        var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
  1950
+        $.post(url).done(
  1951
+            $.proxy(this.restore_checkpoint_success, this)
  1952
+        ).fail(
  1953
+            $.proxy(this.restore_checkpoint_error, this)
  1954
+        );
  1955
+    };
  1956
+    
  1957
+    /**
  1958
+     * Success callback for restoring a notebook to a checkpoint.
  1959
+     * 
  1960
+     * @method restore_checkpoint_success
  1961
+     * @param {Object} data (ignored, should be empty)
  1962
+     * @param {String} status Description of response status
  1963
+     * @param {jqXHR} xhr jQuery Ajax object
  1964
+     */
  1965
+    Notebook.prototype.restore_checkpoint_success = function (data, status, xhr) {
  1966
+        $([IPython.events]).trigger('checkpoint_restored.Notebook');
  1967
+        this.load_notebook(this.notebook_id);
  1968
+    };
  1969
+
  1970
+    /**
  1971
+     * Failure callback for restoring a notebook to a checkpoint.
  1972
+     * 
  1973
+     * @method restore_checkpoint_error
  1974
+     * @param {jqXHR} xhr jQuery Ajax object
  1975
+     * @param {String} status Description of response status
  1976
+     * @param {String} error_msg HTTP error message
  1977
+     */
  1978
+    Notebook.prototype.restore_checkpoint_error = function (xhr, status, error_msg) {
  1979
+        $([IPython.events]).trigger('checkpoint_restore_failed.Notebook');
  1980
+    };
  1981
+    
  1982
+    /**
  1983
+     * Delete a notebook checkpoint.
  1984
+     * 
  1985
+     * @method delete_checkpoint
  1986
+     * @param {String} checkpoint ID
  1987
+     */
  1988
+    Notebook.prototype.delete_checkpoint = function (checkpoint) {
  1989
+        $([IPython.events]).trigger('notebook_restoring.Notebook', checkpoint);
  1990
+        var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id + '/checkpoints/' + checkpoint;
  1991
+        $.ajax(url, {
  1992
+            type: 'DELETE',
  1993
+            success: $.proxy(this.delete_checkpoint_success, this),
  1994
+            error: $.proxy(this.delete_notebook_error,this)
  1995
+        });
  1996
+    };
  1997
+    
  1998
+    /**
  1999
+     * Success callback for deleting a notebook checkpoint
  2000
+     * 
  2001
+     * @method delete_checkpoint_success
  2002
+     * @param {Object} data (ignored, should be empty)
  2003
+     * @param {String} status Description of response status
  2004
+     * @param {jqXHR} xhr jQuery Ajax object
  2005
+     */
  2006
+    Notebook.prototype.delete_checkpoint_success = function (data, status, xhr) {
  2007
+        $([IPython.events]).trigger('checkpoint_deleted.Notebook', data);
  2008
+        this.load_notebook(this.notebook_id);
  2009
+    };
  2010
+
  2011
+    /**
  2012
+     * Failure callback for deleting a notebook checkpoint.
  2013
+     * 
  2014
+     * @method delete_checkpoint_error
  2015
+     * @param {jqXHR} xhr jQuery Ajax object
  2016
+     * @param {String} status Description of response status
  2017
+     * @param {String} error_msg HTTP error message
  2018
+     */
  2019
+    Notebook.prototype.delete_checkpoint_error = function (xhr, status, error_msg) {
  2020
+        $([IPython.events]).trigger('checkpoint_delete_failed.Notebook');
  2021
+    };
  2022
+
  2023
+
1734 2024
     IPython.Notebook = Notebook;
1735 2025
 
1736 2026
 
9 IPython/frontend/html/notebook/static/js/notebookmain.js
@@ -80,14 +80,19 @@ $(document).ready(function () {
80 80
     IPython.page.show();
81 81
 
82 82
     IPython.layout_manager.do_resize();
83  
-    $([IPython.events]).on('notebook_loaded.Notebook', function () {
  83
+    var first_load = function () {
84 84
         IPython.layout_manager.do_resize();
85 85
         var hash = document.location.hash;
86 86
         if (hash) {
87 87
             document.location.hash = '';
88 88
             document.location.hash = hash;
89 89
         }
90  
-    });
  90
+        IPython.notebook.set_autosave_interval(IPython.notebook.minimum_autosave_interval);
  91
+        // only do this once
  92
+        $([IPython.events]).off('notebook_loaded.Notebook', first_load);
  93
+    };
  94
+    
  95
+    $([IPython.events]).on('notebook_loaded.Notebook', first_load);
91 96
     IPython.notebook.load_notebook($('body').data('notebookId'));
92 97
 
93 98
 });
33 IPython/frontend/html/notebook/static/js/notificationarea.js
@@ -177,6 +177,39 @@ var IPython = (function (IPython) {
177 177
         $([IPython.events]).on('notebook_save_failed.Notebook', function () {
178 178
             nnw.set_message("Notebook save failed");
179 179
         });
  180
+        
  181
+        // Checkpoint events
  182
+        $([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) {
  183
+            var msg = "Checkpoint created";
  184
+            if (data.last_modified) {
  185
+                var d = new Date(data.last_modified);
  186
+                msg = msg + ": " + d.format("HH:MM:ss");
  187
+            }
  188
+            nnw.set_message(msg, 2000);
  189
+        });
  190
+        $([IPython.events]).on('checkpoint_failed.Notebook', function () {
  191
+            nnw.set_message("Checkpoint failed");
  192
+        });
  193
+        $([IPython.events]).on('checkpoint_deleted.Notebook', function () {
  194
+            nnw.set_message("Checkpoint deleted", 500);
  195
+        });
  196
+        $([IPython.events]).on('checkpoint_delete_failed.Notebook', function () {
  197
+            nnw.set_message("Checkpoint delete failed");
  198
+        });
  199
+        $([IPython.events]).on('checkpoint_restoring.Notebook', function () {
  200
+            nnw.set_message("Restoring to checkpoint...", 500);
  201
+        });
  202
+        $([IPython.events]).on('checkpoint_restore_failed.Notebook', function () {
  203
+            nnw.set_message("Checkpoint restore failed");
  204
+        });
  205
+
  206
+        // Autosave events
  207
+        $([IPython.events]).on('autosave_disabled.Notebook', function () {
  208
+            nnw.set_message("Autosave disabled", 2000);
  209
+        });
  210
+        $([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) {
  211
+            nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
  212
+        });
180 213
 
181 214
     };
182 215
 
25 IPython/frontend/html/notebook/static/js/savewidget.js
@@ -26,7 +26,9 @@ var IPython = (function (IPython) {
26 26
     SaveWidget.prototype.style = function () {
27 27
         this.element.find('span#save_widget').addClass('ui-widget');
28 28
         this.element.find('span#notebook_name').addClass('ui-widget');
29  
-        this.element.find('span#save_status').addClass('ui-widget')
  29
+        this.element.find('span#autosave_status').addClass('ui-widget')
  30
+            .css({border: 'none'});
  31
+        this.element.find('span#checkpoint_status').addClass('ui-widget')
30 32
             .css({border: 'none', 'margin-left': '20px'});
31 33
     };
32 34
 
@@ -54,6 +56,13 @@ var IPython = (function (IPython) {
54 56
         $([IPython.events]).on('notebook_save_failed.Notebook', function () {
55 57
             that.set_save_status('Last Save Failed!');
56 58
         });
  59
+        $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
  60
+            that.set_last_checkpoint(data[0]);
  61
+        });
  62
+        
  63
+        $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
  64
+            that.set_last_checkpoint(data);
  65
+        });
57 66
     };
58 67
 
59 68
 
@@ -121,13 +130,23 @@ var IPython = (function (IPython) {
121 130
 
122 131
 
123 132
     SaveWidget.prototype.set_save_status = function (msg) {
124  
-        this.element.find('span#save_status').html(msg);
  133
+        this.element.find('span#autosave_status').html(msg);
  134
+    }
  135
+
  136
+    SaveWidget.prototype.set_checkpoint_status = function (msg) {
  137
+        this.element.find('span#checkpoint_status').html(msg);
125 138
     }
126 139
 
  140
+    SaveWidget.prototype.set_last_checkpoint = function (checkpoint) {
  141
+        var d = new Date(checkpoint.last_modified);
  142
+        this.set_checkpoint_status(
  143
+            "Last Checkpoint: " + d.format('mmm dd HH:MM')
  144
+        );
  145
+    }
127 146
 
128 147
     SaveWidget.prototype.set_last_saved = function () {
129 148
         var d = new Date();
130  
-        this.set_save_status('Last saved: '+d.format('mmm dd HH:MM'));
  149
+        this.set_save_status('(autosaved: '+d.format('mmm dd HH:MM') + ')');
131 150
     };
132 151
 
133 152
 
13 IPython/frontend/html/notebook/static/less/notebook.less
@@ -27,6 +27,14 @@ span#save_widget {
27 27
     display:inline-block;
28 28
 }
29 29
 
  30
+span#checkpoint_status span#autosave_status {
  31
+    font-size: small;
  32
+}
  33
+
  34
+/*span#save_widget > span#autosave_status {
  35
+    font-size: x-small;
  36
+}
  37
+*/
30 38
 span#notebook_name {
31 39
     height: 1em;
32 40
     line-height: 1em;
@@ -498,3 +506,8 @@ input.raw_input {
498 506
   padding-top: 2px;
499 507
   height: 1em;
500 508
 }
  509
+
  510
+p.p-space {
  511
+  margin-bottom: 10px;
  512
+}
  513
+
15 IPython/frontend/html/notebook/templates/notebook.html
@@ -38,7 +38,8 @@
38 38
 
39 39
 <span id="save_widget">
40 40
     <span id="notebook_name"></span>
41  
-    <span id="save_status"></span>
  41
+    <span id="checkpoint_status"></span>
  42
+    <span id="autosave_status"></span>
42 43
 </span>
43 44
 
44 45
 {% endblock %}
@@ -56,7 +57,17 @@
56 57
                 <hr/>
57 58
                 <li id="copy_notebook"><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BMake&y=1999 a Copy...</a></li>
58 59
                 <li id="rename_notebook"><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BRename...%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
59  
-                <li id="save_notebook"><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BSave%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  60
+                <li id="save_checkpoint"><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BSave&y=1999 and Checkpoint</a></li>
  61
+                <hr/>
  62
+                <li id="restore_checkpoint"><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BRevert&y=1999 to Checkpoint</a>
  63
+                  <ul>
  64
+                    <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  65
+                    <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  66
+                    <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  67
+                    <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  68
+                    <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3B%26lt%3B%2Fa%26gt%3B%26lt%3B%2Fli%26gt%3B%3C%2Fpre&y=1999>
      
  69
+                  </ul>
  70
+                </li>
60 71
                 <hr/>
61 72
                 <li><a href=/old?u=https%3A%2F%2Fgithub.com%2Fipython%2Fipython%2Fpull%2F3158%2F%26quot%23%26quot%3B%26gt%3BDownload&y=1999 as</a>
62 73
                     <ul>
27 IPython/kernel/zmq/zmqshell.py
@@ -34,6 +34,7 @@
34 34
 from IPython.core.magics import MacroToEdit, CodeMagics
35 35
 from IPython.core.magic import magics_class, line_magic, Magics
36 36
 from IPython.core.payloadpage import install_payload_page
  37
+from IPython.display import display, Javascript
37 38
 from IPython.kernel.inprocess.socket import SocketABC
38 39
 from IPython.kernel import (
39 40
     get_connection_file, get_connection_info, connect_qtconsole
@@ -444,6 +445,32 @@ def qtconsole(self, arg_s):
444 445
         except Exception as e:
445 446
             error("Could not start qtconsole: %r" % e)
446 447
             return
  448
+    
  449
+    @line_magic
  450
+    def autosave(self, arg_s):
  451
+        """Set the autosave interval in the notebook (in seconds).
  452
+        
  453
+        The default value is 120, or two minutes.
  454
+        ``%autosave 0`` will disable autosave.
  455
+        
  456
+        This magic only has an effect when called from the notebook interface.
  457
+        It has no effect when called in a startup file.
  458
+        """
  459
+        
  460
+        try:
  461
+            interval = int(arg_s)
  462
+        except ValueError:
  463
+            raise UsageError("%%autosave requires an integer, got %r" % arg_s)
  464
+        
  465
+        # javascript wants milliseconds
  466
+        milliseconds = 1000 * interval
  467
+        display(Javascript("IPython.notebook.set_autosave_interval(%i)" % milliseconds),
  468
+            include=['application/javascript']
  469
+        )
  470
+        if interval:
  471
+            print("Autosaving every %i seconds" % interval)
  472
+        else:
  473
+            print("Autosave disabled")
447 474
 
448 475
 def safe_unicode(e):
449 476
     """unicode(e) with various fallbacks. Used for exceptions, which may not be

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.