2017-11-27 1 views
0

Select 위젯을 사용하여 내 DataTable 위젯 값을 업데이트하려면 어떻게해야합니까? 여기Python bokeh CustomJS 콜백 업데이트 DataTable 위젯

import pandas as pd 
from bokeh.io import show 
from bokeh.layout import column 
from bokeh.models import ColumnDataSource, CustomJS, Select 
from bokeh.models.widgets import DataTable, TableColumn 

df = pd.DataFrame({'a': range(10,50), 'b': range(110,150)}) 

source_foo = ColumnDataSource(data=df.loc[df['a'] < 25]) 
source_bar = ColumnDataSource(data=df.loc[df['a'] > 25]) 
source_fill = ColumnDataSource(data=df.loc[df['a'] < 25]) 

table_columns = [TableColumn(field=i, title=i) for i in ['a', 'b']] 

select = Select(title='Selected value:', value='foo', options=['foo', 'bar']) 

update = CustomJS(args=dict(source_fill=source_fill, source_foo=source_foo, 
     source_bar=source_bar), code=""" 

    var data_foo = source_foo.data; 
    var data_bar = source_bar.data; 
    var data_fill = source_fill.data; 
    var f = cb_obj.value; 
    var list = ['a', 'b'] 

    if (f == 'foo') { 
     for(var i = 0, size = list.length; i < size ; i++) { 
      var e = list[i]; 
      delete data_fill[e]; 
      data_fill[e] = data_foo[e]; 
     } 
    } 
    if (f == 'bar') { 
     for(var i = 0, size = list.length; i < size ; i++) { 
      var e = list[i]; 
      delete data_fill[e]; 
      data_fill[e] = data_bar[e]; 
     } 
    } 

    source_fill.change.emit(); 
    """) 

select.js_on_change('value', update) 

data_table = DataTable(source=source_fill, columns=table_columns, width=150, 
    height=300, row_headers=False, selectable=False) 

layout = column(select, data_table) 

bio.show(layout) 

데이터 값이 selectable=False 경우 변경되지 않습니다 여기 내 예제 코드입니다. selectable=True으로 설정하면 첫 번째 행이 새로 고쳐집니다. DataTable (selectable에 관계없이) 중 하나의 열을 재정렬하면 값이 새로 고쳐집니다. 상쾌하게하는 것이 자동으로 어떻게 강제 될 수 있습니까?

감사합니다.

답변

2

당신은 새로운 데이터에 source_fill.data 포인터를 만들 수 있습니다

if (f == 'foo') { source_fill.data = source_foo.data; } if (f == 'bar') { source_fill.data = source_bar.data; }

+0

가 대단히 감사합니다 !!!! 그것은 완벽하게 작동합니다! 나는'if (f == 'foo') {data_fill = data_foo} ...를 시도했지만, 효과가 없다. – ragesz