# Create a package for a function in .wl format?

**URL:** <https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502>\
**Category:** Questions\
**Tags:** mathematica, computable-document-format, interface-construction, packages-and-paclets\
**Created:** [May 6, 2016, 9:07am UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502 "2016-05-06T09:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erdemuguz1](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@erdemuguz1](https://community.wolfram.com/u/erdemuguz1)\
**Post date:** [May 6, 2016, 9:07am UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502/1 "2016-05-06T09:07:45Z")

</div>

Hello,  
I have been developing a CDF file (a toy problem) to teach me how to use CDF file for more serious codes deployment. I got many help in here and I am thankful for that. Before Mathematica I was using MATLAB extensively and I feel like MATLAB is much more user friendly than Mathematica, specially in developing subroutines (packages for Mathematica) . Here is the code without package but with a function and it works fine.

```
Manipulate[
 DynamicModule[{f = Sin[x], xR = 1, xL = 0, 
   Res = 0.4596976941318603`16}, 
  Column[{Style["1D Definite Integral Calculator", "Function"] , 
    Row[{"Lower Limit ", InputField[Dynamic[xL], Number]}, Spacer[5]],
     Row[{"Upper Limit ", InputField[Dynamic[xR], Number]}, 
     Spacer[5]], 
    Row[{"Function ", InputField[Dynamic[f]]}, Spacer[20]], 
    Button[Style["Calculate", Green, Bold], 
     Dynamic[Res = WD1Int[f, xR, xL, np, nw];]], 
    Row[{"Result", InputField[Dynamic[Res], Enabled -> False]}, 
     Spacer[51]], 
    Row[{"Plot the Function ", Checkbox[Dynamic[fp], {False, True}]}],
     If[fp == True, 
     InputField[
      Dynamic[Plot[f, {x, xL, xR}, Dynamic[PlotLabel -> f], 
        ImageSize -> Full]], FieldSize -> {30, 15}]], 
    Button[Mouseover[Style["http://www.wolfram.com", "Hyperlink"], 
      Style["http://www.wolfram.com", "HyperlinkActive"]], 
     NotebookLocate[{URL["http://www.wolfram.com"], None}], 
     Appearance -> None]}]], {{np, 16, "NumberFormat"}, 
  ControlPlacement -> Bottom}, {{nw, 16, "WorkingPrecision"}, 
  ControlPlacement -> Bottom}, Initialization :>
  (WD1Int[f_, xR_, xL_, np_, nw_] := 
     Module[{xLcheck, xRcheck}, xLcheck = NumberQ[xL];
      xRcheck = NumberQ[xR];
      If[xLcheck == True && xRcheck == True, 
       WResultTF = 
        SetPrecision[
         NIntegrate[f, {x, xL, xR}, 
          WorkingPrecision -> Re[IntegerPart[nw]]], 
         Re[IntegerPart[np]]], 
       "Enter numeric inputs for Lower and Upper Limits"]];)]

```

I would like to use package for my function so I can Encode it (I am using Enterprise CDF). During this process I have been facing several problems. Here is how create the package and updated the function. I use .wl because I have MATLAB in my computer too.

```
Manipulate[
 DynamicModule[{f = Sin[x], xR = 1, xL = 0, 
   Res = 0.4596976941318603`16}, 
  Column[{Style["1D Definite Integral Calculator", "Function"] , 
    Row[{"Lower Limit ", InputField[Dynamic[xL], Number]}, Spacer[5]],
     Row[{"Upper Limit ", InputField[Dynamic[xR], Number]}, 
     Spacer[5]], 
    Row[{"Function ", InputField[Dynamic[f]]}, Spacer[20]], 
    Button[Style["Calculate", Green, Bold], 
     Dynamic[Res = TFPackageSourceCode`WD1Int[f, xR, xL, np, nw];]], 
    Row[{"Result", InputField[Dynamic[Res], Enabled -> False]}, 
     Spacer[51]], 
    Row[{"Plot the Function ", Checkbox[Dynamic[fp], {False, True}]}],
     If[fp == True, 
     InputField[
      Dynamic[Plot[f, {x, xL, xR}, Dynamic[PlotLabel -> f], 
        ImageSize -> Full]], FieldSize -> {30, 15}]], 
    Button[Mouseover[Style["http://www.wolfram.com", "Hyperlink"], 
      Style["http://www.wolfram.com", "HyperlinkActive"]], 
     NotebookLocate[{URL["http://www.wolfram.com"], None}], 
     Appearance -> None]}]], {{np, 16, "NumberFormat"}, 
  ControlPlacement -> Bottom}, {{nw, 16, "WorkingPrecision"}, 
  ControlPlacement -> Bottom}, Initialization :>
  (Needs["TFPackageSourceCode.wl"];)]

```

and the package TFPackageSourceCode.wl follows like this

```
   BeginPackage["TFPackageSourceCode`"];
WD1Int::usage="";
Begin["`Private`"];
    WD1Int[f_,xR_,xL_,np_,nw_]:=Module[{xLcheck,xRcheck},xLcheck=NumberQ[xL];
    xRcheck=NumberQ[xR];
    If[xLcheck==True&&xRcheck==True,WResultTF=SetPrecision[NIntegrate[f,{x,xL,xR},WorkingPrecision->Re[IntegerPart[nw]]],Re[IntegerPart[np]]],"Enter numeric inputs for Lower and Upper Limits"]];
    SetAttributes[WD1Int,{ReadProtected,Protected,Locked}];
    End[];
    EndPackage[];

```

Both the package and the main file is in the same folder and it is in the $Path. (I think only having the package in the $Path should be enough). But the code does not work without any error.

 ![ScreenShot2016-05-06at10.34.18AM.png](https://global.discourse-cdn.com/wolfram/original/2X/4/47b49c0a9459715f9b109f07b5e849159c577115.png)

I also tried to use this line for initialization

```
(Get[FileNameJoin[{NotebookDirectory[EvaluationNotebook[]],
"TF1DINTPackageSourceCode.wl"}]];)]

```

They both don’t work and I don’t know why. To be honest, I have been creating many files to improve the code in every step and I have some files working with packages some are not. That is why I said ANNOYING in the tittle. I deleted all my files and started to work on from zero so I can clearly create working code in a single file which later on I can mimic.

My other major problem is shadowing and private and global variable problem. Some times my variables are red and I get an warning that there are more than one variable named same (not in the same nb) and one of the definition can be shadowed and it can be the current definition. That means if I have 2 versions of the same code (maybe all the variable are same just different coding) Mathematica will be seeing the second function/package too. For me it is really inconvenient and I dont see the advantage of having just Kernel memory. I get this error even my package is “Private” . So what does Private do for me?

 ![ScreenShot2016-05-06at10.43.54AM.png](https://global.discourse-cdn.com/wolfram/original/3X/6/b/6b113f745942e8f516351940a059cd3d1e39f033.png)

I used ClearAll at the beginning of my code but I assume it is not same effect as it is in MATLAB. I don’t understand why each nb can’t be self contained and variable stay in there.

> [@Global vs Private in nb but CDF is working fine.](https://wolfram.discourse.group/t/8476/2):
>
> All new(not found) symbols are created in current $Context so MyIntegrate[func\_]:= NIntegrate[func,{x,0,1}] Will introduce x in Global` context while BeginPackage["MyPackage`"]; MyIntegrate::usage=""; Begin["`Private`"]; MyIntegrate[func\_]:=NIntegrate[func,{x,0,1}]; End[]; EndPackage[]; MyIntegrate[Sin[x]] NIntegrate[Sin\[x\], {MyPackage`Private`x, 0, 1}] is creating definition with MyPackage`Private`x. So the function is integrated over different variable. Additionally some subtleti…

Here I asked the question for that and Kuba is trying to help me. I know that this should not be this hard.

Thank you for the replies.

ps: I could not upload .wl file because the forum did not allow that extension.

---

<div class="post-metadata">

**Author:** ![jbaldwin](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jbaldwin](https://community.wolfram.com/u/jbaldwin)\
**Post date:** [May 6, 2016, 2:56pm UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502/2 "2016-05-06T14:56:04Z")

</div>

For whatever it’s worth, I couldn’t get the first example to work using _Mathematica_ 10.4.1 (Windows 10) until I moved the function `WD1Int` outside of the `Manipulate`.

But in any event, isn’t the problem that _CDF_ does not allow text input? Numerical input, yes. Text input no. So such things work in _Mathematica_ but not in the _CDF_ _Reader_. You could replace the `InputField` with a `PopupMenu`. And using `SaveDefinitions -> True` would also fix the inclusion of functions outside of the `Manipulate`.

---

<div class="post-metadata">

**Author:** ![erdemuguz1](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@erdemuguz1](https://community.wolfram.com/u/erdemuguz1)\
**Post date:** [May 6, 2016, 3:43pm UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502/3 "2016-05-06T15:43:34Z")

</div>

I just copy and paste the first example (to check if I didn’t transfer correctly while posting) but it is working fine. I used enterprise CDF with Enterprise CDF you can create string inputs.

when you move the function outside the Manipulate how are you going to “Manipulate” . Here a version of the code without Manipulate.

```
DynamicModule[{f = Sin[z], zR = 1, zL = 0, Res = 0.45969},
 Column[{Style["1D Definite Integral Calculator", "Function"] , 
   Row[{"Lower Limit ", InputField[Dynamic[zL], Number]}, Spacer[5]], 
   Row[{"Upper Limit ", InputField[Dynamic[zR], Number]}, Spacer[5]], 
   Row[{"Function ", InputField[Dynamic[f]]}, Spacer[20]], 
   Button[Style["Calculate", Green, Bold], 
    Dynamic[Res = DTL[f, zR, zL, 16, 16];]], 
   Row[{"Result", InputField[Dynamic[Res], Enabled -> False]}, 
    Spacer[51]],
   }],
 Initialization :> (DTL[f_, zR_, zL_, np_, nw_] := 
    Module[{xLcheck, xRcheck}, xLcheck = NumberQ[zL];
     xRcheck = NumberQ[zR];
     If[xLcheck == True && xRcheck == True, 
      ResultTF1 = 
       SetPrecision[
        NIntegrate[f, {z, zL, zR}, 
         WorkingPrecision -> Re[IntegerPart[nw]]], 
        Re[IntegerPart[np]]], 
      "Enter numeric inputs for Lower and Upper Limits"]])]

```

and to be honest I am not sure what is the difference between Manipulate and DynamicModule because you can create dynamic input fields just with manipulate too.

---

<div class="post-metadata">

**Author:** ![gdelfino](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/gdelfino/32/41294_2.png) [@gdelfino](https://community.wolfram.com/u/gdelfino)\
**Post date:** [May 6, 2016, 3:44pm UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502/4 "2016-05-06T15:44:18Z")

</div>

Hello Erdem, I also deploy to EnterpriseCDF and I use a package to contain most of the non GUI code. I do it like this:

Needs[“MyPackage`"]; myGUI=Manipulate[(...),Initialization :> (Needs["MyPackage`”]), SaveDefinitions → True];

To avoid issues related to SaveDefinitions, I always quit the kernel, evaluate the initialization cells (including code above) and then CDFDeploy[] it.

For more info I recommend you this excellent presentation “User Interface Programming” by Faisal Whelpley and  
Lou D`Andria: [User Interface Programming: Principles of GUI Construction -- from Wolfram Library Archive](http://library.wolfram.com/infocenter/Conferences/7977/)

Regards,

Gustavo Delfino

---

<div class="post-metadata">

**Author:** ![erdemuguz1](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@erdemuguz1](https://community.wolfram.com/u/erdemuguz1)\
**Post date:** [May 8, 2016, 2:39pm UTC](https://community.wolfram.com/t/create-a-package-for-a-function-in-wl-format/8502/5 "2016-05-08T14:39:48Z")

</div>

Thank you for the link. I will for sure look into it.  
I think my main problem is with the private command in the package. When I remove

WD1Int::usage=“”;  
Begin[“`Private`”];\*)

part from the package and run the Quit command (as you suggested) before running the main file. I don’t have a problem. That raises the question what is the problem with making package Private.  
Why do you include Needs[“TFPackageSourceCode.wl”] at the beginning and again in the initialization part ?
