当另一个选择小部件更改时,如何自动更新下拉选择小部件? (Python 面板 pyviz)
How do i automatically update a dropdown selection widget when another selection widget is changed? (Python panel pyviz)
我有一个 Select 小部件,每当另一个 Select 小部件发生变化时,它应该提供不同的选项列表,因此它会在另一个 Select 小部件发生变化时更新。我如何在下面的示例代码中做到这一点?
_countries = {
'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']
}
continent = pn.widgets.Select(
value='Asia',
options=['Africa', 'Asia', 'Europe']
)
country = pn.widgets.Select(
value=_countries[continent.value][0],
options=_countries[continent.value]
)
@pn.depends(continent.param.value)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
country.value = countries[0]
pn.Row(continent, country)
所以,我花了很长时间才找到这个,但是 在你的@pn.depends() 中你必须添加参数 watch=True,所以它会不断地监听如果发生变化并且应该更新您的其他列表。
在这种情况下:
@pn.depends(continent.param.value, watch=True)
整个例子:
_countries = {
'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']
}
continent = pn.widgets.Select(
value='Asia',
options=['Africa', 'Asia', 'Europe']
)
country = pn.widgets.Select(
value=_countries[continent.value][0],
options=_countries[continent.value]
)
@pn.depends(continent.param.value, watch=True)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
country.value = countries[0]
pn.Row(continent, country)
此页面上的 GoogleMapViewer 示例为我指明了正确的方向:
Selector updates after another selector is changed
相同的答案,但形式为 Class:
class GoogleMapViewer(param.Parameterized):
continent = param.Selector(default='Asia', objects=['Africa', 'Asia', 'Europe'])
country = param.Selector(default='China', objects=['China', 'Thailand', 'Japan'])
_countries = {'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']}
@param.depends('continent', watch=True)
def _update_countries(self):
countries = self._countries[self.continent]
self.param['country'].objects = countries
self.country = countries[0]
viewer = GoogleMapViewer(name='Google Map Viewer')
pn.Row(viewer.param)
我有一个 Select 小部件,每当另一个 Select 小部件发生变化时,它应该提供不同的选项列表,因此它会在另一个 Select 小部件发生变化时更新。我如何在下面的示例代码中做到这一点?
_countries = {
'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']
}
continent = pn.widgets.Select(
value='Asia',
options=['Africa', 'Asia', 'Europe']
)
country = pn.widgets.Select(
value=_countries[continent.value][0],
options=_countries[continent.value]
)
@pn.depends(continent.param.value)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
country.value = countries[0]
pn.Row(continent, country)
所以,我花了很长时间才找到这个,但是 在你的@pn.depends() 中你必须添加参数 watch=True,所以它会不断地监听如果发生变化并且应该更新您的其他列表。
在这种情况下:
@pn.depends(continent.param.value, watch=True)
整个例子:
_countries = {
'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']
}
continent = pn.widgets.Select(
value='Asia',
options=['Africa', 'Asia', 'Europe']
)
country = pn.widgets.Select(
value=_countries[continent.value][0],
options=_countries[continent.value]
)
@pn.depends(continent.param.value, watch=True)
def _update_countries(continent):
countries = _countries[continent]
country.options = countries
country.value = countries[0]
pn.Row(continent, country)
此页面上的 GoogleMapViewer 示例为我指明了正确的方向:
Selector updates after another selector is changed
相同的答案,但形式为 Class:
class GoogleMapViewer(param.Parameterized):
continent = param.Selector(default='Asia', objects=['Africa', 'Asia', 'Europe'])
country = param.Selector(default='China', objects=['China', 'Thailand', 'Japan'])
_countries = {'Africa': ['Ghana', 'Togo', 'South Africa'],
'Asia' : ['China', 'Thailand', 'Japan'],
'Europe': ['Austria', 'Bulgaria', 'Greece']}
@param.depends('continent', watch=True)
def _update_countries(self):
countries = self._countries[self.continent]
self.param['country'].objects = countries
self.country = countries[0]
viewer = GoogleMapViewer(name='Google Map Viewer')
pn.Row(viewer.param)