Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15371 > unrolled thread

Re: DynamicModule values not saved correctly in "docked" cells?

Started byJohn Fultz <jfultz@wolfram.com>
First post2013-09-05 12:06 +0000
Last post2013-09-07 10:00 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: DynamicModule values not saved correctly in "docked" cells? John Fultz <jfultz@wolfram.com> - 2013-09-05 12:06 +0000
    Re: DynamicModule values not saved correctly in "docked" cells? Chris Osborn <chrisosb@gmail.com> - 2013-09-07 10:00 +0000

#15371 — Re: DynamicModule values not saved correctly in "docked" cells?

FromJohn Fultz <jfultz@wolfram.com>
Date2013-09-05 12:06 +0000
SubjectRe: DynamicModule values not saved correctly in "docked" cells?
Message-ID<l09s4f$ja7$1@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@wolfram.com
User Interface Group
Wolfram Research, Inc.



On Sep 2, 2013, at 1:47 AM, Chris Osborn <chrisosb@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
>

[toc] | [next] | [standalone]


#15377

FromChris Osborn <chrisosb@gmail.com>
Date2013-09-07 10:00 +0000
Message-ID<l0etf8$212$1@smc.vnet.net>
In reply to#15371
Hi John,

Thanks very much for your very thorough answer; it is quite helpful.


Chris

On Thursday, September 5, 2013 5:06:39 AM UTC-7, John Fultz wrote:
> 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@wolfram.com
> 
> User Interface Group
> 
> Wolfram Research, Inc.
> 
> 
> 
> 
> 
> 
> 
> On Sep 2, 2013, at 1:47 AM, Chris Osborn <chrisosb@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
> 
> >

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web