I want to do something like this. I create a few widgets, for example a dropdown list , a multiselect box and a text input box. Then I add a button. When users select the variables in the widgets and click the button. The values they have selected will be downloaded as txt files. I want this html to be usable without bokeh server so I would appreciate if someone can get it done through customJS.
Really appreciate it.
from random import random
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import CustomJS, Button
from bokeh.layouts import row, column
savebutton = Button(label="Save", button_type="success")
savebutton.callback = CustomJS(args=dict(multi_select1.value,multi_select2.value), code="""
var value1 = multi_select1.value;
var value2 = multi_select1.value;
var out = "";
for (i = 0; i < value1.length; i++) {
out += value1[i];
}
for (i = 0; i < value2.length; i++) {
out += value2[i];
}
var file = new Blob([out], {type: 'text/plain'});
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(file);
elem.download = 'selected-data.txt';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
""")
assets=['asset1','asset2','asset3','asset4']
multi_select1 = MultiSelect(title="Select:", value=['asset1'],options=assets, height=200, width=100)
multi_select2 = MultiSelect(title="Select:", value=['asset1'],options=assets, height=200, width=100)
plot = column(multi_select1,multi_select2,savebutton)
show(plot)
Ok, looks like i figured it out by myself. Hope it helps for other people.
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import CustomJS, Button
from bokeh.layouts import row, column
x = ['asset1']
source= ColumnDataSource(data=dict(x=x))
callback = CustomJS(args=dict(source=source), code="""
var data = source.data;
var input = cb_obj.value;
data['x']=input;
source.change.emit();
""")
assets=['asset1','asset2','asset3','asset4']
multi_select1 = MultiSelect(title="Select:", value=['asset1'],options=assets, height=200, width=100)
multi_select1.js_on_change('value', callback)
savebutton = Button(label="Save", button_type="success")
savebutton.callback = CustomJS(args=dict(source=source), code="""
var data = source.data;
value1=data['x'];
var out = "";
for (i = 0; i < value1.length; i++) {
out += value1[i];
}
var file = new Blob([out], {type: 'text/plain'});
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(file);
elem.download = 'selected-data.txt';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
""")
plot = column(multi_select1,savebutton)
show(plot)
I updated the version above, but I can't get it to work (no txt file created...)
Does anyone have an idea what I'm doing wrong.
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import CustomJS, Button, MultiSelect
from bokeh.layouts import row, column
from bokeh.models.callbacks import CustomJS
x = ['asset1']
source= ColumnDataSource(data=dict(x=x))
callback1 = CustomJS(args=dict(source=source), code="""
var data = source.data;
var input = cb_obj.value;
data['x']=input;
source.change.emit();
""")
assets=['asset1','asset2','asset3','asset4']
multi_select1 = MultiSelect(title="Select:", value=['asset1'],options=assets, height=200, width=100)
multi_select1.js_on_change('value', callback1)
savebutton = Button(label="Save", button_type="success")
callback2 = CustomJS(args=dict(source=source), code="""
var data = source.data;
value1=data['x'];
var out = "";
for (i = 0; i < value1.length; i++) {
out += value1[i];
}
var file = new Blob([out], {type: 'text/plain'});
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(file);
elem.download = 'selected-data.txt';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
""")
savebutton.js_on_click(callback2)
plot = column(multi_select1,savebutton)
show(plot)
Sorry to add that on an old post
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.