Guest User

Untitled

a guest
Jan 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. BeginPackage["TestPackage`"]
  2. f::usage="f[x] returns a plot.";
  3. Begin["`Private`"]
  4. f[x_]:=Plot[Sin[x y],{y,0,2 Pi},PlotRange->{-1,1},ImageSize->500]
  5. End[]
  6. EndPackage[]
  7.  
  8. Needs["TestPackage`", NotebookDirectory[] <> "TestPackage.m"]
  9. DynamicModule[
  10. {a = 1},
  11. Column[{
  12. Slider[Dynamic[a]],
  13. Dynamic[f[a]]
  14. }]
  15. , Initialization :> (
  16. Needs["TestPackage`", NotebookDirectory[] <> "TestPackage.m"]
  17. )
  18. ]
  19. Export[NotebookDirectory[] <> "output.cdf", EvaluationNotebook[]]
  20.  
  21. Initialization :> (
  22. ImportString[Import[NotebookDirectory[] <> "TestPackage.m", "Text"],
  23. "NB"]
  24. )
  25.  
  26. Needs @ yourPackage (*a*)
  27.  
  28.  
  29. CDFDeploy @ With[
  30. { source = Import[packagePath] (*b*) }
  31. , DynamicModule[
  32. { ... }
  33. , ...
  34. , Initialization :> ( Get @ StringToStream @ source )
  35. ]
  36.  
  37. content = "
  38. BeginPackage["TestPackage`"]
  39. f::usage="f[x] returns a plot.";
  40. Begin["`Private`"]
  41. f[x_]:=Plot[Sin[x y],{y,0,2 Pi},PlotRange->{-1,1},ImageSize->500]
  42. End[]
  43. EndPackage[]";
  44.  
  45. path = FileNameJoin[{$TemporaryDirectory, "package.m"}];
  46. pathEnc = FileNameJoin[{$TemporaryDirectory, "package.enc"}];
  47. pathCDF = FileNameJoin[{$TemporaryDirectory, "CDF.cdf"}]
  48.  
  49. Export[path, content, "Text"];
  50.  
  51. Encode[path, pathEnc]; (*c*)
  52. encodedpackage = Import[pathEnc, "Text"];
  53.  
  54.  
  55. Get @ pathEnc (*a*)
  56.  
  57. CDFDeploy[
  58. pathCDF, #, Method -> "Standalone", "Target" -> "CDFPlayer"
  59. ] & @ With[{
  60. source = encodedpackage (*b*)
  61. },
  62. DynamicModule[{x = 1, stream},
  63. Column[{
  64. Dynamic[{x, f[x]}],
  65. Button["x++", x++],
  66. Dynamic @ $ContextPath
  67. }]
  68. ,
  69. UnsavedVariables :> {stream}
  70. ,
  71. Initialization :> (
  72. stream = StringToStream @ source;
  73. Get @ stream;
  74. Close @ stream;
  75. )
  76. ]
  77. ]
  78.  
  79. SystemOpen @ DirectoryName @ pathCDF
Add Comment
Please, Sign In to add comment