24

Running on canopy version 1.5.5.3123 With;

Folium Version: 0.1.2, Build: 1

The following code;

import folium  
import pandas as pd
LDN_COORDINATES = (51.5074, 0.1278)  
from IPython.display import HTML
import shapefile
#create empty map zoomed in on London
LDN_COORDINATES = (51.5074, 0.1278) 
map = folium.Map(location=LDN_COORDINATES, zoom_start=12)
display(map)  

Returns

<folium.folium.Map at 0x10c01ae10>

But nothing else.

How do i get to display a map within an ipython notebook?

7 Answers 7

17

You can also save the map as html and then open it with webbrowser.

import folium
import webbrowser


class Map:
    def __init__(self, center, zoom_start):
        self.center = center
        self.zoom_start = zoom_start
    
    def showMap(self):
        #Create the map
        my_map = folium.Map(location = self.center, zoom_start = self.zoom_start)

        #Display the map
        my_map.save("map.html")
        webbrowser.open("map.html")


#Define coordinates of where we want to center our map
coords = [51.5074, 0.1278]
map = Map(center = coords, zoom_start = 13)
map.showMap()
8

_build_map() doesn't exist anymore. The following code worked for me

import folium
from IPython.display import display
LDN_COORDINATES = (51.5074, 0.1278)
myMap = folium.Map(location=LDN_COORDINATES, zoom_start=12)
display(myMap)
2
  • 2
    Running this in IPython on MacOS I get <folium.folium.Map at 0x10391f850> Commented Jun 29, 2022 at 21:06
  • One benefit of display() is that it lets you display maps within a for loop (which can be useful in cases where you created a number of maps and then saved them to a list). Example code: for map in map_list: display(map). (Note: I'm running this within a Jupyter notebook, so this may not work in other settings)
    – KBurchfiel
    Commented Jul 17 at 4:15
6

Considering the above answers, another simple way is to use it with Jupiter Notebook.

for example (on the Jupiter notebook):

import folium

london_location = [51.507351, -0.127758]

m = folium.Map(location=london_location, zoom_start=15)
m

and see the result when calling the 'm'.

2
  • 8
    The question is precisely not in Jupiter Notebook
    – Joachim
    Commented Feb 24, 2020 at 10:52
  • @Joachim yes, I mentioned: With considering the above answers, another simple way is to use Jupiter notebook.
    – nima
    Commented Feb 24, 2020 at 11:28
2

I've found this tutorial on Folium in iPython Notebooks quite helpful. The raw Folium instance that you've created isn't enough to get iPython to display the map- you need to do a bit more work to get some HTML that iPython can render.

To display in the iPython notebook, you need to generate the html with the myMap._build_map() method, and then wrap it in an iFrame with styling for iPython.

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

Where by returning embed as the output of the iPython cell, iPython will automatically call display.display() on the returned iFrame. In this context, you should only need to call display() if you're rendering something else afterwards or using this in a loop or a function.

Also, note that using map as a variable name may might be confused with the .map() method of several classes.

1
  • 15
    myMap._build_map() _build_map() doesn't exit anymore. They took it out of the folium. How should we solve the problem if we don't have _build_map()? Commented Dec 9, 2017 at 17:50
2

Is there a reason you are using an outdated version of Folium?

This ipython notebook clarifies some of the differences between 1.2 and 2, and it explains how to put folium maps in iframes. http://nbviewer.jupyter.org/github/bibmartin/folium/blob/issue288/examples/Popups.ipynb

And the code would look something like this (found in the notebook above, it adds a marker, but one could just take it out):

m = folium.Map([43,-100], zoom_start=4)

html="""
    <h1> This is a big popup</h1><br>
    With a few lines of code...
    <p>
    <code>
        from numpy import *<br>
        exp(-2*pi)
    </code>
    </p>
    """
iframe = folium.element.IFrame(html=html, width=500, height=300)
popup = folium.Popup(iframe, max_width=2650)

folium.Marker([30,-100], popup=popup).add_to(m)

m

The docs are up and running, too, http://folium.readthedocs.io/en/latest/

2

There is no need to use iframes in 2022. To display the map, simply use the

{{ map | safe }} tag in html and _repr_html_() method in you view. It is also not necessary to save the map to the template

sample.py

@app.route('/')
def index():
    start_coords = (46.9540700, 142.7360300)
    folium_map = folium.Map(location=start_coords, zoom_start=14)
    return folium_map._repr_html_()

template.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ folium_map | safe }}
</body>
</html>
2
-2

i have same error and nothing work for me finally i found it print(dir(folium.Map)) see method save dose not exist instead use

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.