MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: ProgressIndicator inside DynamicModule


> Alas, can not answer your questions properly, but the following modification
> of your code
> seems to also work fine:
> DynamicModule[{range == {0, 100}, sw == False},
>  Module[{pr == 0},
>   Grid@{{
>      Button["Push me",
>       (sw == True;
>        Do[pr == i; Pause[.02];, {i, range[[1]], range[[2]]}];
>        sw == False;), Method -> "Queued"],
>      Dynamic@ProgressIndicator[pr, range],
>      Dynamic@pr,
>      Dynamic@Switch[sw,
>        True, "Calculating...",
>        False, "Finished.",
>        _, "Standing by."]}}]]
> The issue seems to be with the <pr> variable.  My guess is that by
> localizing <pr> with DynamicModule vs plain Module or not localizing at all,
> you somehow indicate to Dynamic that <pr> is going to change due to Dynamic
> updating, so  Dynamic does not track its possible changes by other means
> (like by explicit assignments in a loop that you have).  My guess is
> supported by observing that  once you localize with DynamicModule,
> 1. Dynamic@ProgressIndicator[pr, range], works while
> ProgressIndicator[Dynamic[pr], range] does not
> 2. Consider the following  modification:
> ClearAll[f];
> DynamicModule[{pr == 0, range == {0, 100}, sw == False},
>  Grid@{{
>     Button["Push me",
>      (sw == True;
>       Do[pr == i; Pause[.02];, {i, range[[1]], range[[2]]}];
>       sw == False;), Method -> "Queued"],
>     Dynamic@ProgressIndicator[pr, range],
>     Dynamic[f[pr]],
>     Dynamic@Switch[sw,
>       True, "Calculating...",
>       False, "Finished.",
>       _, "Standing by."]}}]
> You will observe that now the value of <pr> inside <f> is being properly
> updated inside Dynamic during the run of your code. In particular, by using
> f[x_]:==x, you can get back the desired result  - you can even localize <f>
> as well as put this definition inside the same DynamicModule (although this
> is certainly a hack). In both cases, we replaced Dynamic[pr] with something
> else, which apparently made Dynamic properly update the output. Of course,
> my observations  at best somewhat clarify "how", but not "why" - hopefully
> others will have more to say here.

Well, I think it's up to someone from WRI to comment, in my opinion it
is certainly a bug, and one that I think I have seen in other cases, but
never was able to track down to something this simple. Here I have some
examples which look very inconsistent to me:

DynamicModule[{range = {0, 100}, sw = False, pr = 0},
    Button["Push me", (
      sw = True;
      Do[pr = i; Pause[.02];, {i, range[[1]], range[[2]]}];
      sw == False;
     Method -> "Queued"
      ProgressIndicator[Dynamic[pr], range, ImageSize -> 100],
      ProgressIndicator[Dynamic[pr, TrackedSymbols :> {pr}], range,
       ImageSize -> 100],
      Dynamic[ProgressIndicator[pr, range, ImageSize -> 100]],
      ProgressIndicator[Dynamic[pr + 0], range, ImageSize -> 100]
      Dynamic[pr, TrackedSymbols :> {pr}],
      Dynamic[0 + pr],

If you first look at the second column you will find that just
Dynamic[pr] behaves differently than Dynamic[0+pr], not exactly
something one would expect. At least you can help Mathematica to still
do what you want by using the TrackedSymbols options for Dynamic[pr].

Considering that, it is not so surprising that
ProgressIndicator[Dynamic[pr]] does not work, but now you can't even
make it work with neither the TrackedSymbols-Option nor the pr+0 trick
-- even more inconsistency. The case Dynamic[ProgressIndicator[pr]]
seems then to be like the Dynamic[0+pr] case and probably works for the
same reasons...

Are these bugs? Will they be corrected in future versions?

When dealing with these issues I would also love to see functionality
which would allow to have some insight in or even control the dependency
tree that Mathematica uses to decide whether it has to update a Dynamic
or not. The concept of Dynamic evaluation to create graphical user
interface really is an interesting idea and has charm and advantages.
But when it is not working reliable, I find it to take more effort
(usually searching for tricks and workarounds) to create nontrivial
interfaces than when using the standard concepts (MVC,Observer,...) in
other languages. There I have to create the dependencies myself, but at
least I have full control over them...



  • Prev by Date: Re: How to Enable Automatic Recalculation
  • Next by Date: Re: Evaluate to...
  • Previous by thread: Re: ProgressIndicator inside DynamicModule
  • Next by thread: Re: ProgressIndicator inside DynamicModule