2017-11-24 1 views
0

여러 필터를 기반으로 테이블을 표시하려고합니다.Bokeh DataTable 다중 입력/위젯/필터

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" 

로 행을 선택하는 경우 예를 들어
이 표는 적절하게 업데이트 할 것입니다?

답변

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()