我正在使用 Jupyter Notebook,并试图在您输入字符时获得使用文本框的建议。我发现的一个很好的例子是 here.
我想在用户输入字符时给出建议,因此建议列表会随着输入的每个字符而改变。
我是 Jupyter 的新手,并且仍在尝试学习所有功能。我需要依赖基本的 Jupyter 工具,例如 ipython 小部件(无法安装其他软件包)。任何帮助将非常感激。
最佳答案
您是否尝试过为此使用 bokeh 的 AutocompleteInput
?
查看以下示例:
Access data from bokeh widgets in a jupyter notebook
from bokeh.models.widgets.inputs import AutocompleteInput
from bokeh.io import output_notebook
from bokeh.plotting import show
from bokeh.models import CustomJS
output_notebook()
callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
var kernel = IPython.notebook.kernel;
cmd = "widget_value = '" + cb_obj.value + "'";
kernel.execute(cmd, {}, {});
}
""")
txt_input = AutocompleteInput(completions=['val1', 'val2'], callback=callback)
show(txt_input)
print(widget_value)
编辑:我刚刚看到您无法安装软件包。但是,上述答案需要安装
bokeh
。