Our Mathematica application obtains a group of input files, analyzes them and generates a report in a form of CDF document. The report contains multiple dynamic/interactive graphs and tables. To save the all required data inside the CDF we use Manipulate with SaveDefinitions ->True. When a section containing a dynamic object appears on a screen, an EnableDynamic button also appears on a top-right corner. To interact with the dynamic object, one should first click on the EnableDynamic button.
The problem is that when we try to visually compare two reports (based on two different sets of input files), the dynamic content of the second report overwrites the dynamic content of the first one. This can be avoided if the two CDFs are opened in separate Windows processes. But this is inconvenient and difficult to explain to users.
Is there an approach that solves this problem by, possibly, generating of unique name spaces for each automatically generated CDF report?
The code illustrating the structure of our report generating package is presented below:
BeginPackage["ExampleReportGenerator`"];
If[
StringLength[$InputFileName] >0 && ".m"==StringTake[$InputFileName,-2],
theDirectory=Directory[],
theDirectory = NotebookDirectory[]
];
SetDirectory[theDirectory];
folderName = FileNameSplit[theDirectory][[-1]];
data = Import["data.csv"]//Flatten;
interactiveData = Manipulate[
With[{dataToShow = Take[data, lengthToshow]},ListPlot[dataToShow]],
{{lengthToshow, Length[data], "length to show"}, 1, Length[data],1},SaveDefinitions->True];
UsingFrontEnd[
With[{reportCells = {
Cell["Example Report\nfolder name: "<>folderName,"Title"],
Cell["Interactive Data","Section",CellTags->"Interactive Data"],
Cell[BoxData[ToBoxes@interactiveData],"Output",ShowStringCharacters->False],
}},
With[{reportDocNB = CreateDocument[reportCells,ShowGroupOpener->True]},
With[
{actionsMenu =ActionMenu[Style["Example Report_"<>folderName,Bold],{
"Interactive Data":>{NotebookLocate["Interactive Data"],FrontEndExecute[{FrontEndToken["SelectionOpenAllGroups"]}]}
}]},
SetOptions[reportDocNB,DockedCells->Cell[BoxData[ToBoxes@Row[{" "," ",actionsMenu}]]]]
];
SelectionMove[reportDocNB,All,Notebook];
FrontEndTokenExecute["SelectionCloseAllGroups"];
NotebookSave[reportDocNB,FileNameJoin[{theDirectory, "Example_Report_" <>folderName<>".nb"}]];
CDFDeploy[FileNameJoin[{theDirectory, "Example_Report_" <>folderName<>".cdf"}],reportDocNB];
]];
]
EndPackage[]
If this package runs in a directory where data.csv file contains roots of integers it generates a report like this:
If the data.csv file contains second powers of integers the report present the appropriate interactive visualization.
The problem appears when the two cdf documents are opened together. In this case the dynamic content of the first cdf is overwritten by the second one (we see powers instead of roots):


DynamicModuleto scope your variables (probably withUnsavedVariables) or make your CDFs/NBs withCellContext -> Notebookand incorporate$CellContext`as described in: [How does$CellContext`work?](http://mathematica.stackexchange.com/a/102391/5478) – Kuba Apr 09 '16 at 18:22