I need to give a notebook a few simple state variables (counters) that will be saved with the notebook, persist across kernel restarts, etc. From what I've read, using variables defined within a DynamicModule
is the appropriate strategy, especially since I'm going to have a cell (docked at the top of the notebook) that displays the current value of those variables.
My problem is that these variables should NOT be changed via UI widgets, by the user. Instead, I need to change them programmatically, from code that's resident in a package the notebook loads. I can't figure out how to do that. I've read the documentation on DynamicModule "Wormholes", but that only discusses state variables that are changed by UI widgets (sliders, buttons, etc.). I've also read this post, which might be relevant, but I don't grok it well enough to see how.
A different approach might possibly be to store the state variables in TaggingRules associated with the notebook, and have the display area query those within a Dynamic[]
function... but that seems like it's well outside the intended purpose of Dynamic
, and the subtleties involved -- wrapping Dynamic
around a TaggingRule value query, probably with some string-to-number coercion involved -- daunts me.
So: Can I programmatically alter the values of variables within a DynamicModule from outside that module? Or is there a different/better way to proceed?
Edit: What makes my question different from others I've found on this site, and other discussions of DynamicModule wormholes, is that they all use a UI widget to change the state variable. I'm specifically asking how to do it programmatically.
Edit 2: As the discussion here has evolved, I realize that the question I originally posted did not in fact capture my ultimate aim. Therefore, the perfectly good answers below don't answer the question stated in the title, although they do match the intent communicated in the description. Therefore, I've changed the title.
TaggingRules
programmatically, without using any GUI widgets, you just need to have a handle to the notebook that they're associated with (EvaluationNotebook[]?) $\endgroup$Dynamic[]
around a rule-extraction rather than a variable, but as I spend more time reading up on them, I'm starting to think that may be possible. Time to experiment… $\endgroup$