0

I tried to render a map with folium in a python script. When I put the rendering in a for statement, no map renders. Below are two scripts. One works, one does not. The only difference is the placement of the m1 object to render the map. Can anyone indicate what the issue is? Is it sometype of scoping issue?

Thanks.

Al

#works

import folium

lat1=[42.3601]
lon1=[-71.0589]
for i in range(1):
    m1 = folium.Map(location=[lat1[0],lon1[0]],zoom_start=14)
    for j in range(len(lat1)):
        folium.Marker(location=[lat1[j],lon1[j]],
            icon= folium.Icon(color='green')
            ).add_to(m1)         
m1

#does not work moved the m1 statement in the first for statement

import folium

lat1=[42.3601]
lon1=[-71.0589]
for i in range(1):
    m1 = folium.Map(location=[lat1[0],lon1[0]],zoom_start=14)
    for j in range(len(lat1)):
        folium.Marker(location=[lat1[j],lon1[j]],
            icon= folium.Icon(color='green')
            ).add_to(m1)         
    m1
Searching
  • 11
  • 1

1 Answers1

1

It's how jupyter notebooks work

(I'm assuming you intend for this to run in a notebook because otherwise you need to save to html to view the map)

for example, the following prints nothing

x = 5
for i in range(5):
    x

but this prints 5:

x = 5
x

(Note: this is a notebook behaviour and python does different things for printing elsewhere)

Friskod
  • 51
  • 3
  • [This answer](https://stackoverflow.com/a/71302319/8508004) expands on this some more with some good references. @Searching the steps in the loop aren't the final thing encountered by your first example block. – Wayne Mar 04 '22 at 18:13