5
我想使用Bokeh中的切换按钮来创建一个交互式网站,用户可以点击切换按钮来选择绘制哪些图。加载图形数据从文件按钮点击与景
这些按钮可以从文本文件(包含两列x和y数据)加载数据。数据文件有两列包含由空格分隔的x和y数据。
当选择切换按钮时,将绘制相应的数据,当取消切换按钮时将删除该图。
我目前无法将参数传递给回调事件,有可能吗?
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Toggle
from bokeh.plotting import figure, output_file, show
output_file("load_data_buttons.html")
x = [0]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=filename,dict(source=source), code="""
var data = source.get('data');
console.log(filename)
x = data['x']
y = data['y']
#load data stored in the file name and assign to x and y
source.trigger('change');
""")
toggle1 = Toggle(label="Load data file 1", type="success",callback=callback("data_file_1.txt"))
toggle2 = Toggle(label="Load data file 2", type="success",callback=callback("data_file_2.txt"))
layout = vform(toggle1, toggle2, plot)
show(layout)
感谢您的回答HYRY。这真的很不错,唯一的麻烦是我有数百个数据文件导致了很多数据。我不想事先将它预先加载,因为这需要很长时间。你知道点击按钮时加载数据的方法吗? – Jon
然后你需要创建一个javascript函数来解析csv文件,这里是一个例子:http://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript – HYRY
非常感谢,这个答案的组合解决了我的问题。非常感激。 – Jon