Flask-Sitemap¶
Flask-Sitemap is a Flask extension helping with sitemap generation.
Contents¶
Installation¶
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
Usage¶
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/.
Simple Example¶
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.
Index Page¶
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.
Caching¶
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)
Configuration¶
The details of the application settings that can be customized.
SITEMAP_URL_METHOD¶
Default: http
.
SITEMAP_GZIP¶
Default: False
.
SITEMAP_BLUEPRINT_URL_PREFIX¶
Default: /
.
SITEMAP_ENDPOINT_URL¶
Return sitemap index or sitemap for pages with less than
SITEMAP_MAX_URL_COUNT
urls.
Default: sitemap.xml
.
SITEMAP_ENDPOINT_PAGE_URL¶
Return GZipped sitemap for given page range of urls.
Note
It is strongly recommended to provide caching decorator.
Default: sitemap<int:page>.xml
SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS¶
Default: False
.
SITEMAP_IGNORE_ENDPOINTS¶
Default: None
.
SITEMAP_VIEW_DECORAROS¶
Default: []
.
SITEMAP_MAX_URL_COUNT¶
The maximum number of urls per one sitemap file can be up to 50000, however there is 10MB limitation for the file.
Default: 10000
.
API¶
This documentation section is automatically generated from Flask-Sitemap’s source code.
Flask-Sitemap¶
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)
-
class
flask_sitemap.
Sitemap
(app=None)¶ Flask extension implementation.
-
gzip_response
(data)¶ GZip response data and create new Response instance.
-
init_app
(app)¶ Initialize a Flask application.
-
page
(page)¶ Generate sitemap for given range of urls.
-
register_generator
(generator)¶ Register an URL generator.
The function should return an iterable of URL paths or
(endpoint, values)
tuples to be used asurl_for(endpoint, **values)
.Returns: the original generator function
-
render_page
(urlset=None)¶ Render GZipped sitemap template with given url set.
-
sitemap
()¶ Generate sitemap.xml.
-
xml_response
(data)¶ Standard XML response.
-
Changelog¶
Here you can see the full list of changes between each Flask-Sitemap release.
Version 0.2.0 (released 2016-01-05)¶
New features¶
- Adds new command line interface to generate sitemap. (#13)
Improved features¶
- Adds ‘application/octet-stream’ content type to GZipped response. (#20)
Bug fixes¶
- Uses pytest-runner as distutils command with dependency resolution.
Notes¶
- Improves integration of PyTest and addresses problem with missing test requirements. (#15)
- If you want to use command line interface install dependencies using pip install flask-sitemap[cli].
Version 0.1.0 (released 2015-02-03)¶
- Initial public release. (#12)
- Support for configurable gzip response.
- Quickstart example for signals and caching. (#8)
- Support for sitemap pages. (#3)
- Adds an option
SITEMAP_VIEW_DECORATORS
for specifying list of view decorators. (#4) - Adds support for ignoring certain endpoints through
SITEMAP_IGNORE_ENDPOINTS
configuration option. (#2) - Adds new option to automatically include all endpoints without
parameters. In order to enable this feature set
SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS
toTrue
. (#2)
Contributing¶
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:
- Search for already reported problems.
- Check if the issue has been fixed or is still reproducible on the latest master branch.
- Create an issue with a test case.
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%
License¶
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:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
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.
Authors¶
Flask-Sitemap is developed for use in Invenio digital library software.
Contact us at info@invenio-software.org
Contributors¶
- Jiri Kuncar <jiri.kuncar@cern.ch>