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

问题描述

我有一些代码,该代码从一系列小部件中获取输入,然后对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,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_manualhttps://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,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,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])