Skip to main content

datasette-leaflet

PyPI Changelog Tests License

Datasette plugin adding the Leaflet JavaScript library.

A growing number of Datasette plugins depend on the Leaflet JavaScript mapping library. They each have their own way of loading Leaflet, which could result in loading it multiple times (with multiple versions) if more than one plugin is installed.

This library is intended to solve this problem, by providing a single plugin they can all depend on that loads Leaflet in a reusable way.

Plugins that use this:

Installation

You can install this plugin like so:

datasette install datasette-leaflet

Usually this plugin will be a dependency of other plugins, so it should be installed automatically when you install them.

Usage

The plugin makes leaflet.js and leaflet.css available as static files. It provides two custom template variables with the URLs of those two files.

  • {{ datasette_leaflet_url }} is the URL to the JavaScript
  • {{ datasette_leaflet_css_url }} is the URL to the CSS

These URLs are also made available as global JavaScript constants:

  • datasette.leaflet.JAVASCRIPT_URL
  • datasette.leaflet.CSS_URL

The JavaScript is packaged as a JavaScript module. You can dynamically import the JavaScript from a custom template like this:

<script type="module">
import('{{ datasette_leaflet_url }}')
  .then((leaflet) => {
    /* Use leaflet here */
  });
</script>

Importing the module also sets a global window.L object with the same contents. Leaflet plugins that expect that global, such as Leaflet.markercluster, can be loaded once the import has completed. Anything a plugin adds to L is available on that global, not on the imported leaflet module.

The bundled copy of Leaflet differs from the official release in one other way: tile layers send a Referer header by default. See Tile layers and the Referer header.

You can load the CSS like this:

<link rel="stylesheet" href="{{ datasette_leaflet_css_url }}">

Or dynamically like this:

<script>
let link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '{{ datasette_leaflet_css_url }}';
document.head.appendChild(link);
</script>

Here's a full example that loads the JavaScript and CSS and renders a map:

<script type="module">
window.DATASETTE_CLUSTER_MAP_TILE_LAYER = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
window.DATASETTE_CLUSTER_MAP_TILE_LAYER_OPTIONS = {"maxZoom": 19, "detectRetina": true, "attribution": "&copy; <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors"};
let link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '{{ datasette_leaflet_css_url }}';
document.head.appendChild(link);
import('{{ datasette_leaflet_url }}')
  .then((leaflet) => {
    let div = document.createElement('div');
    div.style.height = '400px';
    document.querySelector('.content').appendChild(div);
    let tiles = leaflet.tileLayer(
        window.DATASETTE_CLUSTER_MAP_TILE_LAYER,
        window.DATASETTE_CLUSTER_MAP_TILE_LAYER_OPTIONS
    );
    let map = leaflet.map(div, {
        center: leaflet.latLng(0, 0),
        zoom: 2,
        layers: [tiles]
    });
  });
</script>

Tile layers and the Referer header

Datasette serves many of its pages, including table pages, with a Referrer-Policy: no-referrer header, so by default a browser will not send a Referer header when it loads map tiles for those pages. Some tile servers block requests like that. The OpenStreetMap tile server at tile.openstreetmap.org is one of them: its usage policy requires a valid Referer, and it returns "Access blocked" tiles without one.

To avoid this, the copy of Leaflet bundled with this plugin changes the default for the referrerPolicy tile layer option from false to "strict-origin-when-cross-origin". Tile requests then include a Referer header containing just the origin of your Datasette instance, such as https://example.com/. The path and query string of the page showing the map are never sent.

This applies to every L.tileLayer() and L.tileLayer.wms() created using the bundled Leaflet, including those created by other plugins.

Changing the policy for a Datasette instance

Use the tile_referrer_policy plugin setting. Set it to false to go back to sending no Referer header for tiles:

plugins:
  datasette-leaflet:
    tile_referrer_policy: false

Or set it to any other referrer policy, such as origin:

plugins:
  datasette-leaflet:
    tile_referrer_policy: origin

This goes in datasette.yaml for Datasette 1.0, or in metadata.yaml for earlier versions.

The value in use is available to JavaScript as datasette.leaflet.TILE_REFERRER_POLICY. Leaflet reads it once, when the JavaScript is first loaded.

Changing the policy for a single tile layer

Pass the referrerPolicy option when you create the layer. This takes precedence over the plugin setting:

let tiles = leaflet.tileLayer("https://tiles.example.com/{z}/{x}/{y}.png", {
    referrerPolicy: false
});

To change the default for every layer created after that point, use leaflet.TileLayer.mergeOptions({referrerPolicy: false}).

Metadata

Release files for datasette-leaflet 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datasette-leaflet 0.3
File Size Uploaded Provenance Actions
datasette_leaflet-0.3.tar.gz 177.1 kB With provenance Details for datasette_leaflet-0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for datasette-leaflet 0.3
File Interpreter ABI Platform Actions
datasette_leaflet-0.3-py3-none-any.whl Python 3 none any Details for datasette_leaflet-0.3-py3-none-any.whl

Total release size: 350.2 kB

Release files / datasette_leaflet-0.3.tar.gz

Download URL datasette_leaflet-0.3.tar.gz
Size 177.1 kB
Tags Source
SHA-256 checksum
How to use checksums
81fc3112e2c49f77c8b2c1a2a57abc64a6b7127594e8eddcb23f0f4e1b1e5175
BLAKE2b-256 checksum
How to use checksums
7328338cefc12c9e4d1c95f52073b26998824a78c4682cbfd25b9fb4991d92a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / datasette_leaflet-0.3-py3-none-any.whl

Download URL datasette_leaflet-0.3-py3-none-any.whl
Size 173.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f09304ccdf59d4d936cfdaa56fd6d3d9f45d3a5691120e9ad9e30067ee590281
BLAKE2b-256 checksum
How to use checksums
f87f22b396ec152755bb8ebbc0db200481ee50438640cd1563359a2910a78285
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for datasette-leaflet 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for datasette_leaflet-0.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page