Skip to main content

Sitemap generator for Flask applications.

Project description

Flask Sitemapper

Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications. This allows you to create a nice and fully functional sitemap for your project with very minimal code, as demonstrated below. It is compatible with Flask blueprints.

Requirements

  • Python3
  • Flask
  • Jinja2

Installation

pip install flask-sitemapper

Usage

Initialising Flask Sitemapper

The sitemapper must be initialised with the app instance as shown below.

Flask Sitemapper requires SERVER_NAME to be specified in the Flask configuration.

By default, HTTPS will be used for all URLs in the sitemap. To change this, specify https=False when initialising the sitemapper.

import flask
from flask_sitemapper import Sitemapper

app = flask.Flask("test_app")

# For development, use "127.0.0.1:5000"
app.config["SERVER_NAME"] = "example.com"

sitemapper = Sitemapper(app)

If you are using Flask blueprints, you can either list all URLs in a single sitemap by importing the sitemapper instance to your other files or using the add_endpoint method (see below), or by creating a sitemap for each blueprint and using a sitemap index (see below).

Adding URLs to the sitemap

Decorators are added to route functions to include their URLs in the sitemap. These must be included above the Flask decorators.

# Define the homepage route and include it in the sitemap
@sitemapper.include()
@app.route("/")
def r_home():
    return flask.render_template("index.html")

You can pass arguments to the decorator to include additional information in the sitemap. Whatever arguments you provide will be included in the URL entry as-is.

@sitemapper.include(
    lastmod = "2022-02-08",
    changefreq = "monthly",
    priority = 1.0,
)
@app.route("/about")
def r_about():
    return flask.render_template("about.html")

This example would appear in the sitemap as:

<url>
  <loc>https://example.com/about</loc>
  <lastmod>2022-02-08</lastmod>
  <changefreq>monthly</changefreq>
  <priority>1.0</priority>
</url>

For routes where multiple URL paths are defined, the sitemap will only include the last path.

@sitemapper.include()
@app.route("/shop")  # This won't be included
@app.route("/buy")  # This won't be included
@app.route("/store")  # This will be included
def r_store():
    return "<h1>Store Page</h1>"

You can also add Flask endpoints to the sitemap without using their route function. This may be useful when dealing with large or complex projects. Keyword arguments can still be given after the endpoint name.

sitemapper.add_endpoint("r_contact", lastmod="2022-02-09")

Generating and serving the sitemap

To serve your sitemap, you must define a route function that returns sitemapper.generate(). Your sitemap will then be avaliable at the URL(s) you specify.

This route should be defined after all routes that are included in the sitemap.

@app.route("/sitemap.xml")
def r_sitemap():
    return sitemapper.generate()

Master Sitemaps

Master sitemaps, or sitemap indexes, are sitemaps that list other sitemaps. These are used if a single sitemap would be too large, or sometimes for organisational purposes. You can create a master sitemapper by specifying master=True when initialising your sitemapper.

Note that sitemap indexes have a different syntax to regular sitemaps, so it is important to provide this argument.

master_sitemapper = Sitemapper(app, master=True)

You can then decorate your sitemap route functions to add them to the sitemap index.

@master_sitemapper.include()
@app.route("/some_sitemap.xml")
def r_some_sitemap():
    return some_sitemapper.generate()

Or add them with add_endpoint

@master_sitemapper.add_endpoint("r_some_sitemap")

Then create the route for the sitemap index.

@app.route("/sitemap.xml")
def r_sitemap_index():
    return master_sitemapper.generate()

For this example, the sitemap index would look like this:

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://example.com/some_sitemap.xml</loc>
  </sitemap>
</sitemapindex>

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

flask-sitemapper-1.2.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_sitemapper-1.2.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file flask-sitemapper-1.2.2.tar.gz.

File metadata

  • Download URL: flask-sitemapper-1.2.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/22.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for flask-sitemapper-1.2.2.tar.gz
Algorithm Hash digest
SHA256 9b0bc85941016b26a8fbd34e86e478472655ece507da961387b9d9bf84fc546b
MD5 3c1ad09952d9b480000436573cc05a09
BLAKE2b-256 84b8136ed0e721ce2f56d9c19db03c98bcca55e0423bf6cd311177746ec82d50

See more details on using hashes here.

File details

Details for the file flask_sitemapper-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: flask_sitemapper-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/22.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

File hashes

Hashes for flask_sitemapper-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e8a761558768a4d97c0f42654738507a59018842ef4b943ef420c2880c62c5d1
MD5 d793b5e64dfb41f3fcba292183bd8d44
BLAKE2b-256 1ae3f827c0346fe077f3338fb2cb883e57bd13ad4db3f6ab05e26f9b70964d3b

See more details on using hashes here.

Supported by

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