如何在ipywidgets上定义Slider小部件的默认值? [英] How to define the default value of a Slider widget on ipywidgets?
问题描述
在Jupyter Notebook上并使用ipywidget,我可以使用精简版创建滑块
On a Jupyter Notebook and using ipywidgets I can create sliders using the compact version
import ipywidgets as ip
def f(x):
print(x)
ip.interact(f, x = (0, 1, 1))
滑块的默认值为0,但我想设为1.我该怎么做?
The slider's default value is 0 but I would like to make it 1. How can I do this?
推荐答案
小部件缩写(使用元组请求滑块等)仅公开了可以传递给小部件的选项的一小部分.默认值来自函数定义中的默认值(如果有),或者来自范围中心(如果没有).如果您不想更改现有函数的签名以为其提供默认参数,则可以使用两个选项来指定初始值:
The widget abbreviations (using tuples to request sliders, etc.) only expose a small subset of the options that can be passed to a widget. Default values come from the default value in the function definition if available, or the center of the range if not. If you don't want to change the signature of your existing function to give it a default argument, you have two options to specify the initial value:
-
将您的函数包装在另一个指定默认值的函数中:
Wrap your function in another one that specifies defaults:
def f(x, y):
print(x, y)
@interact(x=(0, 5), y=(-1.,3.))
def g(x=1, y=2.0):
return f(x, y)
直接实例化窗口小部件并指定value
属性,而不要依赖缩写:
instantiate a Widget directly and specify the value
attribute, instead of relying on the abbreviations:
w = widgets.IntSlider(value=1, min=0, max=1)
interact(f, x=w)
这篇关于如何在ipywidgets上定义Slider小部件的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!