gpt4 book ai didi

python - Jupyter 笔记本小部件 : How to make "Filter As You Type" Textbox?

转载 作者:行者123 更新时间:2023-12-05 07:13:13 24 4
gpt4 key购买 nike

我正在尝试在我的 Jupyter Notebook 中制作一个“键入时过滤”文本框小部件,如果我在其中写入 M,它会显示所有带有“M”的玩家...如果我在 M 之后键入 O,它会显示带有他们名字中的“MO”(例如 MOHAMED SALAH 和 MO SALAH)等等。下面的代码工作得很好。我遇到的唯一问题是它一直附加到单元格结果...所以如果我写 3 个字母,它会显示 3 个表格。我想清除以前的输出,以便始终只获得最新的结果。我该怎么做?

import pandas as pd, IPython.display, ipywidgets as widgets 
out = widgets.Output()
display(out)

df = pd.DataFrame ({'PLAYER':['MOHAMED SALAH', 'MESSI', 'MO SALAH', 'RONALDO']})
display(df)

textbox = widgets.Text(value='', description='Player:')
display(textbox)

def display_result(value):
value = str(value['new']).upper()
if "{" not in value:
print(value)
result = df[(df['PLAYER'].str.contains(value))]
if result.shape[0]>0: display(result)


textbox.observe(display_result)

输出: enter image description here

最佳答案

您只是在函数中缺少对 out.clear_output() 的调用!

import pandas as pd, IPython.display, ipywidgets as widgets 
out = widgets.Output()
display(out)

df = pd.DataFrame ({'PLAYER':['MOHAMED SALAH', 'MESSI', 'MO SALAH', 'RONALDO']})

textbox = widgets.Text(value='', description='Player:')
display(textbox)

def display_result(value):
value = str(value['new']).upper()
if "{" not in value:
result = df[(df['PLAYER'].str.contains(value))]
out.clear_output()
if result.shape[0]>0:
with out:
display(result)


textbox.observe(display_result)

关于python - Jupyter 笔记本小部件 : How to make "Filter As You Type" Textbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com