Re: DynamicModule values not saved correctly in "docked" cells?
- To: mathgroup at smc.vnet.net
- Subject: [mg131573] Re: DynamicModule values not saved correctly in "docked" cells?
- From: John Fultz <jfultz at wolfram.com>
- Date: Thu, 5 Sep 2013 08:13:18 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <20130902064730.41C8E6A02@smc.vnet.net>
Yes, it is expected. Docked cells are built from the expression on the
rhs of the DockedCells option, and this option only changes when you
explicitly set the option. So, the in-memory docked cell may have
updated values, but these values will not be written to disk or MathLink
because doing so would require rewriting the rhs of the DockedCells
option (which might be impossible to do with full fidelity).
One easy way to deal with this issue is to use TaggingRules at the
notebook level as a sort of notebook-level DynamicModule variable.
Here's an example function to replace your content[] function for docked
cells:
dockedCellContent[] :=
Button[Dynamic[
CurrentValue[EvaluationNotebook[], {TaggingRules, "x"},
RandomReal[]]],
CurrentValue[EvaluationNotebook[], {TaggingRules, "x"}]++]
The technique is documented in the help system. Look up the topic
tutorial/StoringAndTrackingPaletteStates. However, I'm adding one novel
thing here which isn't documented there, which is the third argument of
CurrentValue. I could have initialized the CurrentValue[] by doing
something like:
DynamicModule[{}, (* body *), =
Initialization:>If[CurrentValue[EvaluationNotebook[], {TaggingRules,
"x"}]===Inherited,CurrentValue[EvaluationNotebook[],
{TaggingRules, "x"}]=RandomReal[]]]
but this undocumented third argument does exactly that much more
conveniently, and the format is unlikely to change (and probably will
get documented in a future version).
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.
On Sep 2, 2013, at 1:47 AM, Chris Osborn <chrisosb at gmail.com> wrote:
> It appears that for a DynamicModule expression in a "docked" cell,
symbol assignments are not correctly saved when saving the associated
notebook.
>
> In particular, when I save, close, and then reopen such a notebook, I
find that the value assigned to each DynamicModule symbol is the initial
value assigned to it, and not the value it had when the notebook was
saved.
>
> Is this behavior expected?
>
>
> I have written some code, included below, to demonstrate this issue
(tested on Mathematica 9.0.1.0). The code creates a new notebook with
one docked cell and one non-docked cell. Both cells have DynamicModule
expressions with a single variable in them, and have buttons that
increment the value of their respective variables. To reproduce my
issue:
>
> 1. Execute the code I give below
> 2. In that notebook that is created, click the button in each cell a
few times, causing the values displayed in the buttons to increase by 1
each time
> 3. Save the notebook and close it
> 4. Re-open the notebook
>
> The value in the non-docked cell will be as expected; the value in the
docked cell will be the original value (before clicking the button.)
Here is the code:
>
> ClearAll[content];
> content[] :=
> DynamicModule[{x},
> x = Random[];
> Button[Dynamic[x], x++]
> ];
>
> CreateDocument[
> {content[]},
> DockedCells -> Cell[BoxData@ToBoxes@content[], "DockedCell"]
> ];
>
>
>
> Much thanks in advance for any suggestions!
>
>
> Thanks,
> Chris
>
- References:
- DynamicModule values not saved correctly in "docked" cells?
- From: Chris Osborn <chrisosb@gmail.com>
- DynamicModule values not saved correctly in "docked" cells?