I have not been able to find the answer for the following problem, and would be happy to be educated.
I am writing a custom user interface around some code that has lengthy calculations. In the standard notebook, I can use PrintTemporary
to show the status of the internal variable during the calculation. Now I would like that the print messages are directed into some Panel inside a DynamicModule
. The calculation might be triggered by pressing a Button for example.
The best I have achieved so far is that after pressing the button, nothing happens, and at the end of the calculation, all values are spit out at once, and the last one appears in the Panel. This happens with SessionSubmit[ScheduledTask[...]]
.
What I did is define a global variable (message
), which is updated during the long calculation, and in the ScheduledTask
a local message is assigned to the Global`message
. The local message is displayed in the Panel
. I can also print the local message variable, and in this way I see that indeed it got inbetween all values, but only at the end of the function call, these are printed / displayed in the Panel
.
How can I achieve that the Panel
constantly (or every second) updates during the calculation?
The minimal code example would be:
(****************************)
Global`globalMessage = "2";
externalFunc[] := Block[{},
Global`globalMessage = "3";
Pause[4];
Global`globalMessage = "4";
Pause[4];
Global`globalMessage = "5";
];
insertBelowEvaluationCell[expr_, nb_] :=
(SelectionMove[nb, After, EvaluationCell];
NotebookWrite[nb, Cell[BoxData@ToBoxes[expr], "Print"]]);
nb = EvaluationNotebook[];
diag = CreateDialog[DynamicModule[{testFunc, message, task},
Dynamic@
Column[{Button["Test", testFunc[]],
Panel[Dynamic[message], "Message"],
Button["Close", TaskRemove[task]; DialogReturn[]]}],
Initialization :> (message = "1";
testFunc[] := Block[{}, externalFunc[]];
task =
SessionSubmit[
ScheduledTask[message = Global`globalMessage;
Global`insertBelowEvaluationCell[message, Global`nb], 1]];
)]];
(*******************)
Thanks a lot for your help!
Pause
instead of real calculations. $\endgroup$FinishDynamic
to ensure updating at each desired step. But we need more information. $\endgroup$