5
$\begingroup$

Cross posted on community.wolfram.com

Question and requirements

Is it possible to HTTPRedirect from a Cloud CDF via e.g. Button action?

  • I need an interactive cloud cdf around it.

  • The action would not only redirect but evaluate arbitrary code prior to that.

  • If it matters the target page does not need to be any page but an arbitrary CloudObject is enough.

  • No, I don't want to hide old stuff and show new to the user, I really need to redirect.

  • No, open in new tab does not count.

So it would work analogously to this form's submit button:

CloudDeploy[
    FormFunction[{"go on and submit"}
      , Module[{link}
          , link = CloudPut[RandomReal[], Permissions -> "Public"]
          ; HTTPRedirect[link]
        ] &
    ]
  , Permissions -> "Public"
]

Problems

Wasn't able to achieve that, here's why:

  • Form/ApiFunctions where HTTPRedirect works well can't have interative CDFs embedded inside:

     CloudDeploy[
         FormFunction[
             { "this is ok"
             , ExportForm["this is not ok", "CloudCDFElement"]
             }
           , foo
         ]
       , Permissions -> "Public"
     ]
    

    enter image description here

  • and I can't trigger HTTPRedirect from cloud CDF, nothing happens:

        CloudDeploy[
          ExportForm[
            Button["Submit", Print[1]; HTTPRedirect["http://www.wolfram.com"]], 
            "CloudCDFElement"
          ], Permissions -> "Public"
         ]
    

    enter image description here

$\endgroup$
  • $\begingroup$ Do you need both an actual and a hyperlink? You can obviously restyle a Hyperlink to look like a button. A SystemOpen call works too, except it's viewed as a popup. Is that okay? $\endgroup$ – b3m2a1 Aug 4 '17 at 15:59
  • $\begingroup$ @b3m2a1 nope, see above requirements :) $\endgroup$ – Kuba Aug 4 '17 at 16:15
  • $\begingroup$ Ah. That's tougher. Well I'll keep playing with it. $\endgroup$ – b3m2a1 Aug 4 '17 at 16:16
  • $\begingroup$ So I took a break and came back to it and I'm not entirely sure WRI's made this possible yet. If you open a cloud notebook and make a hyperlink there, you can disable "Open in New Tab", but it doesn't actually do that. Since we don't have access to JS it's a lot harder. EmbeddedHTML seems to only let you work with iframes and I can't access the StyleDefinitions in the cloud so clearly they're baked in. $\endgroup$ – b3m2a1 Aug 4 '17 at 19:48
  • $\begingroup$ @b3m2a1 thanks for your time, yes I was afraid it may be impossible :-/ Cloud front end setup is way more obscure than desktop's so one never knows what is implemented/isn't/won't be etc. $\endgroup$ – Kuba Aug 4 '17 at 20:01
5
$\begingroup$

Here are results of my experiments, so far works:

CloudDeploy[
    ExportForm[
        DynamicModule[{x = 1}, Column[{
            Slider@Dynamic@x,
            Dynamic@x,
            Button["Test redirect",
                Module[{link},
                    link = "https://mathematica.stackexchange.com";
                    Print @ EmbeddedHTML[ StringTemplate[
                       "<script>window.top.location.href =\"``\"</script>"
                    ]@link
                ]
            ]
        ]}]]
      , "CloudCDF"]
    , Permissions -> "Public"
  ]

.top. was added because EmbeddedHTML translates to an iframe and we don't want to redirect only that iframe itself. On the other hand your cloud object may be embedded in another page and .top. will redirect the whole page. You may need that or not, depends, at the end I'm using .parent. because I want to redirect only the CloudObject parent iFrame.

$\endgroup$
  • $\begingroup$ Wow I was doing that but just with the window.location.href. Good to see it's as simple as adding a top. $\endgroup$ – b3m2a1 Aug 5 '17 at 12:50
  • $\begingroup$ I found this Q/A very helpful - much thanks! BTW... CloudDeploy[ ExportForm[ Manipulate[ Column[{ a, Button["Test redirect", Module[ {link}, link = "mathematica.stackexchange.com"; Print@EmbeddedHTML[ StringTemplate[ "<script>window.top.location.href =\"``\"</script>"]@link]] ] }], {a, 0, 2}], "CloudCDF"], "testButtonV2", Permissions -> "Public"] My preference is Manipulate over DynamicModule but it's situational. $\endgroup$ – Mitch Feb 28 '18 at 19:20
  • $\begingroup$ @Mitch I'm glad you find it useful. I converted your answer to a comment which it is. Hope you don't mind. $\endgroup$ – Kuba Feb 28 '18 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.