我有一个django应用程序,它最终使用嵌入式bokeh可视化。
现在,我使用了bokeh.embed.components函数和一个模板,如:
<body>
{{the_div|safe}}
{{the_script|safe}}
</body>多亏了这个堆叠溢出问题。
问题是,现在我需要创建更多的交互式可视化,添加滑块、复选框和其他控件。
这个例子看起来就像我想要的,除了几个问题:
因此,总之,我想知道使用django和bokeh创建动态图表交互的标准方法是什么。
发布于 2016-05-12 15:25:11
有两个用例:
没有服务器的
如果您可以在JS中执行任何更新(不需要调用实际的python代码),那么使用CustomJS回调添加交互非常容易。该链接中有很多示例,但基本的简单代码示例如下所示:
from bokeh.io import vform
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import Figure, output_file, show
output_file("callback.html")
x = [x*0.005 for x in range(0, 200)]
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=dict(source=source), code="""
var data = source.get('data');
var f = cb_obj.get('value')
x = data['x']
y = data['y']
for (i = 0; i < x.length; i++) {
y[i] = Math.pow(x[i], f)
}
source.trigger('change');
""")
slider = Slider(start=0.1, end=4, value=1, step=.1,
title="power", callback=callback)
layout = vform(slider, plot)
show(layout)这将创建一个具有Bokeh绘图和滑块的独立HTML文档,该文档可以根据滑块更新绘图,而不需要服务器(也就是说,您可以将其发送给某人或在静态页面上提供服务,这样就可以工作了)。
与服务器
如果您希望小部件、交互等驱动实际的python代码(例如,scikit-learn或Pandas),那么您需要使用Bokeh服务器。令人高兴的是,0.11版本的新服务器更加健壮、性能更好、可伸缩,而且使用起来更简单。您可以在这里看到几个实时部署的Bokeh应用程序(有指向它们的源代码的链接):
http://demo.bokeh.org/
以及文档中有关各种部署的广泛文档,在这里:
指南/server.html
https://stackoverflow.com/questions/31585777
复制相似问题