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.
Additionally, the PREFFERED_URL_SCHEME configuration variable will be used to determine whether the sitemap uses HTTP or HTTPS for URLs. If this is not set, HTTP will be used.
import flask
from flask_sitemapper import Sitemapper
app = flask.Flask("test_app")
app.config["SERVER_NAME"] = "127.0.0.1:5000"
app.config["PREFFERED_URL_SCHEME"] = "http"
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 create multiple sitemaps for each blueprint by defining a sitemapper instance for each.
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>http://127.0.0.1:5000/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>"
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()
The sitemap generated using these examples would look like this:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"...>
<url>
<loc>http://127.0.0.1:5000/</loc>
</url>
<url>
<loc>http://127.0.0.1:5000/about</loc>
<lastmod>2022-02-08</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://127.0.0.1:5000/store</loc>
</url>
</urlset>
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask-sitemapper-1.0.0.tar.gz.
File metadata
- Download URL: flask-sitemapper-1.0.0.tar.gz
- Upload date:
- Size: 4.0 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c9d3f57672d5448b45246abc5c39b27e2c61cd92a718f5d61eeb36a9592509d
|
|
| MD5 |
ef7a30c947760292b5695939d2db97d8
|
|
| BLAKE2b-256 |
469e11cacc31921cb2920f0b1fa32f5618b8b44950dd4ce53824cae9158a8f6e
|
File details
Details for the file flask_sitemapper-1.0.0-py3-none-any.whl.
File metadata
- Download URL: flask_sitemapper-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6932ef1ab0cc66ad0bb3755684b2c520c6ab31266640882802fdad19397863f0
|
|
| MD5 |
cfd759c21b8983bf22120f6710408733
|
|
| BLAKE2b-256 |
b275b798dc22c8966dea62cc09734bc344feec2ab2524c39b0b9bc9a270e71c5
|