散景单击按钮,使用javascript将小部件值保存到txt文件 [英] Bokeh click button to save widget values to txt file using javascript
本文介绍了散景单击按钮,使用javascript将小部件值保存到txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的事情.我创建了一些小部件,例如下拉列表,多选框和文本输入框.然后我添加一个按钮.当用户在小部件中选择变量并单击按钮时.他们选择的值将下载为txt文件.我希望此html在没有bokeh服务器的情况下可以使用,因此,如果有人可以通过customJS完成此操作,我将不胜感激.
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.
真的很感激.
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)
这篇关于散景单击按钮,使用javascript将小部件值保存到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文