Folium: Interactive Leaflet Maps in Python

Last Updated: 08 Nov 2025


Folium builds interactive maps using Leaflet.js — perfect for location data, GPS tracks, store locator, delivery routes, or real-time dashboards.

Hinglish Tip: Folium = "Google Maps banao Python mein" — marker, popup, zoom, sab kuch browser mein!


Install Once

pip install folium

Basic Map (Delhi Center)

import folium

# Create map centered on Delhi
m = folium.Map(
    location=[28.6139, 77.2090],  # [lat, lon]
    zoom_start=12,
    tiles="OpenStreetMap"        # Free & beautiful
)

m.save("delhi_map.html")  # Open in browser
m                         # In Jupyter → shows inline

Key Parameters in folium.Map()

ParameterValuesPurpose
location[lat, lon]Map center
zoom_start1–18Initial zoom
tiles'OpenStreetMap', 'Stamen Toner', 'CartoDB positron'Map style
width, height'100%', 500Size

Full Example: India Cities with Popups & Icons

import folium
from folium import IFrame
import base64

# Sample city data
cities = [
    {"name": "Delhi", "lat": 28.6139, "lon": 77.2090, "pop": "32M", "img": "delhi.jpg"},
    {"name": "Mumbai", "lat": 19.0760, "lon": 72.8777, "pop": "20M", "img": "mumbai.jpg"},
    {"name": "Bangalore", "lat": 12.9716, "lon": 77.5946, "pop": "13M", "img": None},
]

m = folium.Map(location=[20.5937, 78.9629], zoom_start=5, tiles="CartoDB positron")

for city in cities:
    # HTML popup with image
    html = f"""
    <h3>{city['name']}</h3>
    <p><b>Population:</b> {city['pop']}</p>
    """
    if city['img']:
        encoded = base64.b64encode(open(city['img'], 'rb').read()).decode()
        html += f'<img src="data:image/jpeg;base64,{encoded}" width="200">'

    iframe = IFrame(html, width=250, height=200)
    popup = folium.Popup(iframe, max_width=300)

    # Custom icon
    icon = folium.Icon(color="red", icon="info-sign")

    folium.Marker(
        [city['lat'], city['lon']],
        popup=popup,
        tooltip=city['name'],
        icon=icon
    ).add_to(m)

m.save("india_cities.html")
m

Hinglish Tip: tooltiphover pe name dikhega popupclick pe photo + info — jaise Google Maps!


Add Circle, Polygon, Heatmap

# 1. Circle (Delivery radius)
folium.Circle(
    location=[28.6139, 77.2090],
    radius=5000,  # meters
    popup="5 KM Delivery",
    color="green",
    fill=True,
    fill_color="green"
).add_to(m)

# 2. Polygon (Area boundary)
coords = [[28.6,77.2], [28.7,77.2], [28.7,77.3], [28.6,77.3]]
folium.Polygon(
    locations=coords,
    popup="Restricted Zone",
    color="red",
    fill=True,
    fill_opacity=0.4
).add_to(m)

# 3. Heatmap (Crime hotspots)
from folium.plugins import HeatMap
heat_data = [[28.6,77.2], [28.61,77.21], [28.62,77.19]] * 20
HeatMap(heat_data).add_to(m)

MiniMap + Layer Control

from folium.plugins import MiniMap, MarkerCluster

# MiniMap (bottom-right)
MiniMap().add_to(m)

# Layer Control
folium.LayerControl().add_to(m)

# Marker Cluster (1000+ points)
cluster = MarkerCluster().add_to(m)
for i in range(100):
    folium.Marker([28.6 + i*0.001, 77.2 + i*0.001]).add_to(cluster)

Common Mistakes to Avoid

MistakeFix
Map not showing in JupyterJust write m at the end
Image not in popupUse base64 encoding
Too many markers → slowUse MarkerCluster
Wrong coordinatesAlways [lat, lon], not [lon, lat]
Map too smallSet width='100%', height=500


|| राम नाम सत्य है ||