3

I have a Javascript snippet that my Jupyter Notebook installs into the browser page. It accesses a REST-enabled interactive application running on my PC (i.e., Cytoscape).

An example of a Cytoscape call from Javascript is "GET http://localhost:1234/v1/version" ... executed by the normal xmlHTTPRequest.send() call.

On other Jupyter Notebook systems (e.g., GenePattern Notebook), this works fine. Looking at Chrome's networking trace, it looks like my HTTP call is being intercepted, and a separate call to colab that seems to be trying to authorize the call(???). Here is what Chrome shows in the debug console:

enter image description here

The colab call seems to happen here: https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20200817-085601-RC01_327067882

In the end, my call seems to be getting disallowed ... I get an HTTP status 500.

Any idea how I can convince colab to let my Javascript-level call to my local Cytoscape go through?? Any information could be helpful.

Thanks!

bdemchak
  • 263
  • 2
  • 10

1 Answers1

3

Requests to localhost in Colab are redirected to the VM backend.

Details are here: https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=R8ZvCXC5A0wT

A work-around is to direct requests to 127.0.0.1 rather than the hostname localhost. Only localhost requests are intercepted.

Concretely, this cell would fetch from the Colab backend VM:

%%javascript
// Fetches from the backend VM on port 1234.
fetch('http://localhost:1234/')

Whereas this cell would fetch from the local machine where your browser is running.

%%javascript
// Fetches from the machine running the browser.
fetch('http://127.0.0.1:1234/')
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 1
    Wow. I see. So, the security model for colab is quite different than for normal Jupyter Notebooks? I see that cell output exists in iFrames, segregated from other iFrames. It looks like the Javascript execution model is completely different (i.e., no more passing results viaIPython.notebook.kernel.execute()), and kernel services (including using localhost:xxx to access from Javascript). This model probably only accidentally knocked out localhost connections to workstation apps ... though it closed a hole you probably wanted closed. Any way to punch a hole so I can access local Cytoscape? – bdemchak Aug 19 '20 at 18:37
  • Yup, use `127.0.0.1` rather than `localhost`. I'll update the original answer to mention this shortly. – Bob Smith Aug 19 '20 at 19:13
  • Great news ... this seems to have done the trick. I'll use it from now on. – bdemchak Aug 20 '20 at 00:46