如何强制我的Bokeh DataTable进行排序?

时间:2017-08-01 10:10:02

标签: python python-3.x bokeh

我有一个使用bokeh serve渲染的Bokeh DataTable,我想让它开始排序而无需用户点击列标题。我想对行进行排序,使生日日期按升序排列。我目前的代码如下:

from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn, DateFormatter
from bokeh.plotting import curdoc
from pandas import DataFrame

import datetime

# data is usually fetched from somewhere else but this is the same format
data = [
    ('dan', datetime.datetime(2017, 1, 16, 21, 30)),\
    ('joel', datetime.datetime(2016, 2, 21, 21, 30)),\
    ('jeff', datetime.datetime(2017, 10, 2, 21, 30))]

ne = DataFrame()
ne['Name'] = [x[0] for x in data]
ne['Birthday'] = [x[1] for x in data]

source = ColumnDataSource(ne)

columns = [
    TableColumn(field='Name', title='Name'),
    TableColumn(field='Birthday', title='Birthday', formatter=DateFormatter())
]

dt = DataTable(source=source, columns=columns)

curdoc().add_root(column(dt))

1 个答案:

答案 0 :(得分:2)

不知道这是否是最佳方法,但我通过添加以下行预先排序DataFrame来获得您正在寻找的结果

ne.sort_values('Birthday', inplace=True)

之前

source = ColumnDataSource(ne)