API¶
Sitemaps¶
SimpleSitemap¶
- class SimpleSitemap(base_url: str = '', items: Sequence[Union[dict, str]] = ())¶
The simple sitemap generator.
- item_cls¶
alias of
dynamic_sitemap.items.SitemapItem
- renderer_cls¶
alias of
dynamic_sitemap.renderers.SitemapXMLRenderer
- add_items(*items: Union[dict, str])¶
Add static items to a sitemap.
- render() str ¶
Get a string sitemap representation.
- write(filename: str = 'sitemap.xml')¶
Write a sitemap to a file.
FlaskSitemap¶
- class FlaskSitemap(app: object, base_url: str = '', items: Sequence[Union[dict, str]] = (), config: Optional[Union[type, SitemapConfig]] = None, orm: str = None)¶
The sitemap generator for a Flask application.
- Parameters
app – an instance of Flask application
base_url – a base URL such as ‘http://site.com’
items – list of strings or dicts to generate static sitemap items
config – a class with configurations
orm – an ORM name used in project
- Raises
SitemapValidationError - if ORM extension is not found.
- item_cls¶
alias of
dynamic_sitemap.items.SitemapItem
- renderer_cls¶
alias of
dynamic_sitemap.renderers.SitemapXMLRenderer
- add_items(*items: Union[dict, str])¶
Add static items to a sitemap.
- add_raw_rule(path: str, model: dynamic_sitemap.helpers.Model, changefreq: Optional[str] = None, priority: Optional[float] = None)¶
Add a rule for non-ORM project.
- Parameters
path – a part of URI is used to get a page generated through a model
model – helpers.Model with some extractor
changefreq – how often this URL changes (daily, weekly, etc.)
priority – a priority of URL to be set
- add_rule(path: str, model: dynamic_sitemap.helpers.ORMModel, loc_from: str, lastmod_from: Optional[str] = None, changefreq: Optional[str] = None, priority: Optional[float] = None)¶
Add a rule to generate urls by a template using a specified model.
- Parameters
path – a part of URI is used to get a page generated through a model
model – a model of an app that has a slug, e.g. an instance of SQLAlchemy.Model
loc_from – an attribute of this model which is used to generate URL
lastmod_from – an attribute of this model which is an instance of the datetime object
changefreq – how often this URL changes (daily, weekly, etc.)
priority – a priority of URL to be set
- build()¶
Prepare a sitemap to be rendered or written to a file.
- Example:
>>> from dynamic_sitemap import FlaskSitemap >>> from flask import Flask >>> >>> app = Flask(__name__) >>> sitemap = FlaskSitemap(app, 'http://site.com') >>> sitemap.add_items('/about', '/contacts') >>> sitemap.build()
- get_rules() List[str] ¶
Return a list of URL rules.
- ignore(*patterns)¶
Add URLs which would be igrnored.
- render() str ¶
Get a string sitemap representation.
- view()¶
Generate a response such as Flask views do.
- write(filename: str = 'sitemap.xml')¶
Write a sitemap to a file.
Sitemap indexes¶
SimpleSitemapIndex¶
- class SimpleSitemapIndex(base_url: str = '', items: Sequence[Union[dict, str]] = ())¶
The simple sitemap index generator.
- item_cls¶
alias of
dynamic_sitemap.items.SitemapIndexItem
- renderer_cls¶
alias of
dynamic_sitemap.renderers.SitemapIndexXMLRenderer
- add_items(*items: Union[dict, str])¶
Add static items to a sitemap.
- render() str ¶
Get a string sitemap representation.
- write(filename: str = 'sitemap.xml')¶
Write a sitemap to a file.
Configuration¶
- class SitemapConfig¶
The class to set configurations.
- from_object(obj: Optional[Union[type, dynamic_sitemap.config.SitemapConfig]])¶
Updates values from the given object.
- Parameters
obj – a class with the same attributes as this one or it’s instance
- ALTER_CHANGES = None¶
str, a change frequency of other pages with attributes not defined by add_items
- ALTER_PRIORITY = None¶
int or float, a priority of other pages
- BASE_URL: str = ''¶
str, a base URL such as ‘http://site.com’
- CACHE_PERIOD: Union[int, float] = 0¶
int or float, hours; if set, will use already generated data
- CONTENT_CHANGES = None¶
str, a change frequency of pages generated by models
- CONTENT_PRIORITY = None¶
int or float, a priority of pages generated by models
- FILENAME: str = ''¶
str, a path to write sitemap
- IGNORED: set = {'/admin', '/sitemap.xml', '/static'}¶
set, a set of strings which ignored URLs start with
- INDEX_CHANGES = None¶
str, a change frequency of the index page
- INDEX_PRIORITY = 1.0¶
int or float, a priority of the index page
- TIMEZONE = None¶
str, str, the site’s local time zone, one of pytz.all_timezones
Helpers¶
- class Model(extractor: Callable[[...], Iterable[Tuple[str, datetime.datetime]]])¶
The class to use instead of ORM models. Used with
add_raw_rule
.- Parameters
extractor – a function that fetches loc & lastmod from a database.