Skip to content

Commit

Permalink
save before download-as
Browse files Browse the repository at this point in the history
uses `async : false`

closes #852

download-as-py still unavailable.
  • Loading branch information
minrk committed Oct 8, 2013
1 parent 6d8e6f2 commit a0631a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
18 changes: 15 additions & 3 deletions IPython/html/services/notebooks/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,22 @@ def get(self, path='', name=None):
# List notebooks in 'path'
notebooks = nbm.list_notebooks(path)
self.finish(json.dumps(notebooks, default=date_default))
return
# get and return notebook representation
model = nbm.get_notebook_model(name, path)
self.set_header(u'Last-Modified', model[u'last_modified'])

if self.get_argument('download', default='False') == 'True':
format = self.get_argument('format', default='json')
if format == u'json':
self.set_header('Content-Type', 'application/json')
raise web.HTTPError(400, "Unrecognized format: %s" % ext)

self.set_header('Content-Disposition',
'attachment; filename="%s"' % name
)
self.finish(json.dumps(model['content'], default=date_default))
else:
# get and return notebook representation
model = nbm.get_notebook_model(name, path)
self.set_header(u'Last-Modified', model[u'last_modified'])
self.finish(json.dumps(model, default=date_default))

@web.authenticated
Expand Down
25 changes: 21 additions & 4 deletions IPython/html/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

var IPython = (function (IPython) {
"use strict";

var utils = IPython.utils;

/**
* A MenuBar Class to generate the menubar of IPython notebook
Expand Down Expand Up @@ -83,14 +85,29 @@ var IPython = (function (IPython) {
});
this.element.find('#download_ipynb').click(function () {
var notebook_name = IPython.notebook.get_notebook_name();
var url = that.baseProjectUrl() + 'api/notebooks' + that.notebookPath() +
notebook_name + '?format=json'+ '&download=True';
if (IPython.notebook.dirty) {
IPython.notebook.save_notebook({async : false});
}

var url = utils.url_path_join(
that.baseProjectUrl(),
'api/notebooks',
that.notebookPath(),
notebook_name + '.ipynb?format=json&download=True'
);
window.location.assign(url);
});
this.element.find('#download_py').click(function () {
var notebook_name = IPython.notebook.get_notebook_name();
var url = that.baseProjectUrl() + 'api/notebooks' + that.notebookPath() +
notebook_name + '?format=py' + '&download=True';
if (IPython.notebook.dirty) {
IPython.notebook.save_notebook({async : false});
}
var url = utils.url_path_join(
that.baseProjectUrl(),
'api/notebooks',
that.notebookPath(),
notebook_name + '.ipynb?format=py&download=True'
);
window.location.assign(url);
});
this.element.find('#rename_notebook').click(function () {
Expand Down
7 changes: 6 additions & 1 deletion IPython/html/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ var IPython = (function (IPython) {
*
* @method save_notebook
*/
Notebook.prototype.save_notebook = function () {
Notebook.prototype.save_notebook = function (extra_settings) {
// Create a JSON model to be sent to the server.
var model = {};
model.name = this.notebook_name;
Expand All @@ -1678,6 +1678,11 @@ var IPython = (function (IPython) {
success : $.proxy(this.save_notebook_success, this, start),
error : $.proxy(this.save_notebook_error, this)
};
if (extra_settings) {
for (var key in extra_settings) {
settings[key] = extra_settings[key];
}
}
$([IPython.events]).trigger('notebook_saving.Notebook');
var url = utils.url_path_join(
this.baseProjectUrl(),
Expand Down

0 comments on commit a0631a3

Please sign in to comment.