What is the 2022 way to display Javascript in a Python notebook in Jupyter Lab?

Hello everyone!

I was looking for an update on how to display Javascript content in a notebook, opened with Jupyter Lab, so I though here was the best place to get one…

Is it correct that

  1. Since 2019, the @jupyterlab/javascript-extension is shipped with Jupyter Lab
  2. The extension is always active (i.e. no need to run npm i @jupyterlab/javascript-extension as stated on the npm repository) ?

If so maybe I’d recommend to confirm that no install is required on the extension’s README. Also maybe the current example,

Javascript('console.log("hello world");')

could be replaced with

Javascript('alert("hello world");')

which effect is simpler to spot, at least for non-JS experts?

Last but not least, what is the recommended way to load require in Jupyter Lab? I am aware of a code snippet that looks like this

from IPython.display import HTML, display
from time import sleep

display(HTML("""
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
"""))

sleep(0.1)

(thanks @fcollonval !) but now I am looking for a more robust approach (i.e. without sleep)

Thanks!

1 Like

2022 is here: Improved `%%javascript` cell magic. · Issue #13376 · ipython/ipython · GitHub

Edit: this is a forward looking statement made in 2021: no guarantee it will happen, nor that it will happen exactly as described in the linked, but looks exciting!

4 Likes

Thanks for your suggestions! I opened up Update several extensions readme files to delete old content. by jasongrout · Pull Request #11947 · jupyterlab/jupyterlab · GitHub to implement these suggestions.

As for the sleep - I think it is there just to give the frontend a bit of breathing room to load the script before processing anything after that may use the require library. If your display call is the only thing in the cell, I think you probably don’t need the sleep.

Thank you @krassowski and @jasongrout !

Also I should report on the latest research that I did for the itables package:

  • ES imports work well in all the notebooks environment I have tested (Jupyter Notebook, NB convert, Lab, Jupyter Book, VS Code, PyCharm, Google Colab), so I’d recommend to use ES imports when possible.
  • However for Javascript code that uses require we need to manually import require.js in some cases (with the code snippet above), as require.js is pre-imported only in Jupyter Notebook, NB convert and Jupyter Book.

Thanks! I was looking at the ES imports you implemented in itables when writing Transpile (optional) TypeScript, support imports (package and relative) and schema by krassowski · Pull Request #28 · jupyterlab/jupyterlab-plugin-playground · GitHub. At the time you were using ESM.js which seems brilliant, but given that it is a relatively small player, I felt that relying on it for major deployments may not be a future-proof decision (just yet); even worse, they do not offer a way to verify SRI integrity as discussed in Support other loader mechanisms? · Issue #1 · jupyterlab/jupyterlab-plugin-playground · GitHub.
Long term we would like to purge require.js altogether (as it is not maintained anymore), but it is difficult due to number of users relying on it, so we would need to shim it. SystemJS might be the solution.

Hi @krassowski , thanks for the feedback.

First I must say that I know very little about JS - All I know is that I was advised to use ES modules as they sound like the modern way to load JS libraries.

Is the issue of SRI integrity specific to https://esm.sh?

Also in the case of https://datatables.net/ I could not find an ES version supported by the author, and that is the primary reason for using https://esm.sh. But certainly when another version with native support for ES imports is made available (maybe on another CDN?) I’ll take that one.

1 Like

First I must say that I know very little about JS - All I know is that I was advised to use ES modules as they sound like the modern way to load JS libraries.

:+1:

Is the issue of SRI integrity specific to https://esm.sh?

Yes (and it might change one day).

But certainly when another version with native support for ES imports is made available (maybe on another CDN?) I’ll take that one

:+1: