You can add auto_update=False
as an argument to the function defining the interact. For example:
@interact
def _(a=slider(0,5,step_size=0.2,default=3),
b=slider(0,5,step_size=0.2,default=1),
color=selector(["red","green","blue"],label="Color",default="green"),
auto_update=False):
show(plot(sin(a*x)+cos(b*x), (x,0,4*pi), color=color))
The interact is only updated when you press the button below the interact controls. See this SageMath Cell.
At least in a Jupyter notebook, you can also use an alternative syntax:
@interact.options(manual=True)
def _(a=slider(0,5,step_size=0.2,default=3),
b=slider(0,5,step_size=0.2,default=1),
color=selector(["red","green","blue"],label="Color",default="green")):
show(plot(sin(a*x)+cos(b*x), (x,0,4*pi), color=color))
The button text can be customized. If you prefer, say, "Redraw" instead of the default text, just modify the first line in the preceding snippet as follows:
@interact.options(manual=True, manual_name="Redraw")
Edit (24th Nov. 2019): In a SageMath Cell, one can replace auto_update=False
by, for example, auto=UpdateButton(text="Redraw")
. The text written in the button will be Redraw. The button can be referred by auto
when specifying the layout, if any. For example,
@interact(layout=dict(top=[["a","b"],["color"]], bottom=[["auto"]]))
def _(a=slider(0,5, step_size=0.2, default=3),
b=slider(0,5, step_size=0.2, default=1),
color=selector(["red","green","blue"], label="Color",
default="green", buttons=True),
auto=UpdateButton(text="Redraw")):
show(plot(sin(a*x)+cos(b*x), (x,0,4*pi), color=color))
See this SageMath cell.