Group Abstract Group Abstract

Message Boards Message Boards

Pass a variable to Wolfram code when using Cloud Deploy?

Posted 8 years ago

We're new to Wolfram but in a very short time using the Web Development Platform have been able to develop a solution that presents business stats from system events logged in a Data Drop databin. This databin collects events and "tags" the system from which it came by adding a system ID (sID) as one of the variables logged. Our development solution hard codes the sID in the initialization section. What we would like to do is to pass the sID into the code as a variable in the Cloud Deploy URL which in turn we can pull out of the URL and use to "dynamically" set sID. The APIFunction seems to head in the right direction but I've not been able to figure out how to encapsulate all our code (initialization > creation of datasets > computation of datasets > presentation of stats) in the APIFunction body. In short, we're stuck so am hoping the Community can give some guidance.

THANKS!

POSTED BY: Randy Schultz
10 Replies

I find that it helps myself to come up with a simple, toy example of the problem I'm trying to solve. That often at least helps clear any misconceptions. Let me try to do that for your example:

You have a databin with some data and each entry is labeled with a specific site ID:

myBin = CreateDatabin[
  "Interpretation" -> <|"SiteID" -> "String", "Reading" -> "String"|>];

DatabinAdd[myBin, <|"SiteID" -> "a", "Reading" -> "11"|>];
DatabinAdd[myBin, <|"SiteID" -> "b", "Reading" -> "24"|>];
DatabinAdd[myBin, <|"SiteID" -> "a", "Reading" -> "36"|>];
DatabinAdd[myBin, <|"SiteID" -> "b", "Reading" -> "41"|>];

We might want to extract data from a specific SiteID. We can write a function to do this:

getSiteFromBin[bin_, site_] := 
 Map[Lookup["Reading"]]@
  Select[Lookup[#, "SiteID"] == site &]@Normal[bin]

There are different ways of writing this function. But you can test that it works:

getSiteFromBin[myBin, "a"]
{"11", "36"}

We can now build an API that runs this function on our databin:

CloudDeploy[
 APIFunction[{"SiteID" -> "String"}, 
  getSiteFromBin[myBin, #SiteID] &], "myAPI"]

CloudObject["https://www.wolframcloud.com/objects/me/myAPI"]

And it works:

URLExecute["https://www.wolframcloud.com/objects/me/myAPI?SiteID=a"]

Is this the kind of thing you're trying to do?

POSTED BY: Sean Clarke

So, APIs generally just return data. That's probably their smartest use-case. You build a website and then have something that queries an API to do the actual number crunching and then that website displays the result.

You can have an APIFunction return something more complicated like a webpage, or in this case, something as complicated as a CDF document. CDF is the WolframLanguage's web format for interactive documents:

CloudDeploy[
 APIFunction[{"yVar" -> "Number"}, 
  ExportForm[plotSineFunction[#yVar], "CloudCDF"] &], "spAPI"]
POSTED BY: Sean Clarke

Thanks Sean!

This example is EXACTLY what I was looking for!!!

POSTED BY: Randy Schultz

I followed the recommended steps.

Created function...

plotSineFunction[y_] := Manipulate[Plot[Sin[x (y + a x)], {x, 0, 6}], {a, 0, 2}]

Created APIFunction...

CloudDeploy[
 APIFunction[{"yVar" -> "Number"}, 
  plotSineFunction[#yVar] &], "spAPI"]

Called API with a yVar value of 2...

[https://www.wolframcloud.com/app/objects/user-2147c9f2-d003-455e-b130-9c3bc9a7b9b5/spAPI?yVar=2]

Output was not the manipulable sine plot but rather...

Manipulate[Plot[Sin[x(2 + ax)], {x, 0, 6}], {a, 0, 2}]

Is there a way to output the manipulable sine plot rather than the text output listed above?

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