2015-09-01 13 views
9

Tôi đang cố gắng sử dụng Bokeh để tạo một DataTable có thể chỉnh sửa cập nhật dữ liệu nguồn khi dữ liệu được chỉnh sửa. Tôi bắt đầu với ví dụ DataTable tiêu chuẩn here và biến kwarg có thể chỉnh sửa thành true. Đây là nơi tôi tại địa chỉ:Thêm gọi lại vào Bokeh DataTable

from datetime import date 
from random import randint 

from bokeh.models import ColumnDataSource, Callback 
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn 
from bokeh.io import output_file, output_notebook, show, vform 
output_notebook() 
data = dict(dates=[date(2014, 3, i+1) for i in range(10)], 
      downloads=[randint(0, 100) for i in range(10)]) 

source = ColumnDataSource(data) 

columns = [TableColumn(field="dates", title="Date", formatter=DateFormatter()), 
      TableColumn(field="downloads", title="Downloads")] 

callback = Callback(args=dict(Source=source), code=""" 
     console.log('#cell edited')""") 

data_table = DataTable(source=source, columns=columns, width=400, height=280, editable=True) 
data_table.on_change(callback,source) 
show(vform(data_table)) 

Điều này làm cho một bảng dữ liệu có thể chỉnh sửa, nhưng tôi không thể tìm ra cách để có được gọi lại để cập nhật các nguồn dữ liệu, hoặc để cấu hình nguồn dữ liệu để nó tự động thực hiện cái đó. Tôi nghĩ rằng có một cách để tự động làm điều đó với ColumnDataSource, và sau khi cố gắng mà cố gắng để viết một cuộc gọi lại. Tuy nhiên nó xuất hiện DataTable không có một tùy chọn gọi lại, nhưng nó kỳ quặc có một thuộc tính on_change.

Có ai biết cách thực hiện việc này không?

Trả lời

2

Mã sau sẽ phát hiện sự kiện nhấp (chọn) một hàng hoặc hàng. Tôi đặt một số console.log để xuất các hàng được chọn.

from datetime import date 
from random import randint 
import bokeh 
import bokeh.plotting 

data = dict(dates=[date(2014, 3, i+1) for i in range(10)], 
      downloads=[randint(0, 100) for i in range(10)]) 

source = bokeh.models.ColumnDataSource(data) 

columns = [bokeh.models.TableColumn(field="dates", title="Date", 
      formatter=bokeh.models.DateFormatter()), 
      bokeh.models.TableColumn(field="downloads", title="Downloads")] 

source.callback = bokeh.models.CustomJS(args=dict(source=source), code=""" 
     console.log('#Selected rows:'); 
     var indices = source.selected["1d"].indices; 
     for (var i = 0; i<indices.length; i++){ 
      console.log(i+":"+indices[i]); 
     } 
     """) 

data_table = bokeh.models.DataTable(source=source, columns=columns, 
            width=400, height=280, editable=True) 

bokeh.io.output_notebook() 
bokeh.io.show(data_table) 
Các vấn đề liên quan