# 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.

-
 I do not think CDF supports packages. At least not CDF's used to make demos for the demo project. If this is confirmed, then only way is to include all the code you need in the CDF (this is what I do for demo CDF's). Lets assume it should work. Now you email this CDF file to someone who does not have this package on their system, or does not even have Mathematica. How will then the CDF find this package on your friend computer? CDF should be all self contained, at least if it is meant to be used a portable computable document. – Nasser Jan 26 at 8:38 @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 at 11:33 CDF deployed has to work initially without depending on a someone hitting the ENTER key. This is very important to know about CDF's. Any code used in the initial dynamics evaluation must be there without needing the shift-return. One way to do this is to have the code (the functions used) to be in the initialization section (another option is to use the SaveDefinitions option. There are another 2 or 3 ways to do this, but the above are the most common and direct ways. That is why you would need to copy any functions used from outside packages and paste the code in the initialization section. – Nasser Jan 26 at 11:50 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 at 12:01 Very hard to comment and help without code or screen grabs of the problem. – Mike Honeychurch Jan 26 at 21:25
show 1 more comment

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:

1. When using Needs inside Initialization use the full package directory in the second argument.
2. When using the functions from your package inside Initialization you should use it full name as connectionPackmyConn[]
3. 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.
-
 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 at 8:05 Have you tried to put it in Initialization?? Witch version is your CDF? Signed or not? – Murta Jan 27 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 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 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 at 21:29