Skip to content

Commit

Permalink
match RFC5987 3.2 formatting for Content Disposition, prettify code
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacer committed Oct 4, 2017
1 parent c538a0e commit 817a87c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 131 deletions.
3 changes: 1 addition & 2 deletions notebook/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def respond_zip(handler, name, output, resources):

# Headers
zip_filename = os.path.splitext(name)[0] + '.zip'
handler.set_header('Content-Disposition',
'attachment; filename="%s"' % escape.url_escape(zip_filename))
handler.set_attachment_header(zip_filename)
handler.set_header('Content-Type', 'application/zip')

# create zip file
Expand Down
2 changes: 1 addition & 1 deletion notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ define([
js_idx_to_char_idx: js_idx_to_char_idx,
char_idx_to_js_idx: char_idx_to_js_idx,
_ansispan:_ansispan,
change_favicon: change_favicon
change_favicon: change_favicon,
_get_cookie:_get_cookie
};

Expand Down
270 changes: 142 additions & 128 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,177 +129,191 @@ define('notebook/js/menubar',[

this._new_window(url);
};

MenuBar.prototype._nbconvert_upload_conf = function (download) {

var body = $('<div>');
var notebook_path = utils.encode_uri_components(this.notebook.notebook_path);

var create_json = function(notebook, config){
MenuBar.prototype._nbconvert_upload_conf = function(download) {
var body = $("<div>");
var notebook_path = utils.encode_uri_components(
this.notebook.notebook_path
);

var create_json = function(notebook, config) {
var json_to_pass = {
notebook: notebook.toJSON(),
config: config
};
return json_to_pass;
};


var form = $('<form>');
var fileinput = $('<input>')
.attr('type', 'file')
.attr('tabindex', '1');
var form = $("<form>");
var fileinput = $("<input>")
.attr("type", "file")
.attr("tabindex", "1");

var fileformat = $("<select>");

var fileformat = $('<select>');

var export_opts = {
markdown: {
display_text:'markdown',
exporter_name:'markdown'
},
display_text: "markdown",
exporter_name: "markdown"
},
html: {
display_text:'html',
exporter_name:'html'
},
display_text: "html",
exporter_name: "html"
},
rst: {
display_text:'rst',
exporter_name:'rst'
},
display_text: "rst",
exporter_name: "rst"
},
pdf: {
display_text:'PDF',
exporter_name:'pdf'
},
display_text: "PDF",
exporter_name: "pdf"
},
latex: {
display_text:'LaTeX',
exporter_name:'latex'
},
display_text: "LaTeX",
exporter_name: "latex"
},
script: {
display_text:'script',
exporter_name:'script'
}
display_text: "script",
exporter_name: "script"
}
};

for(var x in export_opts){
for (var x in export_opts) {
fileformat.append(
$('<option/>')
.attr('value', export_opts[x]['exporter_name'])
.text(export_opts[x]['display_text'])
$("<option/>")
.attr("value", export_opts[x]["exporter_name"])
.text(export_opts[x]["display_text"])
);
};
}

form.append(fileformat);
form.append(fileinput);
body.append(form);

var that = this;

var trigger_nbconvert_post = function(){
var trigger_nbconvert_post = function() {
var filereader = new FileReader();
filereader.onload = function(){
filereader.onload = function() {
var my_config_data = filereader.result;
that['json_content'] = create_json(that.notebook, my_config_data);
that["json_content"] = create_json(that.notebook, my_config_data);
on_done();
};
if (fileinput[0].files.length>0){
if (fileinput[0].files.length > 0) {
filereader.readAsText(fileinput[0].files[0]);
} else{
that['json_content']=create_json(that.notebook, '{}')
} else {
that["json_content"] = create_json(that.notebook, "{}");
on_done();
}
};
var on_done = function(){
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 on_done = function() {
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);
console.log(xhr);
// 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\*=utf-8''(.+)/)[1];
saveData(blob, filename);
}
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 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,
title : "Upload nbconvert config",
open : function(){fileinput.focus();},
body : body,
buttons : {
cancel : {
click: function(){return true;}
title: "Upload nbconvert config",
open: function() {
fileinput.focus();
},
body: body,
buttons: {
cancel: {
click: function() {
return true;
}
},
download : {click: trigger_nbconvert_post}
download: { click: trigger_nbconvert_post }
}
});

mod.modal('show');

mod.modal("show");
};


Expand Down
1 change: 1 addition & 0 deletions notebook/templates/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<li id="print_preview"><a href="#">{% trans %}Print Preview{% endtrans %}</a></li>
<li id="custom_download"><a href="#">{% trans %}Custom Download {% endtrans %}</a></li>
<li class="dropdown-submenu"><a href="#">{% trans %}Download as{% endtrans %}</a>
<ul id="download_menu" class="dropdown-menu">
<li id="download_ipynb"><a href="#">Notebook (.ipynb)</a></li>
<li id="download_script"><a href="#">Script</a></li>
<li id="download_html"><a href="#">HTML (.html)</a></li>
Expand Down

0 comments on commit 817a87c

Please sign in to comment.