2

Running the following inside the notebook:

!pip install ipyleaflet
!jupyter nbextension enable --py --sys-prefix ipyleaflet

Successfully registers ipyleaflet extension:

Enabling notebook extension jupyter-leaflet/extension...
      - Validating: ok

However rendering the map within the same notebook does not work:

from ipyleaflet import Map, basemaps
Map(
    center=(0, 0),
    zoom=1,
    scroll_wheel_zoom=True,
    basemap=basemaps.OpenStreetMap.Mapnik,
)

Error:

[Open Browser Console for more detailed log - Double click to close this message]
Failed to load model class 'LeafletMapModel' from module 'jupyter-leaflet'
Error: Script error for "jupyter-leaflet"
http://requirejs.org/docs/errors.html#scripterror
    at makeError (http://localhost:8888/static/components/requirejs/require.js?v=d37b48bb2137faa0ab98157e240c084dd5b1b5e74911723aa1d1f04c928c2a03dedf922d049e4815f7e5a369faa2e6b6a1000aae958b7953b5cc60411154f593:168:17)
    at HTMLScriptElement.onScriptError (http://localhost:8888/static/components/requirejs/require.js?v=d37b48bb2137faa0ab98157e240c084dd5b1b5e74911723aa1d1f04c928c2a03dedf922d049e4815f7e5a369faa2e6b6a1000aae958b7953b5cc60411154f593:1735:36)

Browser says

Refused to execute script from 'http://localhost:8888/static/jupyter-leaflet.js?v=20221102111053' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

The existing solutions to this are upgrading Jupyter notebook to 5.7.7, but I am using version 6.5.2.

If I reload the page with the notebook, the ipyleaflet map shows correctly.

How can I make it work in the first pass?

KarloSpacapan
  • 173
  • 2
  • 16
  • 1
    It may not help at all but in the long run, you shouldn't be using the exclamation point with `pip install` anymore. There's a magic `%pip install` command now to insure installation occurs in the environment backing the notebook. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) about the modern `%pip install` and `%conda install` magic commands added in the last few years. – Wayne Nov 02 '22 at 20:38
  • As for the `!jupyter nbextension enable --py --sys-prefix ipyleaflet` line. That may not work right. What the exclamation point does is start a separate shell instance and run that command and then return any output and return code to the notebook and then the temporary shell is shutdown. So possibly you enabled it off in a realm that then disappeared. That type of command starting with `jupyter` is meant to be run in the terminal where you will next run the notebook by running jupyter notebook. So start fresh, try to enable the extension, & then start the notebook process ... – Wayne Nov 02 '22 at 20:43
  • from where you just did that. It should help it work on the first pass. No guarantee but trying to run that inside a cell in the notebook and then seeing you need to reload the page to get it to work is sort of as expected. Just like in Python you don't want to perform operations on a list you are iterating on. The classic Jupyter notebook interface (version 6 and earlier) is built on much older tech then the modern JupyterLab. FYI, soon the document-centric interface that I'm calling the classic Jupyter notebook will be built on the tech underlying JupyterLab with version 7 & on. – Wayne Nov 02 '22 at 20:50

1 Answers1

2

I received the same error while running Jupyter Notebook but restarting the kernel, and closing and reopening that file worked for me.

KRITI GARG
  • 21
  • 3