diff --git a/notebook/nbconvert/handlers.py b/notebook/nbconvert/handlers.py index 06b6d984455..6775b0cd79f 100644 --- a/notebook/nbconvert/handlers.py +++ b/notebook/nbconvert/handlers.py @@ -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 diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 474b034262e..e96ee26a997 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1114,6 +1114,7 @@ define([ dnd_contain_file: dnd_contain_file, _ansispan:_ansispan, change_favicon: change_favicon + _get_cookie:_get_cookie }; return utils; diff --git a/notebook/static/notebook/js/menubar.js b/notebook/static/notebook/js/menubar.js index fb0882c3132..e9312f8c260 100644 --- a/notebook/static/notebook/js/menubar.js +++ b/notebook/static/notebook/js/menubar.js @@ -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('

').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('

').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,