Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ProgressIndicator inside DynamicModule

113 views
Skip to first unread message

Zachar István

unread,
May 19, 2010, 7:03:09 AM5/19/10
to
Dear Group,

dealing with dynamic functions is very frustrating: every now and then
a problem appears, which is totally against my previous experience and
knowledge, and I am simply not able to resolve it. Here is a new one,
any help would be welcome.

Why does ProgressIndicator won't work inside DynamicModule, where
1. it works without enclosing the Grid in DynamicModule,
2. the 'sw' switch is correctly displayed (but not 'pr').
??

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[Dynamic@pr, range],
Dynamic@pr,
Dynamic@Switch[sw, True, "Calculating...", False, "Finished.", _,
"Standing by."]
}}
]


Thanks in advance
Istvan

István Zachar

unread,
May 19, 2010, 8:10:37 PM5/19/10
to
Dear Patrick,

Thanks for the help, but the problem is not solved.
First of all, I did not state the problem is in the ProgressIndicator. I
rather suggested that DynamicModule is the cause of 'misfunctioning'.
Furthermore, your solution does not solve anything. My toy model
represented a more complex situation, where I have an iterative function
(a modified NDSolve, represented here with the Do loop), for which I
want to check on the progress via a similar construct as StepMonitor.
Thus you have thrown out the baby with the water by just retaining pr++.
If I rewrite your version to mirror my issue, I end up with something
like this:

DynamicModule[{pr = 0, range = {0, 100}, sw = False}, Grid@{{

Button["Push me", sw = Not[sw]; pr = 0;],
Dynamic@ProgressIndicator[Dynamic@pr, range],
Dynamic@If[sw, Do[Pause[0.02]; pr = i, {i, range[[1]],
range[[2]]}]; pr],
Dynamic@Switch[sw, True, "Calculating...", False, "Finished."]
}}]

which AGAIN won't produce the correct behaviour for ProgressIndicator.

Any other idea?


On 2010.05.19. 14:04, Patrick Scheibe wrote:

Hi,

the problem is *not* the progress indicator but your button!
The button-function does not update dynamically the values.
What about:

DynamicModule[{pr = 0, range = {0, 100}, sw = False}, Grid@{

{Button["Push me", sw = Not[sw]],
Dynamic@ProgressIndicator[Dynamic@pr, range],
Dynamic@If[sw && pr < Last@range, Pause[0.02]; pr++, pr],
Dynamic@


Switch[sw, True, "Calculating...", False, "Finished.", _,
"Standing by."]}}]

Cheers
Patrick

Patrick Scheibe

unread,
May 19, 2010, 8:11:52 PM5/19/10
to
Hi,

but when it's just for showing progress of a calculation and not for
dynamically changing input and displaying the result, why don't you use
some simple construct like

count == 1;
input == RandomReal[{1, 50}, 5000];
ProgressIndicator[Dynamic[count], {1, Length[input]}]
complexFunc[x_] :== Nest[BesselJ[0, #] &, x, 20];
Do[(count++; complexFunc[input[[i]]]), {i, 1, Length[input]}]


What exactly is the reason to use a DynamicModule?

The problem in the construct with the DynamicModule and the Do-Loop
seems to be the reason that DynamicModule *needs* to display something
which should be updated dynamically. John Fultz gave already some very
exhaustive insights, how this works and what doesn't work. Maybe you
want to read this here

http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00424.html

Cheers
Patrick


Am May 19, 2010 um 3:06 PM schrieb Istv=E1n Zachar:

> Dear Patrick,
>
> Thanks for the help, but the problem is not solved.
> First of all, I did not state the problem is in the
> ProgressIndicator. I rather suggested that DynamicModule is the
> cause of 'misfunctioning'.
> Furthermore, your solution does not solve anything. My toy model
> represented a more complex situation, where I have an iterative
> function (a modified NDSolve, represented here with the Do loop),
> for which I want to check on the progress via a similar construct as
> StepMonitor. Thus you have thrown out the baby with the water by
> just retaining pr++.
> If I rewrite your version to mirror my issue, I end up with
> something like this:
>

> DynamicModule[{pr == 0, range == {0, 100}, sw == False}, Grid@{{
> Button["Push me", sw == Not[sw]; pr == 0;],
> Dynamic@ProgressIndicator[Dynamic@pr, range],
> Dynamic@If[sw, Do[Pause[0.02]; pr == i, {i, range[[1]],

> range[[2]]}]; pr],
> Dynamic@Switch[sw, True, "Calculating...", False, "Finished."]
> }}]
>
> which AGAIN won't produce the correct behaviour for ProgressIndicator.
>
> Any other idea?
>
>
>
>
> On 2010.05.19. 14:04, Patrick Scheibe wrote:
>
> Hi,
>
> the problem is *not* the progress indicator but your button!
> The button-function does not update dynamically the values.
> What about:
>

> DynamicModule[{pr == 0, range == {0, 100}, sw == False}, Grid@{
> {Button["Push me", sw == Not[sw]],

Patrick Scheibe

unread,
May 19, 2010, 8:14:12 PM5/19/10
to
Hi,

the problem is *not* the progress indicator but your button!
The button-function does not update dynamically the values.
What about:

DynamicModule[{pr == 0, range == {0, 100}, sw == False}, Grid@{
{Button["Push me", sw == Not[sw]],
Dynamic@ProgressIndicator[Dynamic@pr, range],
Dynamic@If[sw && pr < Last@range, Pause[0.02]; pr++, pr],
Dynamic@
Switch[sw, True, "Calculating...", False, "Finished.", _,
"Standing by."]}}]

Cheers
Patrick

Am May 19, 2010 um 1:03 PM schrieb Zachar Istv=E1n:

> Dear Group,
>
> dealing with dynamic functions is very frustrating: every now and then
> a problem appears, which is totally against my previous experience and
> knowledge, and I am simply not able to resolve it. Here is a new one,
> any help would be welcome.
>
> Why does ProgressIndicator won't work inside DynamicModule, where
> 1. it works without enclosing the Grid in DynamicModule,
> 2. the 'sw' switch is correctly displayed (but not 'pr').
> ??
>

> 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[Dynamic@pr, range],

Leonid Shifrin

unread,
May 19, 2010, 8:12:03 PM5/19/10
to
Hi Istvan,

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.

Hope this helps.

Regards,
Leonid

2010/5/19 Zachar Istv=E1n <replic...@gmail.com>

Albert Retey

unread,
May 20, 2010, 6:40:58 AM5/20/10
to
Hi,

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},
Grid[{{
Button["Push me", (
sw = True;
Do[pr = i; Pause[.02];, {i, range[[1]], range[[2]]}];


sw == False;
),
Method -> "Queued"
],

Column[{
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]
}],
Column[{
Dynamic[pr, TrackedSymbols :> {pr}],
Dynamic[0 + pr],
Dynamic[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...

hth,

albert

0 new messages