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 fully functional sitemap for your project with minimal code, as demonstrated below. It is compatible with Flask blueprints.
For more information about sitemaps and the XML schema, visit sitemaps.org.
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 creating the Sitemapper
instance.
Method 1 example (recommended)
import flask
from flask_sitemapper import Sitemapper
# create sitemapper instance
sitemapper = Sitemapper()
# create app
app = flask.Flask("test_app")
# For development, use "127.0.0.1:5000"
app.config["SERVER_NAME"] = "example.com"
# initialize with app
sitemapper.init_app(app)
Method 2 example
import flask
from flask_sitemapper import Sitemapper
# create app
app = flask.Flask("test_app")
# For development, use "127.0.0.1:5000"
app.config["SERVER_NAME"] = "example.com"
# create instance and initialize with app
sitemapper = Sitemapper(app)
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>"
Using with Flask blueprints
Create your Sitemapper
instance(s) in a seperate file or otherwise avoiding circular imports.
Import and use the instance(s) in your blueprints.
Import the instance(s) when creating your flask app and initialize with sitemapper.init_app(app)
after registering your blueprints.
You can also add Flask endpoints to the sitemap by using their endpoint name as shown below. 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-sitemapper-1.3.0.tar.gz
.
File metadata
- Download URL: flask-sitemapper-1.3.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3791ccb74e768cd6c16c66d65ed40cbec1670b17eed0e28827b52cf234485f1c |
|
MD5 | fff7ef071fc67b5594786519ce1aed53 |
|
BLAKE2b-256 | 0751b2b56c2881f369ab1f1c078b3fee7d7fbbca9526bfd2666b2ae91b8953d2 |
File details
Details for the file flask_sitemapper-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: flask_sitemapper-1.3.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e59ba74fae5ee0cc2a89b182daaac1e0a5c74ec13963da0571ba7106f51ab150 |
|
MD5 | 39d707b6dda1d7e8ef1d84da2729dbd5 |
|
BLAKE2b-256 | 37bbfc3278c047a49aebc1da3ac04b8bb55c0c1e4c521f026bbb182236c6c0b7 |