Just starting up on the various cloud functions, and trying to get my mind around them. I am making the transition from webMathematica (JSP, etc.) over to trying out the suite of Cloud functions. I think that I have figured out that CloudDeploy[ ] makes a static definition in that, if I update functions in packages, those updated function don't propagate through to CloudDeploy[ ]. That is a lot different from desktop Mathematica. I have learned instead that I have to execute CloudDeploy again to get the updated functions incorporated into the deployed behavior. In any case, I have learned. Please do correct me if I am missing something on this point (<-there is a question buried in there).
So why am I replying to this discussion: Chad Knutson's post was VERY INFORMATIVE to me. Nevertheless, I didn't really like that it was loading a package without Needs[ ] and mixing the package loading with the function definition. So, below I am posting an alternative format that accomplishes nothing different from Chad Knutson's post but it does do it in a different format. Namely, it separates the package from the API function. Maybe others would find this format helpful, too, so I am posting. Here follows:
Make the package:
BeginPackage["myCloudPackage`"]
myFunc::usage = "Random number generator"
Begin["`Private`"]
myFunc[] := RandomReal[]
End[]
EndPackage[]
Copy over file:
CopyFile["myCloudPackage.m", CloudObject["myCloudPackage.m"]]
Convince you that no cheating is involved so now start with blank slate:
Quit[]
Deploy to the cloud in the alternative format that separates package declaration from function content:
api =
CloudEvaluate[
CompoundExpression[
Needs["myCloudPackage`", "myCloudPackage.m"],
CloudDeploy[APIFunction[{}, myCloudPackage`myFunc[] &]]
]
]
Check out the results:
{URLExecute[api], URLExecute[api]}
An interesting question (to me) is why the following code actually does not work since the context is getting updated in the cloud. The fact that it does not work is revealing/instructive about how the suite of Cloud functions differs from interactions with the usual desktop Mathematica:
api =
CloudEvaluate[
CompoundExpression[
Needs["myCloudPackage`", "myCloudPackage.m"],
AppendTo[$ContextPath, "myCloudPackage`"],
CloudDeploy[APIFunction[{}, myFunc[] &]]
]
]
I think the answer lies in Chad Knutson's comment about the 'parser' but there does not seem to be any information on this in the documentation. Any further information on how to deal with Contexts, Packages, or storing user information between interactions for say 30 min (beyond FormFunction[ ] and FormPage[ ]) would be valuable to me (<-there's another question buried in there).
Thank you for reading my post and I hope you found a nugget of something interesting to you.