如何从许多小部件获取输入但仅在按下提交按钮时才重新计算?

How to take inputs from many widgets but only recalculate when submit button is pressed?

我有一些代码可以从一系列小部件获取输入,然后在 pandas DataFrame 上进行一些计算并打印输出。目前,每次任何小部件更改时都会触发计算。鉴于计算需要一些时间,我想延迟计算,直到我按下提交按钮小部件。我不太明白如何将提交按钮绑定到一个函数,然后该函数还读取其他小部件。

这是我所拥有的简化版本:

import ipywidgets as widgets

# input widgets that should only change the calcs after hitting the submit function
value_1 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)
value_2 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)

# submit button
submit_button = widgets.Button(description='submit')

# submit function
def submit():
    # I don't know what to put here to restrict the triggering of the recalculate function until
    # submit button is pressed
    pass

# recalculate function
def recalculate(value_1, value_2):
    # code to perform some calcs on a dataframe based on the widget inputs and print the result

# tie submit button to a function
submit_button.on_click(submit)

# code that ties the input calculations to the calculations
out = widgets.interactive_output(recalculate, {'value_1': value_1, 'value_2': value_2})

# display widgets
widgets.VBox([value_1, value_2, submit_button, out]

两个选项。

使用interact_manual (https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html#interact_manual):

import ipywidgets as widgets

# input widgets that should only change the calcs after hitting the submit function
value_1 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)
value_2 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)

# submit function
def submit():
    # I don't know what to put here to restrict the triggering of the recalculate function until
    # submit button is pressed
    pass

# recalculate function
def recalculate(value_1, value_2):
    return value_1 + value_2
    # code to perform some calcs on a dataframe based on the widget inputs and print the result

# tie submit button to a function
submit_button.on_click(submit)

# code that ties the input calculations to the calculations
out = widgets.interact_manual(recalculate, value_1= value_1, value_2= value_2)

或者自己动手,创建自己的输出小部件并控制显示

import ipywidgets as widgets

# input widgets that should only change the calcs after hitting the submit function
value_1 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)
value_2 = widgets.BoundedFloatText(value=0.5, min=0, max=1, step=0.05)
out = widgets.Output()

# submit button
submit_button = widgets.Button(description='submit')



# recalculate function
def recalculate(value_1, value_2):
    return value_1.value + value_2.value
    # code to perform some calcs on a dataframe based on the widget inputs and print the result
    
# submit function
def submit(button):
    # I don't know what to put here to restrict the triggering of the recalculate function until
    # submit button is pressed
    total = recalculate(value_1, value_2)
    out.clear_output()
    with out:
        display(total)
    

# tie submit button to a function
submit_button.on_click(submit)

# code that ties the input calculations to the calculations
# out = widgets.interactive_output(recalculate, {'value_1': value_1, 'value_2': value_2})

# display widgets
widgets.VBox([value_1, value_2, submit_button, out])