使用散景小部件的新值进行交互式 python 绘图

时间:2021-01-15 23:39:46

标签: python bokeh databricks

我需要使用我们从 Bokeh 小部件接收到的值来运行一些代码并绘制新生成的数据。我无法修改进入 CustomJS 的 javascript 中的数据,因为在 python 中没有可用的库。我知道我可以使用散景服务器并运行 python 回调,但这必须发生在 Databricks 笔记本中。这可能吗?

由于我无法公开我正在处理的代码,我准备了一个示例代码,希望能传达我想要做的事情。

import numpy as np

from bokeh.io import curdoc, show
from bokeh.models import ColumnDataSource, Grid, LinearAxis, Patches, Plot, Rect
from bokeh.embed import file_html
from bokeh.plotting import figure, output_file, show
from bokeh.resources import CDN


x = [x*0.005 for x in range(0, 200)]
y = x

source = ColumnDataSource(data=dict(x=x, y=y))
button_cds = ColumnDataSource(data=dict(button_val=[1]))
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(button_cds = button_cds), code="""
        var data = button_cds.data;
        var f = cb_obj.value;
        data['button_val'] = f;
        button_cds.change.emit();
    """)

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power")
slider.js_on_change('value', callback)
for i in range(len(source.data['x'])):
  source.data['y'][i] = x[i]**button_cds.data['button_val'][0]

layout = column(slider, plot)

displayHTML(file_html(layout, CDN))

请注意,我可以修改 CustomJS 中的 source.data,但我必须使用 python 代码。

0 个答案:

没有答案