Skip to main content

Generate sitemap.xml for Datasette sites

Project description

datasette-sitemap

PyPI Changelog Tests License

Generate sitemap.xml for Datasette sites

Installation

Install this plugin in the same environment as Datasette.

datasette install datasette-sitemap

Usage

Once configured, this plugin adds a sitemap at /sitemap.xml with a list of URLs.

This list is defined using a SQL query in metadata.json (or .yml) that looks like this:

{
  "plugins": {
    "datasette-sitemap": {
      "query": "select '/' || id as path from my_table"
    }
  }
}

Using metadata.yml allows for multi-line SQL queries which can be easier to maintain:

plugins:
  datasette-sitemap:
    query: |
      select
        '/' || id as path
      from
        my_table

The SQL query must return a column called path. The values in this column must begin with a /. They will be used to generate a sitemap that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc>https://example.com/1</loc></url>
  <url><loc>https://example.com/2</loc></url>
</urlset>

You can use UNION in your SQL query to combine results from multiple tables, or include literal paths that you want to include in the index:

select
  '/data/table1/' || id as path
  from table1
union
select
  '/data/table2/' || id as path
  from table2
union
select
  '/about' as path

If your Datasette instance has multiple databases you can configure the database to query using the database configuration property.

By default the domain name for the genearted URLs in the sitemap will be detected from the incoming request.

You can set base_url instead to override this. This should not include a trailing slash.

This example shows both of those settings, running the query against the content database and setting a custom base URL:

plugins:
  datasette-sitemap:
    query: |
      select '/plugins/' || name as path from plugins
      union
      select '/tools/' || name as path from tools
      union
      select '/news' as path
    database: content
    base_url: https://datasette.io

Try that query.

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-sitemap
python3 -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

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-sitemap-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

datasette_sitemap-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file datasette-sitemap-0.1.1.tar.gz.

File metadata

  • Download URL: datasette-sitemap-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for datasette-sitemap-0.1.1.tar.gz
Algorithm Hash digest
SHA256 78738449ec3ff98d5a117bd406816a1702d54f7fc842d359230603ac952f0a8a
MD5 5065ec6e0c16b8148cf61af9c16a8506
BLAKE2b-256 ca3b9dbb14707684e4121ce0def751c7e23c54fed3ae477c8f2924ff19c98895

See more details on using hashes here.

File details

Details for the file datasette_sitemap-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for datasette_sitemap-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ec0724a0c53d21a94908462e6bfc2beb0d5e6d584c7438c9cea8624de08342fd
MD5 b8fefcb01ce7297eb94d694e859f2130
BLAKE2b-256 539ef553dfde5249e7740b7897c5408f7f9bade0d372063d0fb4ee661599a468

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