這裏的背景虛化的服務器解決方案:
from bokeh.layouts import layout
from bokeh.io import curdoc
from bokeh.models.widgets import AutocompleteInput
from bokeh.models.widgets import (Panel, Tabs, DataTable, TableColumn,
Paragraph, Slider, Div, Button, Select)
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool,TapTool
def update_selected(wttr,old,new):
a_val = autocomp.value
names = source.data['names']
ind = [i for i,x in enumerate(names) if x == a_val]
source.selected={'0d': {'glyph': None, 'indices': []}, '1d': {'indices': ind}, '2d': {}}
data_dict = {'names':['test2','test3','hello','goodbye'],
'x':[0,1,2,3], 'y':[10,20,30,40]}
source = ColumnDataSource(data_dict)
autocomp = AutocompleteInput(completions=['test2','test3','hello','goodbye'])
autocomp.on_change('value',update_selected)
fig = figure(plot_width=400,
plot_height=400,
x_axis_label='x',
y_axis_label='y',
tools=["pan","hover","box_zoom","reset,save"])
fig.scatter('x','y',source=source)
layout = layout([[fig, autocomp]])
curdoc().add_root(layout)
編輯:這是使用customJS
from bokeh.layouts import layout
from bokeh.io import curdoc
from bokeh.models.widgets import AutocompleteInput
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models.callbacks import CustomJS
data_dict = {'names':['test2','test3','hello','test3'],
'x':[0,1,2,3], 'y':[10,20,30,40]}
source = ColumnDataSource(data_dict)
fig = figure(plot_width=400,
plot_height=400,
x_axis_label='x',
y_axis_label='y',
tools=["pan","hover","box_zoom","reset,save"])
fig.scatter('x','y',source=source)
autocomp = AutocompleteInput(completions=['test2','test3','hello','goodbye'])
code = """
var autocomplete = cb_obj.value
var names = source.data.names
function getAllIndexes(arr, val) {
var indexes = [], i;
for(i = 0; i < arr.length; i++)
if (arr[i] === val)
indexes.push(i);
return indexes;
}
var index = getAllIndexes(names,autocomplete)
source.selected = {'0d':{indices: [0]}, '1d':{indices: index},'2d':{indices: [0]}}
"""
callback = CustomJS(args={'source':source}, code=code)
autocomp.callback = callback
layout = layout([[fig, autocomp]])
curdoc().add_root(layout)
我加入'節目(版面)'在結束試圖解決方案中的解決方案你的代碼(並添加output_file並顯示到導入),然後運行它。它拉出一個圖和自動完成,但選擇「完成」不會突出顯示數據點。它爲你工作? –
我剛剛嘗試過,它似乎工作正常。你在使用第二個還是第一個代碼?我認爲第一個需要散景服務。第二個我只是嘗試然後鍵入顯示(佈局),它是響應選擇。你還使用什麼瀏覽器?我只在鉻合金中測試過它 – Anthonydouc
是的,我也使用Chrome。我簡直就是將代碼複製到文本編輯器中,將行改爲'from bokeh.plotting import figure,ColumnDataSource,output_file,show',並在最後添加'show(layout)'。然後,我使用python 3.5從終端運行該文件,並在Chrome中拉出該圖。自動完成工作(雖然不是很好,沒有將jquery ui片段添加到html文檔的頭部),但選擇完成對圖形沒有任何影響...我仍然對Bokeh不熟悉,但希望我不會忘記其他一些步驟 –