如何告诉`ipywidgets.interactive`,它应该仅将特定参数视为小部件? [英] How to tell `ipywidgets.interactive`, that it should consider only specific parameters as widgets?

查看:72
本文介绍了如何告诉`ipywidgets.interactive`,它应该仅将特定参数视为小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 ipywidgets.interactive 试图使该函数之后的每个插补都成为小部件.在下面的示例中,我得到了两个小部件,一个用于 age ,一个用于 name :

It seems like ipywidgets.interactive tries to make every imput after the function to be a widget. In the following example, I get two widgets, one for age and one for name:

import ipywidgets 

def greetings(age, name='John'):
    print(f'{name} is {age} years old')

ipywidgets.interactive(greetings, age = widgets.IntSlider(), name = "Bob")

我的期望是,我只能获得一个 age 年龄的小部件,因为它的类型为 ipywidgets.widgets.widget_int.IntSlider ,而"Bob" 的类型为 str (我看不到小部件的链接).自动化小部件创建会导致两个问题:1.我不希望用户指定每个参数2.一些元组(如示例中未显示)之类的参数会导致错误.

My expectation was, that I only get one widget for age, since it is of type ipywidgets.widgets.widget_int.IntSlider whereas "Bob" is of type str (where I don't see the link to widgets). The automated widget creation causes two problems: 1. I don't want that the user can specify every parameter 2. Some parameters like tuples (not shown in the example) will result in an error.

如何告诉 ipywidgets.interactive ,它应该仅将特定参数视为小部件?

How to tell ipywidgets.interactive, that it should consider only specific parameters as widgets?

help(ipywidgets.interactive)文档没有帮助.

推荐答案

首先,感谢您提供的清晰示例和说明.

Firstly, thanks for the clear example and description.

您可以利用ipywidgets中的 fixed 函数通过交互式调用传递任何类型的固定kwarg,但无需为其生成小部件.见下文:

You can leverage the fixed function in ipywidgets to pass fixed kwargs of any type through your interactive call, but without generating widgets for them. See below:

import ipywidgets 

def greetings(age, name='John'):
    print(f'{name} is {age} years old')

ipywidgets.interactive(greetings, 
                       age = ipywidgets.IntSlider(), 
                       name = ipywidgets.fixed("Bob")
                      )

您还应该能够通过此方法传递元组.请参阅文档中的更多信息: https://ipywidgets.readthedocs.io/zh-CN/latest/examples/Using%20Interact.html#Fixing-arguments-using-fixed

You should also be able to pass tuples through this method. See more information in the documentation: https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html#Fixing-arguments-using-fixed

这篇关于如何告诉`ipywidgets.interactive`,它应该仅将特定参数视为小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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