Interrogating a running evaluation
For simplistic purposes there is Evaluate in Subsession which is in the Evaluation menu, and has the shortcut key F7 under Windows & Linux (and alt+shift+enter under macOS).
Simply make a new Cell while the evaluation is running, containing e.g. a
, make sure the cell has focus, and press F7; Mathematica evaluates this and returns the current value of a
, and continues the computation.
If you prefer an ongoing count you can put e.g. Dynamic[a]
in the new cell.
Update
The other answers provide a good way to add dynamic monitoring using a single kernel when access to the front end is available. The method I proposed using multiple kernels will work even when the front end is not accessible. So, you might ask, when would the front end not be available?
Suppose you have a remote machine, with two kernels, a master/monitor kernel, and a slave/working kernel. It is possible to communicate with the master kernel using the Channel framework. In this situation, one might want to ask the master kernel to check on the status of the slave kernel, and report back diagnostic information. This is in fact a situation that I currently use, and one where I don't think the other answers will work.
I had a stylesheet approach that is not nearly as effective as MrWizard's answer, so I have removed that part of my original answer.
Original answer
Yes, this is possible. First, you need to make sure that your version of Mathematica has multiple kernels defined. Then, you can use Dynamic
(or other symbols that take an Evaluator
option, e.g., Button
or Cell
) to display the value of a symbol in another kernel.
Multiple kernels
You can create additional kernels using the Evaluation | Kernel Configuration Options
menu item. Another method is to use CurrentValue
. For instance, the following returns the list of evaluators that have been defined:
CurrentValue[$FrontEnd, EvaluatorNames]
{"Local" -> {"AutoStartOnLaunch" -> True}, "Local 2" -> {}}
Let's add a new evaluator:
CurrentValue[$FrontEnd, {EvaluatorNames, "Debug"}] = {"AppendNameToCellLabel"->True}
{"AppendNameToCellLabel" -> True}
Check:
CurrentValue[$FrontEnd, EvaluatorNames]
{"Local" -> {"AutoStartOnLaunch" -> True}, "Local 2" -> {}, "Debug" -> {"AppendNameToCellLabel" -> True}}
Dynamic
With multiple kernels, you can use Dynamic
with the Evaluator
option to display the value of a symbol in a different kernel. For example, suppose you have a running computation in kernel "Local". Then, in a notebook running the kernel "Debug", you can evaluate:
Dynamic[a, Evaluator->"Local"]
to display the value of the symbol a
in kernel "Local".
For example, in the following animation, I create two notebooks, one using the kernel "Local", and the other using the kernel "Debug". Then, I start a computation in the "Local" notebook. Subsequently, in the "Debug" notebook, I execute the Dynamic
expression above, which displays the value of a
in the "Local" kernel:
Here is a quick solution in case you do not want / can not play with additional Kernels.
Steps
Assuming, the calculation already started:
create e.g. an input cell, just by typing below
Show cell expression Ctrl+Shift+E
Replace cell expression with:
Cell[BoxData[ DynamicBox[ToBoxes[$CellContext`a, StandardForm] ]]]
Switch back Ctrl+Shift+E
Why does it work?
Shortly, by default Dynamic
content is evaluated via preemtive link which means that this evaluation can interrupt main link evaluations (our Do loop).
See: tutorial/AdvancedDynamicFunctionality#1938125129
We just need DynamicBox
to appear in the notebook structure without using kernel.
We are editing cell expressions manually in the front end, the kernel is not needed. Once we finish the front end will try to handle our cell.