Message Boards Message Boards

12 Replies
5 Total Likes
View groups...
Share this post:

Instant API works in Notebook but fails when deployed.

Posted 10 years ago

So I made a function in a notebook like so and it worked.

func = APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base]&];
func[<|"base" -> 5 , "digits" -> 20|>] 

output = 3.032322143033432411241224042

When I deploy it to the cloud using CloudDeploy[func] and then use the params ?digits=20&base=5

I get the following from the API

BaseForm[3.1415926535897932384626433832795028841971693993751058209749`20., 5]

Am I doing something wrong?


POSTED BY: Reed Silverstein
12 Replies
Posted 10 years ago

It looks like Import from a URL is not working in an API due to a bug, an overzealous security setting. We'll get that fixed soon.

POSTED BY: Joel Klein

Awesome! I thought it was just me sucking at learning this new WL. :) lol

POSTED BY: David Johnston

Seems like its fixed. :)

POSTED BY: David Johnston

I am having a similar issue with "Import." For some reason I can't get it to work as an Instant API. I tried CloudImport as well. Any ideas?

My Code:

  "urlTargets" -> "String" -> "",
  func2 = data = Import[#urlTargets, "Hyperlinks"]
POSTED BY: David Johnston
Posted 10 years ago

According to the documentation, BaseForm is interpreted by the notebook interface to display a certain way, but it does not affect evaluation. When you CloudDeploy an APIFunction, its output format is Wolfram Language by default, so you're seeing what's expected.

There are several options, depending on what you're going for:

  1. Give the output as an image, specifying the "PNG" format as a second argument to APIFunction:

    CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"},
        BaseForm[N[Pi, #digits], #base]&,"PNG"]]
  2. Make the output text format by running BaseForm through ToString, and specifying "Text" format. The base is put on a second line to show it as a subscript:

    CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"},
        ToString[BaseForm[N[Pi, #digits], #base]]&,"Text"]]
  3. If you want to return a text response giving just the numeric value without showing the base, you could use RealDigits.

For readability, first we break out a function piDigits:

piDigits[base_, digits_] :=
   RealDigits[N[Pi, digits], base] /. {
      {rdigits_List, nleft_} :> StringJoin@{ToString /@ Take[rdigits, nleft], ".", ToString /@ Drop[rdigits, nleft]}

Then we deploy an API based on it that returns Text:

CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"}, piDigits[#base, #digits]&, "Text"]]
POSTED BY: Joel Klein

I checked with developers on this and found out that BaseForm assumes a Mathematica-like front end.
That is why it works in Wolfram Programming Cloud.

BaseForm is interpreted by the FE to print the number in that form in a notebook. By default CloudDeploy returns Wolfram Language so the result he is seeing isn't formatted because it is WL that is not displayed in a notebook. I tried

CloudDeploy[ APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base] &,"HTML"]]

with HTML output and got closer to the expected result, but the formatting isn't perfect. This format is probably closer to the user intention, though. ****

POSTED BY: Bruce Miller

Nice, I think that is good enough for what I had in mind.

Thanks Bruce!

POSTED BY: Reed Silverstein

CloudDeploy[ APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base] &], Permissions -> "Public"]

POSTED BY: Reed Silverstein

What is the full CloudDeploy function call (with options) you used?

POSTED BY: Bruce Miller

I set it to Public and i'm still having an issue. Also Mario i'm trying to pass in the parameters to the API , not a static response.

POSTED BY: Reed Silverstein


did you set the Permissions to "Public"?

I tried

CloudDeploy[ APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base]&];func[<|"base" -> 5 , "digits" -> 20|>]         ,Permissions -> "Public"]

and got a link to this website:

enter image description here

Cheers, Marco

POSTED BY: Marco Thiel
Posted 10 years ago

Marco, what you're seeing there isn't a result of setting Permissions->"Public". Note that your APIFunction expression ends in a semicolon. What you actually ended up CloudDeploying is the result of calling func with some arguments, which is a simple expression. By default, CloudDeploy of an expression is rendered as a notebook as you showed.

POSTED BY: Joel Klein
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract