# Function saved in a package won't load in the deployed CDF on startup

I wrote a function and saved it in a package. I saved a copy of the package in the Autoload folder too. I deployed a CDF that contains this function. When I open the CDF later, this function (which represents modified input fields) is not evaluated. But when I do any operation that evaluates this function (like a button click), it gets evaluated and the cdf works just fine.

I am using Mathematica 9 to deploy the cdf and also to run it to check it.

Updated, the package's code down below -

BeginPackage["ModifiedInputFields"] InputFieldsCreation[noOfrows_,
noOfInputFields_] :=
(

Module[
{inputvaluelist = Table[0, {noOfrows}, {noOfInputFields}]},
(
If[
(noOfrows > 0),
(*If noOfrows value is greater than 0*)

(
{
Table[
(
With[{i = i, j = j}, (Row[{

Dynamic[InputField[
Dynamic[inputvaluelist[[i, j]]],
Number,
ContinuousAction -> True,
ImageSize -> {50, 20},
Alignment -> Center

]]
}])]
(*with is closed*)
),
{i, 1, noOfrows},
{j, 1, Length[Part[inputvaluelist, i]]}
],(*Table closed*)
Dynamic[inputvaluelist]
}

),
("")
]
)
]
(*Module closed*)
)
EndPackage[]


What I am looking at is to also be able to extract all the inputs that the user gives in the input fields into a list that we pass on to other functions. So that is a critical feature, which I am worried might also be a reason for the package giving trouble at cdf startup.

-
@Nasser the person I am intending to send this cdf has mathematica installed, and I am planning on sending the packages to them too. They will also be running the cdf from mathematica 9. I see this problem when I try to open this cdf from another computer as well as when I re-open mathematica. Any button press that has the function evaluation as an action makes the function work, but not when the cdf is just started. I get big red boxes across all the modified input fields i created. –  Murali Jan 26 '13 at 11:33
We've done this before, writing packages and then using Needs to call them and then the deployed cdf working just fine. We use Dynamic Module regularly instead of Manipulate, never used SaveDefinitions option though, I don't think it is available for Dynamic Module. –  Murali Jan 26 '13 at 12:01
Very hard to comment and help without code or screen grabs of the problem. –  Mike Honeychurch Jan 26 '13 at 21:25
@MikeHoneychurch I am editing the question and adding the code, all of my package –  Murali Jan 27 '13 at 7:44

If you use Enterprise Mathematica edition, or some kind of signed version, you can do what you want. In this example I use the CDF with package to hide a database connection, so the package can be distributed without someone discover your connection user name and password.

There is the package, that should be saved together with the CDF file (same directory):

BeginPackage["connectionPack",{"DatabaseLink"}]

marcheConn::usage="myConn[] connect to database";

Begin["Private"]

myConn[] :=
OpenSQLConnection[
];

End[]
EndPackage[]


Here is the code for the test CDF

testCDF=DynamicModule[{},

Column[{
Button["Random Sample from Database",output=test[]],
Button["Current directory",output=Directory[]],
Dynamic[output//TableForm]
}]

,Initialization:>(
SetDirectory[NotebookDirectory[]];
Needs["connectionPack",NotebookDirectory[]<>"connectionPack.m"];
test[]:=Module[{conn,r},
conn=connectionPackmyConn[];
r=SQLExecute[conn,"select * from someTable"];
CloseSQLConnection[conn];
RandomSample[r,5]
];
)
]


Now you can export this using:

CDFDeploy["test.cdf", testCDF, "Method" -> "Standalone", "Target" -> "PlayerPro"]


Important points:

• When using Needs inside Initialization use the full package directory in the second argument.
• When using the functions from your package inside Initialization you should use it full name as connectionPackmyConn[]
• Just for completeness of this example. If you wand to protect you connection string you could Encode and use SetAttributes[myConn, {ReadProtected,Locked}];. This way user can not retrive it using ??myConn.

## update

I discovered that the encryption do not solve the conn security. When you create the connection, you can retrieve all connections parameters just executing conn. The only advantage is that the user needs Mathematica to do this, and user and pass will not be in plain text.

-
I have edited my question with my code. In your code, I want to see it work when the first button, whose action is output=test[] to not be there, instead that action should happen automatically when the cdf opens. –  Murali Jan 27 '13 at 8:05
Have you tried to put it in Initialization?? Witch version is your CDF? Signed or not? –  Murta Jan 27 '13 at 12:53
Yes, tried initialization, I think the cdf is unsigned. We are developing and evaluating internally using Mathematica 9 standard license. –  Murali Jan 27 '13 at 13:52
I understand. I think that to do what you want you need a signed version. To make the code of my post work I used the Enterprise Version. –  Murta Jan 27 '13 at 14:00
Did you actually test the use of ReadProtected? I seem to remember that this attribute was not allowed -- but may be wrong. –  Mike Honeychurch Jan 27 '13 at 21:29

From the docs: "Because CDF Player cannot load custom data at runtime, you must ensure that all necessary information is embedded within the interactive elements of your .cdf file. This can be done with either Initialization or SaveDefinitions; both are options to Manipulate."

-
the cdf is being tested in mathematica itself. Meant to be tested for proper running in Mathematica, before we finally aim to push it for use in player pro, but that is in the future. It should have worked when opened in Mathematica. I've seen this thread -> Understanding CDF But discussion centers around cdfs for cdf players and player pro's –  Murali Jan 26 '13 at 11:51