【问题标题】:Return value from function where ipython widgets are used to obtain input parameters从使用 ipython 小部件获取输入参数的函数返回值
【发布时间】:2016-05-26 13:47:59
【问题描述】:

我正在尝试“小部件化”我的 IPython 笔记本,但在处理事件和从函数返回值时遇到了麻烦。这是我认为最好的工作流程:

  • 使用小部件获取任意函数的输入值
  • 在事件触发时调用函数
  • 函数返回值

我首先尝试使用“interact”方法来调用函数,但这似乎很难关联事件和返回值。从阅读其他交互式示例来看,开设课程似乎是可行的方法。我不经常写课。所以希望我的错误很简单。

下面制作了两个小部件,当用户按下“Enter”时应该调用一个函数并将其返回值存储在类中以备将来使用。

实际上,它会在我输入任何文本之前触发该函数两次,并在我更改值时抛出“unicode object is not callable”。



    import ipywidgets as widgets
    from IPython.display import display

    def any_function_returning_value(word1,word2):
        new_word = 'Combining words is easy: %s %s'%(word1,word2)
        print new_word
        return new_word

    class learn_classes_and_widgets():
        def __init__(self, param1 = 'a word', param2 = 'another word'):
            self.p1_text = widgets.Text(description = 'Word #1',value = param1)
            self.p2_text = widgets.Text(description = 'Word #2',value = param2)
            self.p1_text.on_submit(self.handle_submit())
            self.p2_text.on_submit(self.handle_submit())
            display(self.p1_text, self.p2_text)

        def handle_submit(self):
            print "Submitting"
            self.w = any_function_returning_value(self.p1_text.value,self.p2_text.value)
            return self.w

    f = learn_classes_and_widgets(param1 = 'try this word')
    #f.w should contain the combined words when this is all working

【问题讨论】:

    标签: ipython ipywidgets


    【解决方案1】:

    由 Oliver Ruebel 通过电子邮件回答。这是他对我的问题的修复。

    分配给 on_submit 错误

    当您调用 on.submit 函数时,您需要将要调用的函数交给它。在您的代码中,这看起来像这样。

    self.p1_text.on_submit(self.handle_submit())
    self.p2_text.on_submit(self.handle_submit())
    

    但是,您的代码所做的是调用 self.handle_submit (因为您在函数后包含了“()”括号),然后分配返回 该函数的值到您的提交句柄。这解释了您所看到的行为。即,该函数在您的 init() 中被调用两次,然后 当事件发生时,它会尝试对函数返回的字符串进行操作。解决这个问题很简单,只需删除“()”,即:

    self.p1_text.on_submit(self.handle_submit)
    self.p2_text.on_submit(self.handle_submit)
    

    handle_submit 函数签名错误

    句柄提交函数必须接受小部件的文本对象作为输入。即,您将获得 self.p1_text 或 self.p2_text 作为输入, 取决于哪个小部件调用它。即,您的函数应如下所示:

    def handle_submit(self, text):
    ...
    

    通过上述更改,一切都应按预期工作。但是,如果您想为不同的小部件实现不同的行为,您 应该为不同的小部件使用不同的句柄函数,并将任何共享行为放入句柄调用的其他函数中 功能。

    import ipywidgets as widgets
    from IPython.display import display
    
    def any_function_returning_value(word1,word2):
        new_word = 'Combining words is easy: %s %s'%(word1,word2)
        print new_word
        return new_word
    
    class learn_classes_and_widgets():
        def __init__(self, param1 = 'a word', param2 = 'another word'):
            self.p1_text = widgets.Text(description = 'Word #1',value = param1)
            self.p2_text = widgets.Text(description = 'Word #2',value = param2)
            self.p1_text.on_submit(self.handle_submit)
            self.p2_text.on_submit(self.handle_submit)
            display(self.p1_text, self.p2_text)
    
        def handle_submit(self, text):
            print "Submitting"
            print "Text " + str(text.value)
            self.w = any_function_returning_value(self.p1_text.value,self.p2_text.value)
            return self.w
    
    f = learn_classes_and_widgets(param1 = 'try this word')
    

    【讨论】:

    • 对于所有的徘徊,返回值在f.w中找到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2014-11-23
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多