Group Abstract Group Abstract

Message Boards Message Boards

0
|
15.7K Views
|
8 Replies
|
1 Total Like
View groups...
Share
Share this post:

CloudDeploy with a Package. ExternalBundle?

POSTED BY: Ken Levasseur
8 Replies
Posted 9 years ago
POSTED BY: Scot Martin

Great, thanks. I wanted to make sure that there wasn't any subtlety that I was not aware of.

POSTED BY: David Reiss

David, yes, I use Unprotect and Protect out of habit. The kernel should not have any user definitions when the APIFunction is evaluated.

POSTED BY: Chad Knutson

Thanks Chad.

Just out of curiosity is there a particular reason for the Unprotect and Protect in the package? Or is that just your approach to package layout?

One reason why I can see why it might be desirable is in case the running Kernel on the cloud has a prior definition of the functions in the package. But I would assume that each time the API is called it runs on what is effectively a fresh Kernel.

POSTED BY: David Reiss

Apologies for the long delay in responding. The challenge with loading packages in the cloud is that the parser has to read the function name before the package is loaded. To make this work, you have to provide the context for the function.

Here is a simple example. First my package, saved as "myCloudPackage.m":

BeginPackage["myCloudPackage`"]
Unprotect[myFunc];
myFunc::usage="simple cloud function";
Begin["`Private`"]
myFunc[x_] := 1+x
End[]
Protect[myFunc];
EndPackage[]

I then upload the file to the cloud:

CopyFile["myCloudPackage.m", CloudObject["myCloudPackage.m"]]

Now I can deploy an APIFunction to use the package. Note that I'm using the full context of myFunc.

api = CloudDeploy[
  APIFunction[{"x" -> "Number"}, 
   CloudGet[CloudObject["myCloudPackage.m"]]; 
   myCloudPackage`myFunc[#x] &]]

Finally evaluate in Mathematica:

In[227]:= URLExecute[api, {"x" -> 3}]

Out[227]= "4"
POSTED BY: Chad Knutson

David, WordData is not an external library that one needs to load to use: Mathematica loads its content as needed whenever WordData is called.

Thus if you execute

func = APIFunction[{"str" -> "String"}, WordData[#str] &];

and then deploy it with

CloudDeploy[func]

you will get an API which will call WordData as needed and generate the expected output. For example executing the resulting API with the word "happy" gives this:

enter image description here

Ken, I haven't answered your question here. I haven't yet tried to make use of a package via a cloud account. I will experiment, but perhaps a more knowledgeable person will have a quick answer for you.

POSTED BY: David Reiss

I am also wondering this. I want to use WordData in a cloud deployed API. However, it doesn't say in the examples how to load any libraries or resources.

POSTED BY: David Johnston

One clarification. I put the package in the Applications folder of my cloud storage and can evaluate it with "less than less than"package name` in another Notebook in my cloud. That works fine. What I can't seem to do is use the package in an expression that I CloudDeploy.

POSTED BY: Ken Levasseur
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard