Hey,
I’ve tried googling several articles related to this, but still unable to figure why my download script button work. I am not understanding javascript much. Can you guys help me here. The objective is to download the content of columndatasource s3. I’ve attached my code for same. Any trouble shoot method you can recommend?
button_1 = Button(label="Download All", width=300)
download_javaScript="""
function table_to_csv(source) {
const columns = Object.keys(source.data)
const nrows = source.get_length()
const lines = [columns.join(',')]
for (let i = 0; i < nrows; i++) {
let row = [];
for (let j = 0; j < columns.length; j++) {
const column = columns[j]
row.push(source.data[column][i].toString())
}
lines.push(row.join(','))
}
return lines.join('\\n').concat('\\n')
}
var filename = 'results.csv';
var blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
"""
button_1.js_on_event('tap', CustomJS(args=dict(source=s3), code=download_javaScript))
layout = column(p, div, Column(ti), Column(button), Column(table), Column(button_1))
show(layout)