如何使用小部件文本框中键入的单词搜索数据帧,然后使用python ipywidgets显示搜索结果?

如何使用小部件文本框中键入的单词搜索数据帧,然后使用python ipywidgets显示搜索结果?,python,search,widget,jupyter,interaction,Python,Search,Widget,Jupyter,Interaction,我正在学习Python和Jupyter中的小部件交互。我的任务是: t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back'], 'number':[1,3,2,3,1,2], 'word':['a','haha','runing over there',

我正在学习Python和Jupyter中的小部件交互。我的任务是:

t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back'],
                'number':[1,3,2,3,1,2],
                'word':['a','haha','runing over there','abcdefg','aaa','bye']})

import ipywidgets as widgets
from IPython.display import display

widgets.Text(
    value='Hello World',
    placeholder='Type something',
    description='keyword:',
    disabled=False
)

我需要输入一些单词,例如“live”,然后代码将自动搜索数据帧t并显示其中包含live的所有行


我在寻找一些提示,因为我不知道从哪里开始。

最后想出一个简单的例子。把它放在这里给可能需要它的人

t=pd.DataFrame({'string':['i live here','you live in eltham','machine learning','learning english','go home','go back','live home'],
                'number':[1,3,2,3,1,2,4],
                'word':['a','haha','runing over there','abcdefg','aaa','bye','hou']})

def myFUN_searchString(value,string):
    s=string.split(' ')
    return value in s

def myFUN_search(value):
    t.loc[:,'Flag']=''
    t.loc[:,'Flag']=[myFUN_searchString(value,x) for x in t.loc[:,'string']]
    return t.loc[:,'Flag']

import ipywidgets as widgets
from IPython.display import display

keyword=widgets.Text(
    value='electricity',
    placeholder='Type something',
    description='keyword:',
    disabled=False
)
display(keyword)


button = widgets.Button(description="search")
display(button)

output = widgets.Output()

@output.capture()
def on_button_clicked(b):
    t.loc[:,'Flag']=myFUN_search(keyword.value)
    t1=t.loc[(t['Flag'])]
    t1.drop(['Flag'],axis=1,inplace=True)
    t1.reset_index(drop=True,inplace=True)
    if t1.shape[0]>30:
        t1=t1.loc[0:30]

    display(t1)

button.on_click(on_button_clicked)
display(output)