Folium: Folium map not rendering when Marker 'popup' specified?

Created on 18 Sep 2017  路  6Comments  路  Source: python-visualization/folium

_Apologies, in advance, if this not the most appropriate place for this question (as this might be a Jupyter Notebook issue)!_


Issue

I cannot get Folium to render a map when the 'popup' parameter for Marker objects are specified via list indexing (where the list contains strings). What's confusing is that it works fine if the Marker 'popup' is a static string!

Example: Working code

This code works (i.e., a viewable map is rendered) in a Jupyter notebook cell:

# Imports dependency used to drop markers onto a map
import folium

# Isolating "northing" and "easting" information with labels for each point
locationlist = df_nearme[["Latitude","Longitude"]].values.tolist()
labels = df_nearme["Cafe Name"].values.tolist()

# Create map and drop points onto it
m = folium.Map(location=[lat_home, long_home], zoom_start=14)
for point in range(len(locationlist)):
    folium.Marker(locationlist[point]).add_to(m)

m

So does this code:

# Imports dependency used to drop markers onto a map
import folium

# Isolating "northing" and "easting" information with labels for each point
locationlist = df_nearme[["Latitude","Longitude"]].values.tolist()
labels = df_nearme["Cafe Name"].values.tolist()

# Create map and drop points onto it
m = folium.Map(location=[lat_home, long_home], zoom_start=14)
for point in range(len(locationlist)):
    folium.Marker(locationlist[point], popup='abc123').add_to(m)

m

Example: Not working code

BUT this code does _not_ render a viewable map (no error is thrown, but I just get a blank, white box):

# Imports dependency used to drop markers onto a map
import folium

# Isolating "northing" and "easting" information with labels for each point
locationlist = df_nearme[["Latitude","Longitude"]].values.tolist()
labels = df_nearme["Cafe Name"].values.tolist()

# Create map and drop points onto it
m = folium.Map(location=[lat_home, long_home], zoom_start=14)
for point in range(len(locationlist)):
    folium.Marker(locationlist[point], popup=labels[point]).add_to(m)

m

As I mentioned before, I suspect this has something to do with Jupyter Notebook, but I'm not entirely sure and figured I'd ask to see if anyone else has run into this issue.

Thanks!

Most helpful comment

The problem is with the popup text. Latest version of folium takes raw HTML for simplicity, a feature that many wanted for a while, so when your text has ' or &, for example, you need to use parse_html=True, see http://nbviewer.jupyter.org/gist/ocefpaf/d1d612d290f766935d8abe1559523a72

All 6 comments

@Per48edjes can you create a sscce with the data so I can try to reproduce?

Hmmmm, not sure how to do that given I'm writing df_near_me from a database query. Here's a sample of the data (in tabular form) that you can load into a Pandas DataFrame with the same name (and then run the code above).

# Import Pandas
import pandas as pd

# Data in JSON string format
data = '{"id":{"0":411083201,"1":418513660,"2":528057543,"3":586713622,"4":647656728,"5":647656785,"6":1493456455,"7":1493456487,"8":2005894602,"9":2095344770,"10":2187987262,"11":2411692096,"12":2411698457,"13":2474058013,"14":2474058022,"15":2881830804,"16":2895323815,"17":2895323816,"18":2983919102,"19":3321734312,"20":3641568148,"21":4010355532,"22":4030622426,"23":4037746568,"24":4055719117,"25":4259001279,"26":4340535594,"27":4625994189,"28":4666687025},"Latitude":{"0":37.7673162,"1":37.7645003,"2":37.7682118,"3":37.7648492,"4":37.771672,"5":37.7721693,"6":37.7763591,"7":37.777046,"8":37.7690716,"9":37.766319,"10":37.7664253,"11":37.7770377,"12":37.7763106,"13":37.7739857,"14":37.774748,"15":37.775488,"16":37.7752166,"17":37.7759142,"18":37.7744588,"19":37.7760172,"20":37.7762395,"21":37.765011,"22":37.769195,"23":37.7750452,"24":37.7726713,"25":37.7717782,"26":37.7745253,"27":37.7768943,"28":37.7752822},"Longitude":{"0":-122.4219479,"1":-122.4216812,"2":-122.4223857,"3":-122.4320119,"4":-122.4331366,"5":-122.4307254,"6":-122.4180877,"7":-122.4172737,"8":-122.4277243,"9":-122.417422,"10":-122.4290387,"11":-122.4175698,"12":-122.4232558,"13":-122.424226,"14":-122.4226877,"15":-122.4159,"16":-122.4195185,"17":-122.4191912,"18":-122.4205881,"19":-122.4314951,"20":-122.4168763,"21":-122.4226685,"22":-122.4315398,"23":-122.4210561,"24":-122.4220741,"25":-122.4167145,"26":-122.4306476,"27":-122.4245402,"28":-122.4161381},"Cafe Name":{"0":"Four Barrel Coffee","1":"Muddy Waters","2":"Carlin\'s Cafe","3":"Peet\'s Coffee & Tea","4":"Nectar","5":"Cafe International","6":"Ma\'velous","7":"Starbucks","8":"Starbucks","9":"Flying Pig Bistro","10":"Church Street Cafe","11":"Anderson Bakery","12":"Blue Bottle Coffee","13":"mercury cafe","14":"gourmet and more","15":"Cumaica","16":"All Star Cafe","17":"Boston Cafe","18":"Javalencia Cafe","19":"Alamo Square Cafe","20":"Blue Bottle Coffee","21":"Stanza","22":"Duboce Park Cafe","23":"Eden Cafe","24":"Delessio Market & Bakery","25":"Gaslamp Cafe","26":"The Center SF Tea House","27":"Cafe la Vie","28":"Peet\'s Coffee"},"Street":{"0":"Valencia Street","1":"Valencia Street","2":"Valencia Street","3":"Market Street","4":"Haight Street","5":"Haight Street","6":"Market Street","7":"Market Street","8":"Market Street","9":"South Van Ness Avenue","10":null,"11":null,"12":null,"13":"Octavia Street","14":null,"15":"Mission Street","16":"Market Street","17":"Van Ness Avenue","18":"Market Street","19":null,"20":"Market Street","21":null,"22":"Sanchez Street","23":"Franklin Street","24":"Market Street","25":"Howard Street","26":"Fillmore Street","27":"Octavia Street","28":null}}'

# Create dataframe from JSON data
df_nearme = pd.read_json(data)

# Imports dependency used to drop markers onto a map
import folium

# Isolating "northing" and "easting" information with labels for each point
locationlist = df_nearme[["Latitude","Longitude"]].values.tolist()
labels = df_nearme["Cafe Name"].values.tolist()

# Create map and drop points onto it
m = folium.Map(location=[lat_home, long_home], zoom_start=14)
for point in range(len(locationlist)):
    folium.Marker(locationlist[point], popup=labels[point]).add_to(m)

m

(Sorry, I'm pretty green. Thanks for being patient.)

The problem is with the popup text. Latest version of folium takes raw HTML for simplicity, a feature that many wanted for a while, so when your text has ' or &, for example, you need to use parse_html=True, see http://nbviewer.jupyter.org/gist/ocefpaf/d1d612d290f766935d8abe1559523a72

Shouldn't this at least be documented in folium.map.Marker?

Shouldn't this at least be documented in folium.map.Marker?

Sure. Do you fancy sending a PR to fix that?

Sure! #771

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pjandrews picture pjandrews  路  4Comments

ghandic picture ghandic  路  5Comments

aviogit picture aviogit  路  4Comments

sherl0cks picture sherl0cks  路  5Comments

jgoad picture jgoad  路  4Comments