Bokeh DataTable多输入/小部件/过滤器

时间:2017-11-24 12:54:35

标签: python bokeh

我正在尝试根据多个过滤器显示表格。

def update():
    current = df[(df['nb_words'] >= slider.value[0]) & (df['nb_words'] <= slider.value[1])].dropna()
    source.data = {
        'id'                   : current.id,
        'author'               : current.author,
        'nb_words'             : current.nb_words,
        'text'                 : current.text,
        'topic'                : current.topic,
        'national'             : current.national
}

nb_words = RangeSlider(title="Min nb employees", start=0, end=1000, value=(0, 1000), step=10, format="0,0")
topic = CheckboxButtonGroup(labels=list(df.topic.unique()))
national = RadioButtonGroup(labels=['Yes', 'No'], active=0)
text = TextInput()

nb_words.on_change('value', lambda attr, old, new: update())

data_table = DataTable(source=source, columns=columns, width=1000, fit_columns=False)
controls = widgetbox(nb_words, button)
table = widgetbox(data_table)

此处更新仅在nb_words的滑块更改时有效 但是,我想允许用户一次多次选择 例如,如果用户选择

行,则表格将充分更新
- 20 <= nb_words <= 200  
- topic = ["topic1", "topic2"]  
- national = 1  
- and text that contains the word "fantastic"

如何使用多个小部件更新表?

1 个答案:

答案 0 :(得分:0)

根据https://demo.bokehplots.com/apps/movies,我必须创建另一个函数select_text()。这是最后的脚本:

nb_words = RangeSlider(title="Min nb words", start=0, end=1000, value=(0,1000), step=10, format="0,0")
text = TextInput(title="Enter a Keyword")

source = ColumnDataSource(data=dict())


def select_text():
    text_value = text.value.strip()
    selected = df[
        (df.nb_words >= slider.value[0]) &
        (df.nb_words <= slider.value[1])
    ]
    if (text_value != ""):
        selected = selected[selected.text.str.lower().str.contains(text_value)==True]
    return selected



def update():
    current = select_text()
    source.data = dict(
                id = current.id,
                author = current.author,
                nb_words = current.nb_words,
                text = current.text,
                topic = current.topic,
                national = current.national,
    )

controls = [nb_words, text]
for control in controls:
    control.on_change('value', lambda attr, old, new: update())


columns = [TableColumn(field="id", title="ID"),
           ...
           TableColumn(field="national", title="National"),
           ]

data_table = DataTable(source=source, columns=columns, width=1000, fit_columns=False)


sizing_mode = 'fixed'
inputs = widgetbox(*controls, sizing_mode = sizing_mode)
table = widgetbox(data_table)

curdoc().add_root(row(inputs, table))
curdoc().title = "Topic Selection"

update()