2017-01-13 157 views
3

幻灯片通过thisBokeh gallery的例子,我试图实现一个滑块滑动浏览大量的收集数据(本质上是一个生物数据的时间推移)。我没有在滑块上使用自定义JavaScript回调,而是尝试使用这些小部件。我不知道这是否可行。找到我最小的工作示例。它正确显示滑块和图像,但看起来更新不会发生。幻灯片通过与幻灯片回调的散景滑块

##Creating the 15 different pictures 
#Want to make 15 different pictures of a certain field function evaluated on a grid of img_size_x x img_size_y 
import numpy as np 
img_size_x,img_size_y = (512,512) 
variations=15 

#Make the field 
xx,yy=np.meshgrid(np.arange(img_size_x),np.arange(img_size_y)) 

#Broadcast the field into as many copies as there are variations to make use of the ufuncs 
xx= np.tile(xx,variations).reshape(variations,img_size_x,img_size_y) 
yy= np.asarray(map(np.transpose,np.tile(yy.T,variations).reshape(variations,img_size_x,img_size_y))) 

varied_parameter=np.linspace(.01,0.5,variations) #frequencies of a sin/cos function, for example 
varied_parameter=np.repeat(varied_parameter,img_size_x*img_size_y).reshape(variations,img_size_x,img_size_y) #broadcast 

matrix_images=np.cos(varied_parameter*xx)+np.sin(varied_parameter*yy) # field function evaluated for diff frequencies. 

##Creation of the Bokeh interface to slide through these pictures 
from bokeh.plotting import figure, show, output_file, output_notebook 
from bokeh.models import ColumnDataSource 
from bokeh.layouts import row, widgetbox 
from bokeh.models.widgets import Slider 
import bokeh.palettes as pal 
output_notebook() 

data=matrix_images[0] #starting value for the column data source 
source = ColumnDataSource(dict(image=[data])) #the figure.image function takes a vector of matrices 
image_sl = Slider(title="Image number", value=0, start=0, end=variations-1, step=1) #slider to go through the images 
def update_img(attrname, old, new): 
    curr_value = image_sl.value 
    x=matrix_images[int(curr_value)] #make sure index is int to select image number 'curr_value' 
    source.data = dict(image=[x]) 

image_sl.on_change('value', update_img) #give slider its callback function 
inputs = widgetbox(image_sl) #wrap the slider into a display object 


p = figure(x_range=(0, 10), y_range=(0, 10)) 
# must give a vector of image data for image parameter 
p.image('image', source=source,x=0, y=0, dw=10, dh=10, palette=pal.Greys256) 

show(row([p,image_sl])) # open a browser 

回答

0

您尝试使用的更新类型只适用于Bokeh Server。当output_notebookoutput_fileshow一起使用时,生成的输出是带有嵌入式JavaScript的HTML,它呈现实际绘图。这意味着这些图应该被认为是在浏览器中运行的独立文件。这意味着这些图不能直接访问或运行任何Python代码。

Bokeh为您提供了几种方式来为Python中的图绘制回调。首先是使用Bokeh Server。你可以阅读关于它here。既然你已经写好你的回调来与Bokeh服务器一起工作,那么以这种方式工作是非常容易的。我注释掉了这些行,这会导致错误。

xx= np.tile(xx,variations).reshape(... 
yy= np.asarray(map(np.transpose, ... 

然后加入进口用于curdoc from bokeh.io import curdoc并用curdoc().add_root(row([p,image_ls]))替换show(row([p,image_ls]))。从那里可以用$ bokeh serve --show name_of_your_file.py运行该示例。

如果你想要在笔记本或独立文件中运行的东西。您也可以选择使用PyScript回调。这个回调看起来像python,它和你的Python代码一样写在同一个文件中。但是,它将被解释为一种称为PyScript的语言,以将您的“Python代码”编译为JavaAcript。再次,这将无法访问您的Python运行时环境。可以将Bokeh对象传递给这些回调,但是,就是这样。这可能不适合您的用例,因为您的回调需要访问matrix_images。你可以阅读更多here。我建议阅读关于添加交互的整个部分,它有很多关于如何使用CustomJS回调的很好的例子。

另一种选择是使用Jupyter Notebook Widgets。这个解决方案只能在笔记本上运行。你可以阅读这个方法here