notebook icon indicating copy to clipboard operation
notebook copied to clipboard

importmap in _html_repr_ fails in local notebooks (does work in collab)

Open Strilanc opened this issue 3 years ago • 3 comments
trafficstars

REPRO STEPS

From the command line, run jupyter notebook, create a new notebook, add a code cell, and put the following into the code cell:

class C:
    def _repr_html_(self):
        return """
<html>
<head>
  <script type="importmap">
    {
      "imports": {
        "three": "https://unpkg.com/[email protected]/build/three.module.js"
      }
    }
  </script>
</head>
<body>
  <div id="out">loading...</div>
  <script type="module">
    import {Vector2} from "three";
    document.getElementById("out").textContent = "worked!";
  </script>
</body>
</html>
        """

C()

Make sure the notebook is trusted, and execute the cell.

WHAT SHOULD HAPPEN

The output should say "worked!".

More specifically, the output should be an html widget containing a div with its inner text set to "worked!".

Note that, in google collab, this is in fact the result you get.

WHAT ACTUALLY HAPPENS

The output gets stuck at "loading...". Additionally, if you open the browser's javascript console, you will find an error:

jquery.min.js:2 Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".
    at b (jquery.min.js:2:1)
    at Pe (jquery.min.js:2:1)
    at S.fn.init.append (jquery.min.js:2:1)
    at OutputArea._safe_append (outputarea.js:458:1)
    at OutputArea.append_execute_result (outputarea.js:497:1)
    at OutputArea.append_output (outputarea.js:325:1)
    at OutputArea.handle_output (outputarea.js:256:1)
    at output (codecell.js:399:1)
    at Kernel._handle_output_message (kernel.js:1199:1)
    at i (jquery.min.js:2:1)

LIKELY CAUSE

It appears that, whatever mechanism is being used to translate the HTML returned by _repr_html_ into an output cell widget, it is not correctly translating import maps in a way that results in the javascript within the output cell having access to the declared import maps.

Strilanc avatar Oct 21 '22 19:10 Strilanc