【问题标题】:Suppress output from an interact() widget抑制来自 interact() 小部件的输出
【发布时间】:2016-07-01 00:58:35
【问题描述】:

我正在使用一个小部件来参数化一些数据的生成。我想捕获数据,而不输出它。这可能吗?在interact() 之后添加; 不起作用。示例:

import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed

def generate_data(n, p, s):
    return np.random.negative_binomial(n, p, s)

w_n = widgets.IntSlider(min=1, max=10000, step=1)
w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
w_s = widgets.IntSlider(min=500,max=10000,step=50)

data = interact(generate_data, n = w_n, p = w_p, s = w_s);

【问题讨论】:

    标签: ipython ipython-notebook jupyter jupyter-notebook ipywidgets


    【解决方案1】:

    您可以修改全局变量,而不是在generate_data() 中返回值。它避免了打印输出,您可以稍后在代码中使用val

    import ipywidgets as widgets
    from ipywidgets import interact, interactive, fixed
    import numpy as np
    
    val = None
    def generate_data(n, p, s):
        global val
        val = np.random.negative_binomial(n, p, s)
    
    w_n = widgets.IntSlider(min=1, max=10000, step=1)
    w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
    w_s = widgets.IntSlider(min=500,max=10000,step=50)
    
    interact(generate_data, n = w_n, p = w_p, s = w_s)
    

    【讨论】:

      【解决方案2】:

      我已经向 ipywidgets 提交了一个拉取请求来解决这个问题:

      https://github.com/ipython/ipywidgets/pull/712

      更改实际上非常简单。使用此版本,您只需将额外的 kwarg 传递给 interact

      data = interact(generate_data, n = w_n, p = w_p, s = w_s,
                      __output_result=False)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        • 2016-03-07
        • 2021-07-08
        • 1970-01-01
        相关资源
        最近更新 更多