函数的返回值,其中ipython小部件用于获取输入参数 [英] Return value from function where ipython widgets are used to obtain input parameters

查看:145
本文介绍了函数的返回值,其中ipython小部件用于获取输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试widgetize我的IPython笔记本,并遇到事件和从函数返回值的麻烦。这是我认为最好的工作流程:

I'm trying to "widgetize" my IPython notebooks and am running into trouble with events and returning values from a function. This is the workflow I'm thinking is the best way to go:


  • 使用小部件获取任意函数的输入值

  • 在事件触发器上调用该函数

  • 从函数返回值

我首先尝试使用interact方法来调用函数,但这似乎很难关联事件和返回值。通过阅读其他交互式示例,创建课程似乎是要走的路。我不经常写课;所以希望我的错误很简单。

I first tried using the "interact" method to call the function, but that seemed difficult to associate events and return values. From reading other interactive examples, making a class seemed like the way to go. I don't write classes very often; so hopefully my error is something simple there.

以下是两个小部件,当用户按下Enter时应调用一个函数并将其返回值存储在类中供将来使用。

The following makes two widgets, and when the user presses "Enter" should call a function and store its return value in the class for future use.

实际上,在我输入任何文本之前它会触发该函数两次,并在我更改值时抛出'unicode对象不可调用'。

In reality, it fires off the function two times before I enter any text and throws 'unicode object is not callable' when I change value.



    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


推荐答案

Oliver Ruebel通过电子邮件回答。以下是我对问题的修正。

Answered by Oliver Ruebel via email. Here are his fixes to my problems.

对on_submit的分配错误

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

When you call the on.submit function you need to hand it the function you want to call. In you code this looks like this.

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

然而,你所代码的是它调用自我.handle_submit(因为你在函数后包含了()括号)然后将该函数的返回
值赋给你的提交句柄。这解释了您所看到的行为。即,在 init ()中调用该函数两次,然后在事件发生时调用
,它将对函数返回的字符串进行操作。解决这个问题很简单,只需删除(),即:

However, what you are code does is it calls self.handle_submit (because you included the "()" brackets after the function) and then assigns the return value of that function to your on submit handle. This explains the behavior you are seeing. I.e., the function is called twice in your init() and then when the event happens it trys to act on the string that was returned by the function. The fix for this is simple, just remove the "()", i.e.:

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

handle_submit函数签名错误

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

The handle submit function must accept the text object of the widget as input. I.e., you will get self.p1_text or self.p2_text as input, depending on which widget calls it. I.e., your function should look like this:

def handle_submit(self, text):
...

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

With the above changes, everything should work as expected. However, if you want to implement different behaviors for the different widgets you should use different handle functions for the different widgets and place any shared behavior into some other function that is called by your handle function.

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')

这篇关于函数的返回值,其中ipython小部件用于获取输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆