【发布时间】:2022-11-10 05:56:08
【问题描述】:
在笔记本中运行以下内容:
!pip install ipyleaflet
!jupyter nbextension enable --py --sys-prefix ipyleaflet
成功注册ipyleaflet分机:
Enabling notebook extension jupyter-leaflet/extension...
- Validating: ok
但是在同一个笔记本中渲染地图不起作用:
from ipyleaflet import Map, basemaps
Map(
center=(0, 0),
zoom=1,
scroll_wheel_zoom=True,
basemap=basemaps.OpenStreetMap.Mapnik,
)
错误:
[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)
浏览器说
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.
现有的解决方案是将 Jupyter notebook 升级到5.7.7,但我使用的是6.5.2 版本。
如果我用笔记本重新加载页面,ipyleaflet 地图会正确显示。
我怎样才能让它在第一遍中工作?
【问题讨论】:
-
它可能根本没有帮助,但从长远来看,您不应该再使用带有
pip install的感叹号。现在有一个神奇的%pip install命令来确保安装发生在支持笔记本的环境中。请参阅 here 关于最近几年添加的现代%pip install和%conda install魔术命令。 -
至于
!jupyter nbextension enable --py --sys-prefix ipyleaflet行。这可能无法正常工作。感叹号的作用是启动一个单独的 shell 实例并运行该命令,然后返回任何输出并将代码返回到笔记本,然后关闭临时 shell。所以可能你在一个然后消失的领域中启用了它。以jupyter开头的这种类型的命令意味着在终端中运行,您接下来将通过运行 jupyter notebook 来运行笔记本。所以重新开始,尝试启用扩展,然后启动笔记本进程...... -
<继续> 从您刚刚执行此操作的位置开始。它应该有助于它在第一次通过时工作。不能保证,但尝试在笔记本的单元格中运行它,然后看到您需要重新加载页面以使其工作,这有点像预期的那样。就像在 Python 中一样,您不想对正在迭代的列表执行操作。经典的 Jupyter notebook 界面(版本 6 和更早版本)是建立在比现代 JupyterLab 更古老的技术之上的。仅供参考,我称之为经典 Jupyter notebook 的以文档为中心的界面很快将建立在 JupyterLab 7 及更高版本的技术基础之上。
标签: python python-3.x jupyter-notebook ipyleaflet