On Google Colab, localhost access from Javascript fails

Viewed 419

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!

1 Answers

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/')
Related