Flask extension for generating XML sitemaps
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.
Installation
Flask Sitemapper requires Python 3.7 or newer. The Python packages flask
and jinja2
are also required, and will be installed automatically.
The latest version can be installed from PyPi with pip as shown below.
pip install flask-sitemapper
Usage
Initialising Flask Sitemapper
The sitemapper must be initialised with the app instance as shown below.
By default, HTTPS will be used for all URLs in the sitemap. To change this, specify https=False
when creating the Sitemapper
instance.
Recommended Method
import flask
from flask_sitemapper import Sitemapper
# create sitemapper instance
sitemapper = Sitemapper()
# create app
app = flask.Flask("test_app")
# initialize with app
sitemapper.init_app(app)
Alternative Method
import flask
from flask_sitemapper import Sitemapper
# create app
app = flask.Flask("test_app")
# 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()
Sitemap Indexes
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, which generates a sitemap index, by specifying master=True
when creating the instance.
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>
Example App
import flask
from flask_sitemapper import Sitemapper
sitemapper = Sitemapper()
app = flask.Flask("test_app")
sitemapper.init_app(app)
@sitemapper.include()
@app.route("/")
def r_home():
return flask.render_template("index.html")
@sitemapper.include(
lastmod = "2022-02-08",
changefreq = "monthly",
priority = 1.0,
)
@app.route("/about")
def r_about():
return flask.render_template("about.html")
@app.route("/sitemap.xml")
def r_sitemap():
return sitemapper.generate()
app.run()
Development & Contributing
Thank you for contributing to the project! All issues and pull requests are highly appreciated.
Development Requirements
The following Python libraries will be installed automatically to a virtual environment by poetry during setup.
- flask
- jinja2
- black
- isort
- pytest
- requests
Project Setup
Install poetry
if not already installed.
pip install poetry
Clone the repository and enter the repository's directory.
git clone https://github.com/h-janes/flask-sitemapper
cd flask-sitemapper
Create the poetry virtual environment and install dependencies.
poetry install
You may want to configure your editor to use the virtual environment for linting and debugging. You can find the path of the virtual environment with the command:
poetry env info --path
Code Style
Flask Sitemapper uses black
for code formatting and isort
for import ordering.
It is recommended that you configure your editor to run black
and isort
on save, making sure that they access the configurations defined in pyproject.toml
Otherwise, you should ensure that your code conforms to these style standards before submitting a pull request.
Testing
Tests are stored in the tests
directory. You should ensure that all tests are passing before creating a pull request. If you create a pull request with new features, creating new tests for your additions is highly appreciated. You can run all tests with the command:
poetry run pytest
Building
The project can be built with the command:
poetry build
This should generate a dist
directory containing a .tar.gz
file and a .whl
file.
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.4.1.tar.gz
.
File metadata
- Download URL: flask_sitemapper-1.4.1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3e0012e9603905c874c2bb05b2425a82a9123bd5ef523e637f7480d1182f1d7 |
|
MD5 | c277bc0fb6e904df3a0b01dc89380cce |
|
BLAKE2b-256 | 3b5e2600d599cb9cbe234ef6343c48f90ae09c67bd737a5e934700f2b8d0e0cc |
File details
Details for the file flask_sitemapper-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: flask_sitemapper-1.4.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3faa3d9d518f932a3e948b015d9905bccc455878e20c7995c9046e08b9ff7190 |
|
MD5 | fa87c50990790c70077eb5294ae3426a |
|
BLAKE2b-256 | 08a74f974477c3cf0b2fa37e971bc450e56b495aa5aad6d56b85a1460e1435d7 |