7

We can create a real-time monitor for a variable like this:

CreatePalette@Panel@Row[{"x = ", Dynamic[x]}]

(This is more interesting and useful if x happens to be something like $Assumptions. It's so easy to set a value and then forget about it.)

Unfortunately this stops working if the kernel is re-launched (Quit[], then evaluate something). The palette won't show changes in the value of x any more.

Is there a way to do this so it keeps working even across kernel sessions? I find myself restarting the kernel quite often. (If the resulting palette causes the kernel to be automatically started after Quit that's fine.)


Update: As mentioned in the comments, it turns out that the palette ceases working only if we quit by evaluating Quit[]. When using Evaluation -> Quit Kernel -> Local, it will keep working.


Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • The Mma tutorial on [Storing and Tracking Palette States](http://reference.wolfram.com/mathematica/tutorial/StoringAndTrackingPaletteStates.html) discusses some methods that should be useful. – kglr Jan 06 '12 at 13:40
  • @kguler I have seen that previously, but if I remember right, it's only about storing the state between session (e.g. `DynamicModule`). This is not what I need. Note that the program code (not state) stays the same between sessions, and the variable I monitor is hard-coded. Yet after a kernel restart it stops working, either because it doesn't update dynamically, or because the ``$CellContext` `` before `x` in the palette causes trouble (just a guess, I couldn't verify or fix this) – Szabolcs Jan 06 '12 at 13:43
  • 3
    Note that, if you quit the kernel from the drop-down menu, Evauation->Quit kernel -> Local, your palette will still be working after you start a new session. – Leonid Shifrin Jan 06 '12 at 13:47
  • @kguler I tried to include the displayed variable as a "remembered" `DynamicModule` value : `DynamicModule[{var = Unevaluated[x]}, ...]`. The result is the same though. – Szabolcs Jan 06 '12 at 14:02
  • @Leonid Good observation, I almost never use that menu! So the front end does something extra when we quit the kernel. I wonder if those operations are defined in a user readable .tr file somewhere. – Szabolcs Jan 06 '12 at 14:03
  • 2
    You could also run `FrontEndTokenExecute["EvaluatorQuit"]`. This is effectively the same as choosing Quit Kernel from the menu as mentioned by Leonid. However, according to the documentation, the frontend token `"EvaluatorQuit"` hasn't been fully integrated yet so it might change in future. – Heike Jan 06 '12 at 17:15
  • Very good point @Heike. It's not a solution, but it's a workaround to define `$PreRead = (If[# === "Quit" || # === "Quit[]", FrontEndTokenExecute["EvaluatorQuit"], #]) &` to protect my against my habit of typing `Quit`. I guess one could even put this in the front end's init.m file (though not sure how and when that's read) – Szabolcs Jan 06 '12 at 17:32

2 Answers2

3

I can only guess, because on my Ubuntu here the situations seems buggy. The trick with the Quit from the menu like Leonid suggested did not work here. Another one is: on a fresh Mathematica session with only one notebook open:

Dynamic[x]
x = 1
Dynamic[x]
x = 2

gives as expected

2
1
2
2

Typing in the next line Quit, evaluating and typing then x=3 updates only the first of the Dynamic[x].

Nevertheless, have you checked the command

Internal`GetTrackedSymbols[]

This gives not only the tracked symbols but additionally some kind of ID where the dynamic content belongs. If you can find out, what exactly these numbers are and investigate in the other functions you find in the Internal context, you may be able to add your palette Dynamic-content manually after restarting the kernel.

I thought I had something like that with

Internal`SetValueTrackExtra 

but I'm currently not able to reproduce the behavior.

halirutan
  • 4,281
  • 18
  • 44
  • More interesting findings. I can reproduce your finding about the second `Dynamic` not updating, not even when I use the menu command to quit. – Szabolcs Jan 06 '12 at 20:45
  • Interestingly, it takes two `Quit[]`s or two invocations of `Quit Kernel-Local` on the `Evaluation` menu to clear both dynamic expressions to the value `x`. Then, typing `x=3` updates both. – kglr Jan 10 '12 at 03:42
  • Funnier still, it takes as many `Quit[]`s as the number of `Dynamic[x]`s in all the open notebooks to clear all to the value `x`. – kglr Jan 10 '12 at 03:47
  • That\s useful, it seems those numbers are `BoxIDs`: http://mathematica.stackexchange.com/questions/102204/ways-to-obtain-a-boxobject – Kuba Mar 04 '16 at 06:57
3

@halirutan's answer jarred my memory...

Have you ever come across: Experimental/ref/ValueFunction? (documentation address)

Although the documentation contains no examples, the 'more information' section provides the following tidbit:

The assignment ValueFunction[symb] = f specifies that whenever symb gets a new value val, the expression f[symb,val] should be evaluated.

telefunkenvf14
  • 1,011
  • 7
  • 19
  • Interesting finding! It doesn't solve the problem with the kernel restart, and `Dynamic` stopping updating (if you find a solution for that using this function, please let me know!), but it's definitely worth a mention! – Szabolcs Jan 11 '12 at 08:09