2017-09-05 114 views
1

我是散景的初學者,我想知道是否有可能在HTML文件中保存curdoc()?我們可以採取如下我的小測試的例子:用散景保存一個curdoc

import numpy as np from bokeh.palettes 
import RdYlGn11 as palette from bokeh.plotting 
import figure from bokeh.layouts 
import row, widgetbox from bokeh.models 
import ColumnDataSource from bokeh.models.widgets 
import Slider, TextInput, Select from bokeh.io 
import curdoc, output_file, save, set_curdoc 
x = np.array([-10., -8., -7., -2., 0., 1., 2., 5., 7., 9.]) 
y = np.array([-15., -12., -9., -5., 1., 4., 6., 7., 9., 12.]) 
def f(x, y, a, b): 
    return a * x + b * y 
z = f(x, y, -2, 3.2) 
colors = np.array(palette) 
task_color = np.arange(-10, 10, 10) 
source = ColumnDataSource(data = dict(X = x, Y = y, Z = z, color = colors[task_color.searchsorted(z)])) 
plot = figure(plot_height = 800, plot_width = 800) 
plot.circle("X", "Y", size = 10, line_color = "color", fill_color = "color", source = source) 
plot.text("X", "Y", text = "Z", source = source) 
A = Slider(title = "First coefficient", value = -2., start = -10, end = 10, step = 1.) 
B = Slider(title = "Second coefficient", value = 3.2, start = 0., end = 5., step = 0.1) 
def update_data(attrname, ols, new): 
    a = A.value 
b = B.value 
x = np.array([-10., -8., -7., -2., 0., 1., 2., 5., 7., 9.]) 
y = np.array([-15., -12., -9., -5., 1., 4., 6., 7., 9., 12.]) 
z = f(x, y, a, b) 
source.data = dict(X = x, Y = y, Z = z, color = colors[task_color.searchsorted(z)]) 
for w in [A, B]: 
    w.on_change('value', update_data) 
inputs = widgetbox(A, B) 
curdoc().add_root(row(inputs, plot)) 
curdoc().title = "My test" 

回答

2

這是一個散景應用爲了功能,它不能被保存,它必須是執行由散景服務器。即,如果上面的代碼是在一個文件app.py,那麼只有這樣,才能得到它的功能和執行是在你的命令行中運行

bokeh serve --show app.py 

。沒有辦法「保存」它,以便它將運行而不需要 Bokeh服務器。除了腳本以外,還有其他一些指定和構建應用程序的方法,但要運行,它們都必須在Bokeh服務器中運行。請參閱:

http://bokeh.pydata.org/en/latest/docs/user_guide/server.html

Alernatively,如果你不需要在你的回調執行真正的,實際的Python代碼,你可以有很多高度互動的獨立(即無背景虛化服務器)地塊的東西像CustomJS回調。參見:

http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/widgets.html#userguide-interaction-widgets