Ask Your Question
1

sage interactive disable auto update?

asked 2019-04-05 15:09:31 +0100

msage gravatar image

i have a sheet with interactive sliders which generates plots. when I alter a value on a slider, the plot is executed not only at the destination value, but also on many steps between starting point and destination. As the plot takes some time, it is very slow to get the result. Is there a possibility to deactivate autoupdate, and for example make a Button for executing the script with the new values?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2019-04-08 02:48:26 +0100

Juanjo gravatar image

updated 2019-11-24 02:07:36 +0100

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.

edit flag offensive delete link more

Comments

Your clips work on my compiled 9.4RC0. but the sample code from
https://doc.sagemath.org/html/en/prep... throws an error. I have looked at it and can't see anything wrong. It could be my compile pulled something wrong in or is it a mistake?

     @interact
     def _(m=('matrix', identity_matrix(2)), auto_update=False):
           print(m.eigenvalues())
rrogers gravatar imagerrogers ( 2021-10-25 03:22:48 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-04-05 15:09:31 +0100

Seen: 665 times

Last updated: Nov 24 '19