Hi,
I tried to replicate this example https://demo.bokehplots.com/apps/export_csv
When I try it, nothing happens when I click the button. I copypasted the “download.js” from here bokeh/download.js at branch-3.0 · bokeh/bokeh · GitHub
From here I just had to do minor modification on “filetext” and “currRow”.
Here is the code I used, is anything wrong with the callback?
from bokeh.plotting import Figure, output_file
from bokeh.models import CustomJS, ColumnDataSource, Button
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import gridplot
from bokeh.resources import CDN
from bokeh.embed import file_html
columns = [
TableColumn(field='x',title='Site'),
TableColumn(field='y',title='Latitude'),
]
table_source = ColumnDataSource( data = {‘x’:[1,2,3],‘y’:[4,5,6]})
data_table = DataTable(source=table_source, columns=columns)
download_button = Button(label=‘Save Table to CSV’, button_type = “success”)
download_button.callback = CustomJS(args=dict(source=table_source),code="""
var data = source.data;
var filetext = ‘x,y\n’;
for (i=0; i < data[‘x’].length; i++) {
var currRow = [data[‘x’][i].toString(),
data[‘y’][i].toString().concat(’\n’)];
var joined = currRow.join();
filetext = filetext.concat(joined);
}
var filename = ‘data_result.csv’;
var blob = new Blob([filetext], { type: ‘text/csv;charset=utf-8;’ });
//addresses IE
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
}
else {
var link = document.createElement(“a”);
link = document.createElement(‘a’)
link.href = URL.createObjectURL(blob);
link.download = filename
link.target = “_blank”;
link.style.visibility = ‘hidden’;
link.dispatchEvent(new MouseEvent(‘click’))
}
“”")
grid = gridplot([[download_button],[data_table]])
outfile=open(‘exportest.html’,‘w’)
outfile.write(file_html(grid,CDN,‘exportest’))
outfile.close()
``