我遇到了ipython笔记本中的问题。我想在某些类的meethods中实现iphython小部件,但似乎在这个github问题中提到的ipython v2中还不可能:https://github.com/ipython/ipython/issues/6278
引自fperez
此代码:
from IPython.html.widgets import interact class Foo(object): def show(self, x): print x f = Foo() interact(f.show, x=(1,10))
产生此异常:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-58-b03b8685dfc0> in <module>() 7 f = Foo() 8 ----> 9 interact(f.show, x=(1,10)) /home/fperez/usr/lib/python2.7/site-> packages/IPython/html/widgets/interaction.pyc in interact(__interact_f, > **kwargs) 235 f = __interact_f 236 w = interactive(f, **kwargs) --> 237 f.widget = w 238 display(w) 239 return f AttributeError: 'instancemethod' object has no attribute 'widget'
我尝试将交互式小部件包装在lambda函数中,但我的尝试似乎不起作用
def constructWidgets(self):
interact(lambda a:self.getWidgetValue(a),a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0, description="WidgetNameFoo"))
我收到此错误消息
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: <lambda>() got an unexpected keyword argument 'WidgetNameFoo'
据我所知,描述字段(在本例中为'WidgetNameFoo')与变量名'a'之间存在冲突。当描述字段计算变量名时,ipython不会返回TypeError消息。例如:
def constructWidgets(self):
interact(lambda a:self.getWidgetValue(a),a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0, description="a"))
我需要传输一个根据具体情况而变化的描述字段。
感谢您的帮助! =)