当前位置: 动力学知识库 > 问答 > 编程问答 >

ipython - Suppress output from an interact() widget

问题描述:

I am using a widget to parametrise the generation of some data. I would like to capture the data, without outputting it. Is this possible? Adding ; after interact() doesn't work. Example:

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

网友答案:

Instead of returning a value in generate_data(), you could modify a global variable. It avoids printing the output, and you can use val later in your code.

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)
网友答案:

I have submitted a pull request to ipywidgets that addresses this problem:

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

The changes are actually very simple. With this version, you would simply pass an extra kwarg to interact:

data = interact(generate_data, n = w_n, p = w_p, s = w_s,
                __output_result=False)
分享给朋友:
您可能感兴趣的文章:
随机阅读: