Repository: ipython/ipython

Issue:
firefox 25.0: notebooks report "Notebook save failed", .py script save fails, but .ipynb save succeeds
Notebooks have recently reported "Notebook save failed" when I save (or it autosaves). The .ipnb is updated, but the .py script fails to save. If I don't set NotebookManager.save_script=True, things work as expected. I get this in in the log when I attempt a save is:

> [tornado.access] WARNING | 400 PUT /api/notebooks/Untitled0.ipynb (127.0.0.1) 3.94ms

The "Last Checkpoint" is reported as Ocober 28 or 29 (depending on the notebook) This is presumably just before problems started?

Suspiciously, I have this in my apt logs:

> Start-Date: 2013-10-30  12:25:13
> Commandline: apt-get upgrade -y --fix-missing
> Upgrade: firefox-globalmenu:amd64 (24.0+build1-0ubuntu0.12.04.1, 25.0+build3-0ubuntu0.12.04.1), firefox:amd64 (24.0+build1-0ubuntu0.12.04.1, 25.0+build3-0ubuntu0.12.04.1), firefox-locale-cs:amd64 (24.0+build1-0ub
> End-Date: 2013-10-30  12:25:43

That's the most recent firefox update, and is just after the reported "Last Checkpoint"s.

I'm running in firefox 25.0 (the most recent ubuntu update). My ipython is updated (14625df).



Discussion:
I can reproduce this.

It's not Firefox specific, I see the same on Chrome.

Also fails on Chromium for me too. Ubuntu updated chromium yesterday (and in July before that). 


Resolving patch:
diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py
--- a/IPython/html/services/notebooks/filenbmanager.py
+++ b/IPython/html/services/notebooks/filenbmanager.py
@@ -251,7 +251,7 @@ def save_notebook_model(self, model, name='', path=''):
             self.log.debug("Writing script %s", py_path)
             try:
                 with io.open(py_path, 'w', encoding='utf-8') as f:
-                    current.write(model, f, u'py')
+                    current.write(nb, f, u'py')
             except Exception as e:
                 raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s %s' % (py_path, e))
 


Test patch:
diff --git a/IPython/html/services/notebooks/tests/test_nbmanager.py b/IPython/html/services/notebooks/tests/test_nbmanager.py
--- a/IPython/html/services/notebooks/tests/test_nbmanager.py
+++ b/IPython/html/services/notebooks/tests/test_nbmanager.py
@@ -196,6 +196,25 @@ def test_save_notebook_model(self):
             self.assertEqual(model['name'], 'Untitled0.ipynb')
             self.assertEqual(model['path'], sub_dir.strip('/'))
 
+    def test_save_notebook_with_script(self):
+        with TemporaryDirectory() as td:
+            # Create a notebook
+            nm = FileNotebookManager(notebook_dir=td)
+            nm.save_script = True
+            model = nm.create_notebook_model()
+            name = model['name']
+            path = model['path']
+
+            # Get the model with 'content'
+            full_model = nm.get_notebook_model(name, path)
+
+            # Save the notebook
+            model = nm.save_notebook_model(full_model, name, path)
+
+            # Check that the script was created
+            py_path = os.path.join(td, os.path.splitext(name)[0]+'.py')
+            assert os.path.exists(py_path), py_path
+
     def test_delete_notebook_model(self):
         with TemporaryDirectory() as td:
             # Test in the root directory