Creating marker on map using Folium results in blank HTML page

后端 未结 2 1358
南方客
南方客 2021-01-27 18:29

I tried to create a Map using folium library in python3. It work\'s fine until i add a Marker to the map. On adding Marker the output results in just a blank HTML page.

相关标签:
2条回答
  • 2021-01-27 19:01

    in my experience, folium is very sensitive to single quotes (').

    The reason being, folium generates javascript, and transforms your strings into javascript strings, which it encloses with single quotes. It does, however, not escape any single quotes contained in the string (not even sure if that's possible in js), so having a single quote in the string has the same effect as not closing a string in python.

    Solution: Replace all single quotes in your strings with backticks (`) or (better) use @Bob Haffner's answer.


    Edit: out of sheer coincidence i stumbled over another solution just now. Apparently folium.Popup has an argument parse_html. Use it like this:

    folium.Marker([x,y], popup=folium.Popup("Foo'Bar",parse_html=True)).add_to(map)
    

    however, i could not make it work without running into a unicodeescape error. nonetheless it exists, supposedly for this purpose, and might work for you.


    Edit 2: As i've been told on github, this issue will be fixed in the next release. Ref: folium#962

    0 讨论(0)
  • 2021-01-27 19:02

    @MCO is absolutely correct.

    I like to leverage html.escape() to handle the problem characters like so

    import folium
    import html
    
    map = folium.Map(location=[20.59,78.96], tiles = "Mapbox Bright")
    
    folium.Marker([26.80,80.76],popup=html.escape("Hey, It's Moradabad")).add_to(map)
    map.save("mapOutput.html")
    
    0 讨论(0)
提交回复
热议问题