尝试将 Bokeh 示例重写为独立文档,但不确定如何将小部件添加到 html 文件。
使用“file_html”我可以创建一个 html 字符串,但如果我还想保存和添加滑块、按钮等小部件该怎么办?
from bokeh.models.widgets import *
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
def bkapp():
df = sea_surface_temperature.copy()
source = ColumnDataSource(data=df)
plot = figure(x_axis_type='datetime', y_range=(0, 25), y_axis_label='Temperature (Celsius)',
title="Sea Surface Temperature at 43.18, -70.43")
plot.line('time', 'temperature', source=source)
def callback(attr, old, new):
if new == 0:
data = df
else:
data = df.rolling('{0}D'.format(new)).mean()
source.data = ColumnDataSource(data=data).data
slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
slider.on_change('value', callback)
html = file_html(plot, CDN, "myplot")
with open('my-plot.html','w') as file:
file.write(html)