1

I want to put HTML formatting into a folium map popup. How do I format the html popup inside my for loop?

My code:

import folium
import pandas
from folium import IFrame
data=pandas.read_csv("addresses.txt")
lat=list(data["LAT"])
lon=list(data["LON"])
name=list(data["NAME"])
desc=list(data["DESCRIPTION"])
map=folium.Map(location=[40.748817,73.985429],zoom_start=14,tiles="stamenterrain")
#folium.TileLayer('cartodbpositron').add_to(map)
for lt,ln,nm,ds in zip(lat,lon,name,desc):
 test = folium.Html('<b>nm<br>desc</b>', script=True)
 fg.add_child(folium.CircleMarker(location=[lt,ln],radius=6,color='grey',fill_color='yellow',
popup=test))
map.add_child(fg)
map.save("Map1.html")
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Jun 9, 2017 at 16:49
1
  • Unrelated to the question but consider looping over data directly and extracting each value from the resulting row/series. Embrace pandas instead of tearing the DataFrame into lists. Commented Jan 19, 2020 at 20:40

1 Answer 1

2

folium should handle this correctly by default.

As per the example docs:

import folium
import branca 
m = folium.Map([43, -100], zoom_start=4)
html = """
 <h1> This popup is an Iframe</h1><br>
 With a few lines of code...
 <p>
 <code>
 from numpy import *<br>
 exp(-2*pi)
 </code>
 </p>
 """
iframe = branca.element.IFrame(html=html, width=500, height=300)
popup = folium.Popup(iframe, max_width=500)
folium.Marker([30, -100], popup=popup).add_to(m)

So in your example

for lt,ln,nm,ds in zip(lat,lon,name,desc):
 test = folium.Html('<b>nm<br>desc</b>', script=True) # i'm assuming this bit runs fine
 iframe = branca.element.IFrame(html=test, width=350, height=150)
 popup = folium.Popup(iframe, parse_html=True)
 folium.Marker(location=[lt,ln],radius=6,color='grey',fill_color='yellow', popup=popup)).add_to(map)
answered Apr 10, 2019 at 7:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.