如何对 ipywidgets 交互式小部件(滑块)中的键名进行排序?
How to sort key names in ipywidgets interactive widgets (slider)?
我正在尝试在 jupyter 笔记本上使用 ipywidgets 制作交互式滑块,以在用户更改滑块小部件时更改绘图的数据,这很简单,我们可以轻松找到示例代码。我遇到的问题是双重的:1)当我的函数中有很多参数(=变量,滑块)要显示时,滑块是垂直排列的,因此如果不滚动 jupyter 页面就很难控制它们。有没有办法按照我的意愿排列滑块,比如 m × n 网格? 2) 为了传递大量 integer-/float-valued 滑块,我制作了一个字典以传递给交互函数。在这里,关键 (=slider/variable/parameter) 名称似乎以随机顺序显示。我尝试在预先按键名排序后通过字典,但它仍然没有解决问题。
如果您能分享任何想法,我将不胜感激。
def myfun(**var_dict):
v = [value for value in var_dict.values()]
return np.sum(v)
var_dict = {'var1':1,'var2':2,'var3':3,'var4':4}
w = interactive(myfun,**var_dict)
display(w)
您将无法使用 **kwargs 解决此问题。如 PEP468 所述
"The only reason they [keyword arguments] don't work is because the interpreter throws that ordering information away."
"Starting in version 3.5 Python will preserve the order of keyword arguments as passed to a function"
因此,如果您想获得此行为,您应该:
使用 'interactive':
时命名你的参数
from ipywidgets import interactive
from IPython.display import display
import numpy as np
def myfun(**kwargs):
return np.sum(list(kwargs.itervalues()))
w=interactive(myfun,var1=1,var2=2,var3=3,var4=4)
display(w)
或者,如果你真的想使用字典,据我所知,最好是自己构建小部件,而不使用 'interactive'。
你可以这样做:
from IPython.display import display,clear_output
from ipywidgets import widgets
from collections import OrderedDict
var_dict = OrderedDict([('var1',1),('var2',2),('var3',3),('var4',4)])
def myfct(change,list_widgets):
clear_output()
print np.sum([widget.value for widget in list_widgets])
list_widgets=[]
# create the widgets
for name,value in var_dict.iteritems():
list_widgets.append(widgets.IntSlider(value=value,min=value-2,max=value+2,description=name))
# link the widgets with the function
for widget in list_widgets:
widget.observe(lambda change:myfct(change,list_widgets),names='value',type='change')
# group the widgets into a FlexBox
w = widgets.VBox(children=list_widgets)
# display the widgets
display(w)
享受:-)
我正在尝试在 jupyter 笔记本上使用 ipywidgets 制作交互式滑块,以在用户更改滑块小部件时更改绘图的数据,这很简单,我们可以轻松找到示例代码。我遇到的问题是双重的:1)当我的函数中有很多参数(=变量,滑块)要显示时,滑块是垂直排列的,因此如果不滚动 jupyter 页面就很难控制它们。有没有办法按照我的意愿排列滑块,比如 m × n 网格? 2) 为了传递大量 integer-/float-valued 滑块,我制作了一个字典以传递给交互函数。在这里,关键 (=slider/variable/parameter) 名称似乎以随机顺序显示。我尝试在预先按键名排序后通过字典,但它仍然没有解决问题。
如果您能分享任何想法,我将不胜感激。
def myfun(**var_dict):
v = [value for value in var_dict.values()]
return np.sum(v)
var_dict = {'var1':1,'var2':2,'var3':3,'var4':4}
w = interactive(myfun,**var_dict)
display(w)
您将无法使用 **kwargs 解决此问题。如 PEP468 所述 "The only reason they [keyword arguments] don't work is because the interpreter throws that ordering information away." "Starting in version 3.5 Python will preserve the order of keyword arguments as passed to a function"
因此,如果您想获得此行为,您应该: 使用 'interactive':
时命名你的参数from ipywidgets import interactive
from IPython.display import display
import numpy as np
def myfun(**kwargs):
return np.sum(list(kwargs.itervalues()))
w=interactive(myfun,var1=1,var2=2,var3=3,var4=4)
display(w)
或者,如果你真的想使用字典,据我所知,最好是自己构建小部件,而不使用 'interactive'。 你可以这样做:
from IPython.display import display,clear_output
from ipywidgets import widgets
from collections import OrderedDict
var_dict = OrderedDict([('var1',1),('var2',2),('var3',3),('var4',4)])
def myfct(change,list_widgets):
clear_output()
print np.sum([widget.value for widget in list_widgets])
list_widgets=[]
# create the widgets
for name,value in var_dict.iteritems():
list_widgets.append(widgets.IntSlider(value=value,min=value-2,max=value+2,description=name))
# link the widgets with the function
for widget in list_widgets:
widget.observe(lambda change:myfct(change,list_widgets),names='value',type='change')
# group the widgets into a FlexBox
w = widgets.VBox(children=list_widgets)
# display the widgets
display(w)
享受:-)