与排列在 HBox 中的 SelectMultiple 小部件的交互功能
Interactive function with SelectMultiple widgets arranged in a HBox
我想用 Jupyter notebook 构建一个交互式应用程序,但我还没有太多使用小部件的经验。
在我的代码中,我定义了 2 个 SelectMultiple 小部件(参见下面代码中的定义)。
我使用例如 'HBox'(参见代码)将 2 个小部件组合在特定布局中。
我还定义了一个函数,该函数基于这两个小部件在数据库中搜索的值和 returns 一个等于数据库中所选元素数量的整数(请参见下面的代码)。
我想打印函数 "No_selected" 的输出,以便当我在 HBox 布局中显示的 2 个 SelectMultiple 小部件之一中选择不同的输入时,它会交互更改。
我已尝试阅读小部件的文档 (https://ipywidgets.readthedocs.io/en/stable/user_guide.html)。
我曾尝试在该函数之前使用 Interact 装饰器“@Interact”,但是当我这样做时,Jupyter 会为 W1 和 W2 显示两个额外的小部件,而不是允许我 运行 通过使用这两个小部件来实现该功能HBox 中的小部件。
W1 的小部件定义示例(W2 的类似):
W1 = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears'],
value=['Oranges'],
#rows=10,
description='Fruits',
disabled=False
)
布局示例:
Wboth = widgets.HBox([W1, W2])
函数定义示例:
def SELECTION(W1=('Apples'), W2=('Apples')):
...
...
...
return No_selected
你能建议我怎么做吗?
我认为您需要比这里的标准 interact
更复杂的东西,因为您需要从不仅仅是要更改的小部件中获取信息。
我已经整理了您想要的行为的 class
版本,我发现自己使用 observe
的次数比 interact
多得多,现在我对组合和子类化更加熟悉。您可以更改 _observed_function 以使用当前的小部件选择执行您需要的操作。
import ipywidgets as widgets
class SelectMultipleInteract(widgets.HBox):
def __init__(self):
self.W1 = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears'],
value=['Oranges'],
#rows=10,
description='Fruits',
disabled=False
)
self.W2 = widgets.SelectMultiple(
options=['Carrots', 'Potatoes', 'Tomatoes'],
value=['Carrots'],
#rows=10,
description='Veg',
disabled=False
)
self.selectors = [self.W1, self.W2]
super().__init__(children=self.selectors)
self._set_observes()
def _set_observes(self):
for widg in self.selectors:
widg.observe(self._observed_function, names='value')
def _observed_function(self, widg):
for widg in self.selectors:
print(widg.description)
print(widg.get_interact_value())
SelectMultipleInteract()
我想用 Jupyter notebook 构建一个交互式应用程序,但我还没有太多使用小部件的经验。
在我的代码中,我定义了 2 个 SelectMultiple 小部件(参见下面代码中的定义)。
我使用例如 'HBox'(参见代码)将 2 个小部件组合在特定布局中。
我还定义了一个函数,该函数基于这两个小部件在数据库中搜索的值和 returns 一个等于数据库中所选元素数量的整数(请参见下面的代码)。
我想打印函数 "No_selected" 的输出,以便当我在 HBox 布局中显示的 2 个 SelectMultiple 小部件之一中选择不同的输入时,它会交互更改。
我已尝试阅读小部件的文档 (https://ipywidgets.readthedocs.io/en/stable/user_guide.html)。
我曾尝试在该函数之前使用 Interact 装饰器“@Interact”,但是当我这样做时,Jupyter 会为 W1 和 W2 显示两个额外的小部件,而不是允许我 运行 通过使用这两个小部件来实现该功能HBox 中的小部件。
W1 的小部件定义示例(W2 的类似):
W1 = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears'],
value=['Oranges'],
#rows=10,
description='Fruits',
disabled=False
)
布局示例:
Wboth = widgets.HBox([W1, W2])
函数定义示例:
def SELECTION(W1=('Apples'), W2=('Apples')):
...
...
...
return No_selected
你能建议我怎么做吗?
我认为您需要比这里的标准 interact
更复杂的东西,因为您需要从不仅仅是要更改的小部件中获取信息。
我已经整理了您想要的行为的 class
版本,我发现自己使用 observe
的次数比 interact
多得多,现在我对组合和子类化更加熟悉。您可以更改 _observed_function 以使用当前的小部件选择执行您需要的操作。
import ipywidgets as widgets
class SelectMultipleInteract(widgets.HBox):
def __init__(self):
self.W1 = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears'],
value=['Oranges'],
#rows=10,
description='Fruits',
disabled=False
)
self.W2 = widgets.SelectMultiple(
options=['Carrots', 'Potatoes', 'Tomatoes'],
value=['Carrots'],
#rows=10,
description='Veg',
disabled=False
)
self.selectors = [self.W1, self.W2]
super().__init__(children=self.selectors)
self._set_observes()
def _set_observes(self):
for widg in self.selectors:
widg.observe(self._observed_function, names='value')
def _observed_function(self, widg):
for widg in self.selectors:
print(widg.description)
print(widg.get_interact_value())
SelectMultipleInteract()