Skip to content

Commit

Permalink
use raw XMLHttpRequest and cookie from utils._get_cookie, test with tar
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacer committed Apr 28, 2017
1 parent 6540a83 commit e576b32
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 43 deletions.
39 changes: 34 additions & 5 deletions notebook/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,45 @@ def respond_zip(handler, name, output, resources):
handler.set_header('Content-Disposition',
'attachment; filename="%s"' % escape.url_escape(zip_filename))
handler.set_header('Content-Type', 'application/zip')

# create zip file
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED) as zipf:
with zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_STORED) as zipf:
output_filename = os.path.splitext(name)[0] + resources['output_extension']
zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
# add external resources
for filename, data in output_files.items():
zipf.writestr(filename, data)

# with zipfile.ZipFile('./written_zip_test.zip',mode = 'w', compression=zipfile.ZIP_STORED) as zipf:
# output_filename = os.path.splitext(name)[0] + resources['output_extension']
# zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
# for filename, data in output_files.items():
# zipf.writestr(filename, data)
buffer.seek(0)
with open('buffer_write.zip', 'wb') as f:
f.write(buffer.read())
buffer.seek(0)

with open('buffer_write_2.zip', 'wb') as f:
f.write(buffer.read())
buffer.seek(0)

# tar_filename = os.path.splitext(name)[0] + '.tar'
# handler.set_header('Content-Disposition',
# 'attachment; filename="%s"' % escape.url_escape(tar_filename))
# handler.set_header('Content-Type', 'application/tar')
#
# output_filename = os.path.splitext(name)[0] + resources['output_extension']
# with tarfile.open(fileobj=buffer, mode='w') as tarf:
# main_file_data = cast_bytes(output, 'utf-8')
# main_file_tarinfo = tarfile.TarInfo()
# main_file_tarinfo.name = output_filename
# main_file_tarinfo.size = len(main_file_data)
# tarf.addfile(main_file_tarinfo, fileobj = io.BytesIO(main_file_data))
# for filename, data in output_files.items():
# extra_file = tarfile.TarInfo()
# extra_file.name = filename
# extra_file.size = len(data)
# tarf.addfile(extra_file, fileobj = io.BytesIO(data))


handler.finish(buffer.getvalue())
return True

Expand Down
1 change: 1 addition & 0 deletions notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ define([
dnd_contain_file: dnd_contain_file,
_ansispan:_ansispan,
change_favicon: change_favicon
_get_cookie:_get_cookie
};

return utils;
Expand Down
113 changes: 75 additions & 38 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,46 +206,83 @@ define('notebook/js/menubar',[
}
};
var on_done = function(){
var url = utils.url_path_join(
that.base_url,
'nbconvert',
fileformat.val(),
notebook_path
) + "?download=" + download.toString();

var xsrf_token = utils._get_cookie('_xsrf');
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.responseType = 'blob';
xhr.withCredentials = true;
xhr.setRequestHeader('X-XSRFToken', xsrf_token);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
var blob = xhr.response;
var content_disposition = xhr.getResponseHeader('Content-Disposition');
var filename = content_disposition.match(/filename="(.+)"/)[1];
saveData(blob, filename);
var url = utils.url_path_join(
that.base_url,
'nbconvert',
fileformat.val(),
notebook_path
) + "?download=" + download.toString();
var create_new_dl_window = function(){
console.log("I ran successfully")
body.empty().append('<p>').text('conversion in progress')
// var win = window.open('',IPython._target);
// win.location=url;
that._new_window(url);

return true;
};
var my_func = function(result){console.info("hi m and matthias!",typeof(result))}//, result)}
console.info("this is inside on done", that.json_content);

var xsrf_token = utils._get_cookie('_xsrf');
console.log(xsrf_token)
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.responseType = 'blob';
xhr.withCredentials = true;
xhr.setRequestHeader('X-XSRFToken', xsrf_token);
// xhr.onload = function(e) {
// if (this.status == 200) {
// // get binary data as a response
// var blob = this.response;
// }
// };
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
var blob = xhr.response;
var content_disposition = xhr.getResponseHeader('Content-Disposition');
var filename = content_disposition.match(/filename="(.+)"/)[1];
saveData(blob, filename);
}
}
}
var data = JSON.stringify(that.json_content);
xhr.send(data);
return true // close the dialog
// return false to keep it open.
};
var saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var blob = new Blob([data], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
var data = JSON.stringify(that.json_content);
xhr.send(data);
// utils.ajax(url, {
// method: "POST",
// data: data,
// processData: false,
// responseType: "blob"
// //create_new_dl_window
// })
// .fail(function(){
// console.warn('something wrong');
// })
// .done(function(data,textstatus,jqXHR){
// var content_disposition = jqXHR.getResponseHeader('Content-Disposition');
// var filename = content_disposition.match(/filename="(.+)"/)[1];
// saveData(data,filename);
// });
//p.onReady(function(){
// body.empty().append('<p>').text('conversion in progress')
//});
// $.post(url, json_content, create_new_dl_window,"json");
// get the data from FileReader and make it json.
return true // close the dialog
// return false to keep it open.
};
}());
var saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var blob = new Blob([data], {type: "octet/stream"}),//this works for tar
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());

var mod = dialog.modal({
notebook: this.notebook,
Expand Down

0 comments on commit e576b32

Please sign in to comment.