5
$\begingroup$

This is closely related to the recent: Creating self destructing notebook.

However the answer doesn't work in my case.

A small example is:

ExportString[Dynamic[Print[1]], "NB"]

and it prints 1 :-(

A real word case is to save a notebook without triggering dynamics inside:

nb = Notebook[{
   Cell @ BoxData @ ToBoxes @ DynamicModule[{},
       "whatever",
       Initialization :> {Print["initialization"]}
   ]},
   NotebookDynamicExpression :> Refresh[Print["dynamicExpression"], None]
];

Now, I want to save a notebook without prompting Dynamic print statements.

Export[
 FileNameJoin[{$HomeDirectory, "Desktop", "silent.nb"}],
 nb, "NB"
]

enter image description here

Can I prevent that?

$\endgroup$
8
  • 1
    $\begingroup$ Worked for me without printing, M10.2 on Mac OS X 10.7.5. $\endgroup$ Mar 10, 2016 at 14:29
  • 1
    $\begingroup$ In fact, I was wrong. It printed to messages window, and somehow I didn't notice that. So the problem is real also in my case. Don't know off hand how would one solve it, alas. $\endgroup$ Mar 10, 2016 at 15:01
  • $\begingroup$ Not sure if the word bug fully describes this case, but IMO this is certainly an unwanted behavior. To call this a bug, we need to have somewhere in documentation a statement that Export to NB with dynamic elements does not leak evaluation in dynamics. I don't have the time right now to check whether any such statement was made somewhere in the docs. But in any case, this is certainly an inconvenience, and this behavior seems to be problematic in the majority of cases. $\endgroup$ Sep 9, 2016 at 13:20
  • $\begingroup$ @LeonidShifrin I think it is done when ImageSizeCache is calculated... $\endgroup$
    – Kuba
    Sep 14, 2016 at 10:58
  • $\begingroup$ Quite possibly you're right. You certainly have more experience with this stuff than me. $\endgroup$ Sep 14, 2016 at 11:05

1 Answer 1

4
$\begingroup$

Explanation

It is not a leak if this is intended, right? It happens because MMA is trying to determine ImageSizeCache for DynamicBox that will be generated.

ImageSizeCache is explained in

tutorial / AdvancedDynamicFunctionality / ImageSizeCache in Dynamic

My approach to fix it

This tutorial will teach us the the fix is to use a fixed value of ImageSizeCache and SynchronousUpdating -> Automatic.

The problem is that one may want to have a better control over SynchronousUpdating at the end.

To circumvent that we can add additional Dynamic which won't be triggered more than needed and will alow to preserve inner Dynamic's options:

ExportString[
  Dynamic[
    Dynamic[
      Print@1
    ],
    SingleEvaluation    -> True,
    SynchronousUpdating -> Automatic,
    ImageSizeCache      -> {28., {0., 9.}}
  ],
  "NB"
]

WRI support response

I felt like it is something very fundamental for working with UI developement in MMA so I asked WRI Support to help me.

It took a while but I received an answer, which is to suspend DynamicUpdating when Export takes place.

[...]

For the effect you want, turn off the global DynamicUpdating option for the fronend just before the call to Export, and then clear that setting after Export.

For example:

CurrentValue[$FrontEnd, DynamicUpdating] = False;
Export[file, nbexpr] 
CurrentValue[$FrontEnd, DynamicUpdating] = Inherited;

[...]

One should know it may not work well with Dynamics that have UpdateInterval option: Toggling DynamicUpdating aborts dynamics with UpdateInterval

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.