Django and Folium integration

django-leaflet
folium marker types
folium interactive map
folium minimap
folium featuregroup
folium github
django maps
folium display map

Django newbie here: my aim is to integrate Folium to an html page. so what I have at the moment:

polls/views.py

def show_map(request):  
    #creation of map comes here + business logic
    m = folium.Map([51.5, -0.25], zoom_start=10)
    test = folium.Html('<b>Hello world</b>', script=True)
    popup = folium.Popup(test, max_width=2650)
    folium.RegularPolygonMarker(location=[51.5, -0.25], popup=popup).add_to(m)

    context = {'my_map': m}

    return render(request, 'polls/show_folium_map.html', context)

polls/urls.py

urlpatterns = [   
       path('show_my_map', views.show_map, name='show_map'),

]

and show_folium_map.html

 <h1>map result comes here</h1>
 {{ my_map }}

problem is that I get the 'to_string' value of the map (I promise you I saw that coming). So how can I integrate the map in such way that I can actually see the map and also define the size?

In order to really include folium into custom django template you have to render your figure first before adding it to context (This will recursivly load all parts of the map into the figure). Afterwards in your template, you have to access header, html and script parts of figure seperatly by calling their render function. Additionally, those parts have to marked as "safe" by django template tag in order to allow html insertion. See example below.

Example:

views.py:

import folium

from django.views.generic import TemplateView


class FoliumView(TemplateView):
    template_name = "folium_app/map.html"

    def get_context_data(self, **kwargs):
        figure = folium.Figure()
        m = folium.Map(
            location=[45.372, -121.6972],
            zoom_start=12,
            tiles='Stamen Terrain'
        )
        m.add_to(figure)

        folium.Marker(
            location=[45.3288, -121.6625],
            popup='Mt. Hood Meadows',
            icon=folium.Icon(icon='cloud')
        ).add_to(m)

        folium.Marker(
            location=[45.3311, -121.7113],
            popup='Timberline Lodge',
            icon=folium.Icon(color='green')
        ).add_to(m)

        folium.Marker(
            location=[45.3300, -121.6823],
            popup='Some Other Location',
            icon=folium.Icon(color='red', icon='info-sign')
        ).add_to(m)
        figure.render()
        return {"map": figure}

templates/folium_app/map.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {{map.header.render|safe}}
</head>
<body>
  <div><h1>Here comes my folium map:</h1></div>
  {{map.html.render|safe}}
  <script>
    {{map.script.render|safe}}
  </script>
</body>
</html>

Folium django, 2020-04-03 python html css I'm trying to create a Folium map for internal marketing purposes for my company. 그래서 내가 Django and Folium integration. Folium is a powerful data visualization library in Python that was built primarily to help people visualize geospatial data. With Folium, one can create a map of any location in the world if its latitude and longitude values are known.

You can get the html as a string by triggering the rendering on the (internal) parent of Map:

m = folium.Map()
html: str = m.get_root().render()

Note that this returns a full html page, so you may need to put it in an iframe.

Alternatively, you can render the head, body and script parts separately. That way you can put each part on your page where it belongs and you don't need an iframe:

m = folium.Map()
html_head: str = m.get_root().header.render()
html_body: str = m.get_root().html.render()
html_script: str = m.get_root().script.render()

What python web-mapping framework to use?, Try Folium, it's really simple to get started: you create your project using GeoDjango with additions like Django-Leaflet. Papyrus, the Mapfish integration in Pyramid, a well-known Python framework (last updated 2017). Maps are defined as a folium.Map object, and we can add other folium objects on top of the folium.Map to improve/add to the map rendered Folium allows the users to select different map projections. We will be using the Spherical Mercator projection in this article since we are visualizing a relatively small area size, and it is a more commonly

You can try the below way. I also had faced the same issue and it worked great for me.

views.py
def show_map(request):  
    #creation of map comes here + business logic
    m = folium.Map([51.5, -0.25], zoom_start=10)
    test = folium.Html('<b>Hello world</b>', script=True)
    popup = folium.Popup(test, max_width=2650)
    folium.RegularPolygonMarker(location=[51.5, -0.25], popup=popup).add_to(m)
    m=m._repr_html_() #updated
    context = {'my_map': m}

    return render(request, 'polls/show_folium_map.html', context)
show_folium_map.html
{{ my_map|safe }}

Web Mapping Tutorial with Python and Folium, This Folium tutorial shows how to create a Leaflet web map from scratch with Python and the Folium library. For more explanations on how the code works,  Django and Folium integration In order to really include folium into custom django template you have to render your figure first before adding it to context (This will recursivly load all parts of the map into the figure).

Easy Webmapping with django-leaflet & django-geojson, Django-geojson is a set of tools to manipulate GeoJSON with Django. Django-​leaflet allows you to use Leaflet in your Django projects. An other solution would be to integrate Leaflet GeoSearch plugin to the django-leaflet  folium works great for creating ad hoc maps with python data. For your application I would suggest not to use folium but to use Leaflet directly. You need a fixed map layout and very good integration with your web application, not to create various maps with differing Python data, which is folium's strong point.

python-visualization/folium, Code review · Project management · Integrations · Actions I am using Folium maps to analyze how census data relates to specific schools. Maybe I am asking a quite preliminary question, but how can I show this on a website, such as Django? "m" won't show folium outside of ipython notebooks #946. For more recent versions of Django we’re using Django’s built in QuerySet.as_manager(). Now supports Pandas 0.14.1 and above The fall in Coverage in this release largely reflects the integration of the PassThroughManager into the code base.

Python, I`m want to make webapp with Django which displays a map for every You need a fixed map layout and very good integration with your web  Django assumes that all databases use UTF-8 encoding. Using other encodings may result in unexpected behavior such as “value too long” errors from your database for data that is valid in Django. See the database specific notes below for information on how to set up your database correctly.

Comments
  • check this out!
  • @Lemayzeur thanks! you will note from my code that I actually took it from the post you mentioned. Problem is that it does m.save('osm.html') and while I can save it to an html and reference it from another file - I'm not sure if this doesn't present a problem with multiple users. Any ideas if this consider 'safe'. Thanks again.