The simple sitemap generator ============================= Dynamic sitemap is a library for sitemaps and sitemap indexes generation based on `the protocol `_. The library provides API to generate sitemaps (sitemap indexes) manually or autogenerate it for some frameworks (only FlaskSitemap is available yet). Contents -------- .. toctree:: api changelog Installation ------------ To use dynamic sitemap lib, first install it using pip: .. code-block:: console (.venv) $ pip install dynamic-sitemap Getting started --------------- Try this small snippet: .. code-block:: python from dynamic_sitemap import ChangeFreq, SimpleSitemap # items should be strings or dicts items = ( '/about', {'loc': '/contacts', 'changefreq': ChangeFreq.NEVER.value}, ) sitemap = SimpleSitemap('https://site.com', items) print(sitemap.render()) In the output you will see something like this: .. code-block:: xml https://site.com/ https://site.com/about https://site.com/contacts 2021-11-07T19:54:04.848443 0.9 You may want to write a sitemap to a file: .. code-block:: python from datetime import datetime from dynamic_sitemap import ChangeFreq, SimpleSitemap sitemap = SimpleSitemap('https://site.com') sitemap.add_items( {'loc': '/about', 'priority': 0.9, 'lastmod': datetime.now().isoformat()}, ) sitemap.write('static/sitemap.xml') Then check out ``static/sitemap.xml`` file. Usage ------ Sitemap for Flask ````````````````` 'Hello world' example: .. code-block:: python from dynamic_sitemap import FlaskSitemap from flask import Flask app = Flask(__name__) sitemap = FlaskSitemap(app, 'https://site.com') sitemap.build() Then visit ``_. The basic example using some SQLAlchemy models: .. code-block:: python from dynamic_sitemap import ChangeFreq, FlaskSitemap from flask import Flask from models import Post, Tag app = Flask(__name__) sitemap = FlaskSitemap(app, 'https://site.com', orm='sqlalchemy') sitemap.config.ALTER_PRIORITY = 0.1 sitemap.ignore('/edit', '/upload') sitemap.add_items('/faq', {'loc': '/about', 'priority': 0.7}) sitemap.add_rule('/blog', Post, loc_from='slug', priority=1.0) sitemap.add_rule('/blog/tag', Tag, loc_from='id', changefreq=ChangeFreq.DAILY.value) sitemap.build() **Not supported** yet: * urls with more than 1 converter, such as ``/page//``. Also you can set configurations from your class: .. code-block:: python class Config: FILENAME = 'static/sitemap.xml' IGNORED = {'/admin', '/back-office', '/other-pages'} CONTENT_PRIORITY = 0.7 sitemap = FlaskSitemap(app, 'https://myshop.org', config=Config, orm='sqlalchemy') sitemap.add_rule('/goods', Product, loc_from='id', lastmod_from='updated') sitemap.build() Sitemap without ORM ``````````````````` If you do not use ORM, you may add rules via :ref:`add_raw_rule() ` using :ref:`helpers.Model ` .. code-block:: python from flask import Flask from db import connect app = Flask(__name__) db = connect(DB_ADDRESS) def extract_posts(): query = 'SELECT slug, updated FROM posts;' with db.execute(query) as cursor: return iter(cursor.fetchall()) post = Model(extract_posts) sitemap = FlaskSitemap(app, 'https://site.com') sitemap.add_raw_rule('/posts/', post) sitemap.build() Changelog --------- Check out :doc:`here `. Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search`