2

Attempting to display a basic map in iPython using folium leaflet library. Recent install of iPython through Anaconda with Folium installed with Pip. Confirmed everything is up to date

ran this code in iPython

import folium
map = folium.Map(location=[48, -102], zoom_start=3)
map.create_map('map.html')
map

I receive a blank frame. I checked the console on the html. I receive a number of Failed to load resource: net::ERR_FILE_NOT_FOUND tracing back to an Uncaught ReferenceError: L is not defined. I checked the html document and found the leaflet reference looks like this:

    src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js">

I assume the issue is with the relative link but I have no found information in the folium docs to resolve this issue.

Thanks ya'll for the help. I look forward to paying it forward.

4

1 回答 1

0

我发现这个iPython Notebooks 中关于 Folium 的教程很有帮助。另外,我对另一个似乎相关的Stackoverflow 问题做了详细的回答。

要在 iPython 笔记本中显示,您需要使用 myMap._build_map() 方法生成 html,将其包装在 iframe 中,并将 iFrame 返回给 iPython 进行显示。

这是您的情况的示例:

import folium  
from IPython.display import HTML
myMap = folium.Map(location=[48, -102], zoom_start=3)
myMap._build_map() 
mapWidth, mapHeight = (400,500) # width and height of the iFrame in pixels
srcdoc = myMap.HTML.replace('"', '"')
embed = HTML('<iframe srcdoc="{}" '
             'style="width: {}px; height: {}px; display:block; width: 50%; margin: 0 auto; '
             'border: none"></iframe>'.format(srcdoc, width, height))
embed

请注意,该.create_map()方法会将完整的地图 HTML 保存到文件中,而您希望将 HTML 代码方便地保存在 iPython 中进行演示 - 这就是我们使用 ._build_map() 的原因。以开头的行embed是神奇的地方——我们将 folium 生成的 HTML 内容包装在一个 iframe 中,该 iframe 可以根据需要设置样式,然后作为单元格的输出返回。IPython 在内部调用.display()返回到单元格的结果,因此您应该有一个很好的居中地图。

于 2016-08-05T21:32:22.770 回答