By default, DynamicModule
uses SynchronousInitialization -> True
. This causes the initialization to be performed on the preemptive link, disabling any updates to the front-end. In particular, print statements, cell creation and dynamic box updates will all be deferred until the initialization completes.
If we wish to monitor that initialization within the notebook itself, we must turn off synchronous initialization. If we do so, then any of a number of strategies are possible, such as...
... using Monitor
to watch x
change within a temporary cell:
DynamicModule[{x}
, Dynamic[x]
, SynchronousInitialization -> False
, Initialization :> Monitor[x = 1; Pause@1; x = 2; Pause@1; x = 3, x]
]
... or CellPrint
to watch x
change within a permanent cell:
DynamicModule[{x}
, Dynamic[x]
, SynchronousInitialization -> False
, Initialization :>
(CellPrint@Dynamic@x; x = 1; Pause@2; x = 2; Pause@2; x = 3)
]
... or even simply using Print
to print out some intermediate information:
DynamicModule[{x}
, Dynamic[x]
, SynchronousInitialization -> False
, Initialization :>
(x = 1; Print@"1st"; Pause@2; x = 2; Print@"2nd"; Pause@2; x = 3)
]
We can even monitor the initialization of x
from within the DynamicModule
by overriding the default UpdateInterval -> Infinity
on Dynamic
:
DynamicModule[{x}
, Dynamic[x, UpdateInterval -> 0]
, SynchronousInitialization -> False
, Initialization :> (x = 1; Pause@1; x = 2; Pause@1; x = 3)
, UnsavedVariables :> {x}
]
It is necessary to add x
to the list of UnsavedVariables
because without it the value of x
will not be seen to change dynamically when the output cell is re-initialized in a new front-end session.
For Debugging Purposes
If debugging messages are our goal, and if it is not acceptable to perform asynchronous initialization for some reason, then a simple solution would be to write logging messages to an external file. That file could then be monitored in real-time using an external utility like tail.
It is possible, if ambitious, to use Mathlink
to send debugging messages to a second front-end. See, for example, the chat application in this this question. Personally, I think this is overkill. I would probably spend the time getting the DynamicModule
to work with asynchronous initialization instead.
DynamicModule[{x}, Dynamic[x], Initialization :> (x = 1; Print["x=2"]; x = 2; Print["x=3"]; x = 3) ]
$\endgroup$Pause
you can see that it isn't working like one could expect. $\endgroup$this tracking to be reflected in Dynamic updating
Since Initialization is processed once from top down. (to generate the initial expression). Once this is done, Initialization basically goes away. $\endgroup$Initialization
evaluates every time you open a cdf/nb with visible relatedDynamicModule
. I would like to see x switching values whileInitilization
is running. The Dynamic cell is already there, it's just waiting forInit
to complete, even if you useSynchronousInitialization->False
. I need it to behave like I've described :) $\endgroup$