Executing Java script code in JupyterLab gives error

I am trying to print a Javascript code to return the notebook URL.

I have the following written in a Jupyter Notebook cell:

%%javascript
var kernel = IPython.notebook.kernel
var host = window.location.href
kernel.execute("notebook_url='"+host+"'")

and when I query for notebook_url it gives output as 'http://localhost:8889/notebooks/LTIBootCamp.ipynb'

enter image description here

But when I try the same from Jupyter Lab it gives the following error

from IPython.display import display, Javascript

display(Javascript(
    'var kernel = IPython.notebook.kernel'
'var host = window.location.href'
'kernel.execute("notebook_url='"+host+"'")'
))

output error:

Javascript Error: unexpected token: identifier

Any suggestions how can can I do the same in JupyterLab?

Right: the IPython- and Jupyter-APIs are not available in clients other than notebook <6, e.g. JupyterLab, VSCode, Colab. It is also uncertain whether these will be introduced as shims in notebook 7.

JupyterLab, in specific, offers an --expose-app-in-browser option, but that API (jupyterapp) is different, and limited in its own ways.

ipylab provides some options for bridging some of these gaps, but is then more tightly bound to that client.

1 Like