Skip to main content

A plugin that bundles Leaflet.js for Datasette

Project description

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 packaed 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>

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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datasette-leaflet-0.2.1.tar.gz (105.4 kB view details)

Uploaded Source

Built Distribution

datasette_leaflet-0.2.1-py3-none-any.whl (104.9 kB view details)

Uploaded Python 3

File details

Details for the file datasette-leaflet-0.2.1.tar.gz.

File metadata

  • Download URL: datasette-leaflet-0.2.1.tar.gz
  • Upload date:
  • Size: 105.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for datasette-leaflet-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c77c51c070f68b88cf9718eb3fb621b2916f7870b735db4ced2c433f3ec21600
MD5 9e568bde941c5fb0881207609dbfe3b8
BLAKE2b-256 31162d641bbc141c0bfdba2ec34af4b0190845025f0355f34c175d8f7136b624

See more details on using hashes here.

File details

Details for the file datasette_leaflet-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: datasette_leaflet-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 104.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for datasette_leaflet-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4c204e145ffb5c96be78eb891c5692ce6f18772bf3e9da4ebd5eec5f0790cbb
MD5 805196fb36d130cfbad888fead50c465
BLAKE2b-256 9827d3468a3c4c1ce4189544a97f51fcd7d62078ce291676255b63cf9d38b142

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page