本文介绍了散景:将vars传递给CustomJS for Widgets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Bokeh的一件好事是,可以从Python层指定回调,从而在javascript级别上产生操作,而无需bokeh-server.因此,无需创建Ipython或Bokeh服务器,就可以创建在浏览器中运行的交互式窗口小部件.

A nice thing about Bokeh is that callbacks can be specified from the Python layer that result actions on the javascript level without the need of bokeh-server. So one can create interactive widgets that run in a browser without an Ipython or Bokeh server running.

0.9.3.文档提供了一个示例,我可以在ipython笔记本中复制该示例: http://docs.bokeh.org/zh_CN/latest/docs/user_guide/interaction.html#cutomjs-for-widgets

The 0.9.3. documentation gives an example that I can reproduce in an ipython notebook:http://docs.bokeh.org/en/latest/docs/user_guide/interaction.html#cutomjs-for-widgets

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)

我想修改这样的代码来创建一些简单的在线任务.我的问题是如何在不调用Slider的情况下将其他变量从python直接传递给javascript.例如,假设我希望Javascript成为:

I want to adapt code like this to create some simple online assignments. My question is how can I pass other variables from python to javascript directly without invoking a Slider. For example suppose I want the Javascript to become:

y[i] = Math.pow(x[i], A*f)

其中A在上面的ipython代码单元中定义(例如A = 10).在javascript中定义'var A = 10'很容易,但是我想在python中设置A和其他变量的值,然后将它们传递到此javascript中.有办法吗?

where the A was defined in an ipython code cell above (for example A = 10). It's easy enough to define 'var A = 10' in the javascript but I'd like to set the value of A and other variables in python and then pass them into this javascript. Is there a way?

推荐答案

从Bokeh 0.9.3开始,您只能传递"Bokeh模型"(例如,数据源和渲染器之类的东西),而不能传递任意的python对象.但是,我们正在使用可轻松镜像的简单名称空间概念来扩展bokeh文档.

As of Bokeh 0.9.3 you can only pass "Bokeh Models" (e.g. things like data sources and renderers), not arbitrary python objects. But we are working on extending bokeh documents with a simple namespace concept that can be mirrored easily.

这篇关于散景:将vars传递给CustomJS for Widgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:13