6
$\begingroup$

Is it possible to show a fixed text such as "updating..." instead of the last result shown, when Manipulate is re-evaluating an expression (in a slow evaluation context)? How to do it?

More precisely, I have a Button element in the set of controls, and it is the action triggered by the button the one that slows down the process, and not the results shown in the Manipulate sentence themselves.

This could be a MWE for what I mean:

Manipulate[result,
 {{x, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None},
 {{y, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None},
 Button["Calculate", Pause[2]; result = x + y],
 TrackedSymbols :> {result},
 Initialization -> Clear[result, x, y]]

In this example, the values for two integer numbers x and y are selected using their respective slider controls, and then a button is used to perform some calculations based on them. Finally, results based on those calculations are shown using Manipulate.

So, regarding this example, my question is: how can I show the text "updating..." while the button is thinking?

If you think there would be an easier solution in case it was not the button but the expression to be shown by Manipulate the one that takes some long time to be executed, I think I could also modify my real problem to be like that. The MWE could be like this:

Manipulate[Pause[2]; parameters[[1]] + parameters[[2]],
 {{a, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None},
 {{b, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None},
 Button["Calculate", parameters = {a, b}], 
 TrackedSymbols :> {parameters}, 
 Initialization -> (Clear[a, b, parameters]; parameters = {5, 5})]

It would be also OK if the text "updating..." appeared in the button, temporarily replacing the "Calculate" text. It would be a little bit strange, but also valid.

I look forward to your solutions.

$\endgroup$
4
$\begingroup$

I don't know if this fits your needs, but it works for me :P

DynamicModule[{a = 0, b = 0, result = {}, panel, acc, message},
 Dynamic[Refresh[
   Overlay[
    {panel, message},
    Dynamic@Switch[acc, 0, {1}, _, {1, 2}],
    Dynamic@Switch[acc, 0, 1, _, 2]
    , Alignment -> Center],
   None]]
 ,
 Initialization :> (
   acc = 0;
   panel = Column[Print["CCreating column"]; {
      Dynamic@result,
      Slider[Dynamic@a, {0, 1}],
      Slider[Dynamic@b, {0, 1}],
      Button["Calculate", acc = 1; FinishDynamic[]; Pause[2]; 
       result = {a, b}; acc = 0;, Method -> "Queued"]
      }];

 message = 
   Framed["Updating...", Background -> GrayLevel@.6]), BaseStyle -> Selectable -> False]

enter image description here

$\endgroup$
  • $\begingroup$ thank you for your answer. I've just added my own answer, that better fits my needs. You can comment on it, if you want. $\endgroup$ – Vicent Sep 11 '14 at 10:26
4
$\begingroup$

I played around with the code and I finally found a way to do what I wanted. In the meanwhile, I think I've also better understood the behaviour of Manipulate.

This is my solution; it deals with the second MWE I posted:

Manipulate[

 (* === Calculations that are triggered each time there is a change
        in tracked symbols —'parameters', in this case: === *)

 message = "Updating..."; result = "";
 Pause[2];
 result = parameters[[1]] + parameters[[2]];
 message = "Ready";

 (* === Results to be shown: === *)

 Column[{Dynamic[message], Dynamic[result]}],

 (* === Controls: === *)

 {{a, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None}, 
 {{b, 5}, 0, 10, 1, Appearance -> {"Labeled"}, AppearanceElements -> None}, 
 Button["Calculate", parameters = {a, b}],

 (* === Other settings: === *)

 TrackedSymbols :> {parameters},
 Initialization -> (
   Clear[a, b, parameters, message, result];
   parameters = {5, 5}; result = 10; message = "Ready"
   )]

This is the result:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.