ipyleaflet
ipyleaflet is a Jupyter widget for Leaflet.js, enabling the creation of interactive maps directly within Jupyter Notebook and JupyterLab environments. It currently stands at version 0.20.0 and maintains an active development pace with several releases throughout the year, adding new features and addressing bugs.
Common errors
-
Error displaying widget: model not found
cause The ipyleaflet Jupyter extension is not properly installed or enabled, preventing the frontend from rendering the widget. This is a common issue.fixEnsure `ipyleaflet` is installed correctly. For Jupyter Notebook, try `jupyter nbextension enable --py --sys-prefix ipyleaflet`. For JupyterLab, try `jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-leaflet` (for older versions of Lab). -
Map attributes (e.g., zoom, center) do not update in JupyterLab.
cause This issue was prevalent in ipyleaflet versions prior to 0.18.0 when used with JupyterLab 4.x due to compatibility problems.fixUpgrade ipyleaflet to version 0.18.0 or newer: `pip install --upgrade ipyleaflet`. -
Map does not display anything, no error message, just empty output.
cause This can happen if the widget state isn't correctly saved or the kernel is no longer available. Often points to an underlying installation or environment issue, or running in an unsupported environment like Google Colab.fixRestart the Jupyter kernel. Verify the extension is enabled (see 'Error displaying widget' fix). Confirm you are not running in Google Colab. If using an older JupyterLab, ensure the labextension is installed.
Warnings
- breaking Older versions of ipyleaflet (pre-0.18.0) experienced issues with JupyterLab 4.x, where map attributes like zoom or center would not update. This was addressed in version 0.18.0.
- deprecated The `add_control()` and `add_layer()` methods on the Map object were deprecated in version 0.17.0. Users should now use the more general `add()` method.
- gotcha For older Jupyter Notebook versions (prior to 5.3) or JupyterLab <=2, manual installation of the Jupyter extension might be required. Failure to do so can result in widgets not displaying.
- gotcha The default zoom level for a newly created `Map()` object changed from 12 to 4 in version 0.20.0, potentially affecting existing code or expectations for initial map views.
- gotcha ipyleaflet does not currently work in Google Colab due to limitations with ipywidgets integration in that environment, and maps will not display.
Install
-
pip install ipyleaflet -
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-leaflet
Imports
- Map
from ipyleaflet import Map
- Marker
from ipyleaflet import Marker
- TileLayer
from ipyleaflet import TileLayer
- basemaps
from ipyleaflet import basemaps
- DrawControl
from ipyleaflet import DrawControl
- FullScreenControl
from ipyleaflet import FullScreenControl
- jupyter_leaflet
import jupyter_leaflet
from ipyleaflet import Map
Quickstart
from ipyleaflet import Map, Marker # Create a map centered at a specific location m = Map(center=(52.204793, 0.121558), zoom=10) # Add a marker marker = Marker(location=(52.204793, 0.121558), draggable=True) m.add_layer(marker) # Display the map (in Jupyter environment) m