如何在下面的例子中移動滑塊時更新python變量extern_python_variable
? extern_python_variable
的值應始終爲f(滑塊的實際位置)。謝謝!如何在散景中的customJS函數中更新全局python變量?
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
extern_python_variable=0
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)
test=source.to_df()
layout = vform(slider, plot)
show(layout)
這段代碼的輸出是一個HTML文件,它包含的JavaScript交互機械。因此,它將在瀏覽器中運行,它將在瀏覽器中運行JavaScript,它不會運行任何Python例程。我想知道你打算用extern_python_variable做什麼?如果你想監視f的值,你可以在瀏覽器中打開一個javascript控制檯,你可以使用console.log(variable_to_monitor)轉儲任何信息。 –
上面的代碼應該只是一個簡單的例子。實際上,我想從python變量中的選擇中保存rect屬性,並且需要這些變量用於進一步的面向數據庫的計算,而這些變量不能在customJS函數中完成。 – justus