Flask-Sitemap is a Flask extension helping with sitemap generation.
Flask-Sitemap is on PyPI so all you need is :
$ pip install flask-sitemap
The development version can be downloaded from its page at GitHub.
$ git clone https://github.com/inveniosoftware/flask-sitemap.git
$ cd flask-sitemap
$ python setup.py develop
$ ./run-tests.sh
This part of the documentation will show you how to get started in using Flask-Sitemap with Flask.
This guide assumes you have successfully installed Flask-Sitemap and a working understanding of Flask. If not, follow the installation steps and read about Flask at http://flask.pocoo.org/docs/.
First, let’s create the application and initialise the extension:
from flask import Flask, session, redirect
from flask_sitemap import Sitemap
app = Flask("myapp")
ext = Sitemap(app=app)
@app.route('/')
def index():
pass
@ext.register_generator
def index():
# Not needed if you set SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS=True
yield 'index', {}
if __name__ == '__main__':
app.run(debug=True)
If you save the above as app.py, you can run the example application using your Python interpreter:
$ python app.py
* Running on http://127.0.0.1:5000/
and you can observe generated sitemap on the example pages:
$ firefox http://127.0.0.1:5000/
$ firefox http://127.0.0.1:5000/sitemap.xml
You should now be able to emulate this example in your own Flask applications. For more information, please read the Index Page guide, the Caching guide, and peruse the API.
By default, a sitemap contains set of urls up to SITEMAP_MAX_URL_COUNT. When the limit is reached a sitemap index file with list of sitemaps is served instead. In order to ease Caching of sitemaps a signal sitemap_page_needed is fired with current application object, page number and url generator.
Large sites should implement caching or their sitemaps. The following example shows an basic in-memory cache that can be replaced by Flask-Cache.
from functools import wraps
from flask_sitemap import Sitemap, sitemap_page_needed
cache = {} # replace by *Flask-Cache* instance or similar
@sitemap_page_needed.connect
def create_page(app, page, urlset):
cache[page] = sitemap.render_page(urlset=urlset)
def load_page(fn):
@wraps(fn)
def loader(*args, **kwargs):
page = kwargs.get('page')
data = cache.get(page)
return data if data else fn(*args, **kwargs)
return loader
self.app.config['SITEMAP_MAX_URL_COUNT'] = 10
self.app.config['SITEMAP_VIEW_DECORATORS'] = [load_page]
sitemap = Sitemap(app=self.app)
The details of the application settings that can be customized.
Default: http.
Default: False.
Default: /.
Return sitemap index or sitemap for pages with less than SITEMAP_MAX_URL_COUNT urls.
Default: sitemap.xml.
Return GZipped sitemap for given page range of urls.
Note
It is strongly recommended to provide caching decorator.
Default: sitemap<int:page>.xml
Default: False.
Default: None.
Default: [].
The maximum number of urls per one sitemap file can be up to 50000, however there is 10MB limitation for the file.
Default: 10000.
This documentation section is automatically generated from Flask-Sitemap’s source code.
Flask-Sitemap generates an application sitemap.xml.
Initialization of the extension:
>>> from flask import Flask
>>> from flask_sitemap import Sitemap
>>> app = Flask('myapp')
>>> ext = Sitemap(app=app)
or alternatively using the factory pattern:
>>> app = Flask('myapp')
>>> ext = Sitemap()
>>> ext.init_app(app)
Flask extension implementation.
GZip response data and create new Response instance.
Initialize a Flask application.
Generate sitemap for given range of urls.
Register an URL generator.
The function should return an iterable of URL paths or (endpoint, values) tuples to be used as url_for(endpoint, **values).
Returns: | the original generator function |
---|
Render GZipped sitemap template with given url set.
Generate sitemap.xml.
Standard XML response.
Here you can see the full list of changes between each Flask-Sitemap release.
Version 0.1 (released 2015-02-03)
Bug reports, feature requests, and other contributions are welcome. If you find a demonstrable problem that is caused by the code of this library, please:
If you create a feature branch, you can run the tests to ensure everything is operating correctly:
$ ./run-tests.sh
...
Ran 8 tests in 0.246s
OK
Name Stmts Miss Cover Missing
--------------------------------------------------
flask_sitemap/__init__ 47 0 100%
flask_sitemap/config 4 0 100%
flask_sitemap/version 2 0 100%
--------------------------------------------------
TOTAL 53 0 100%
Flask-Sitemap is free software; you can redistribute it and/or modify it under the terms of the Revised BSD License quoted below.
Copyright (C) 2013, 2014 CERN.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction.
Flask-Sitemap is developed for use in Invenio digital library software.
Contact us at info@invenio-software.org