What a freezing day is like if you build a sitemap and update time by time manually. Or paying for a recurring monthly subscription for just a second work one-off is not a smart decision as well.
If you are looking for a better way like the feeling of eating better marbling beef meat, this piece is here for you. This article is going to talk about developing dynamic sitemap xml for your Flask App. Let’s go!
Table of Contents: Flask App Dynamic Sitemap XML Tutorial
- Python Packages:
urlparse, render_template, make_response - Parse the Top Level Domain
- Static URL List
- Dynamic URL List
- Sitemap XML template
- Customize the response header
- Full Python Script of Dynamic Sitemap XML
- FAQ
Python Packages: urlparse, render_template, make_response
As usual, we need to skeletonize the fundamental functions. Here is the list of packages needed in developing tasted and incredible dynamic sitemap xml
from urllib.parse import urlparseFrom flask import render_templte, make_response
In a moment, I would go through more details about what each package is for in this dish.
Parse the top domain of the App
First thing first, spotting the top level domain address is indispensable before ruling mapping the URLs. For better tasting the favor, here we need the urlparse. Below is a sample as follow:
host_components = urlparse(request.host_url) host_base = host_components.scheme + "://" + host_components.netloc
Static URL List
Before generating each URL, we need to separate static and dynamic urls because some of the routes are set by variables.
We need to create a static URL list and rule what type of static URLs would be added to the list. Here are the code sample as follows:
static_urls = list() for rule in app.url_map.iter_rules(): if not str(rule).startswith("/inactive"): if "GET" in rule.methods and len(rule.arguments) == 0: url = { "loc": f"{host_base}{str(rule)}" } static_urls.append(url)
Cooking this dish needs the Flask in-built methods which are url_map, startswith
Dynamic URL List
For fetching the variable URL path, we could set up a set of data in Mongo in advance and call them in the function. Here is the code sample:
dynamic_urls = list() dynamicURL variable = db.sample.find({})
Sitemap XML template
To capture the dynamic datafeed from the server side, we need to use Jinja2 in the front-end html template. Here is the code sample:
{% for url in static_urls %}<url> <loc>{{ url["loc"] }}</loc>
</url>{% endfor %}{% for url in dynamic_urls %}<url> <loc>{{ url["loc"] }}</loc></url>{% endfor %}
Customize the response header
Last but not least, we need to customise the sitemap xml header that is different from other routes. For customising the header, we can use make_response
response = make_response(xml_sitemap) response.headers["Content-Type"] = "application/xml"
Full Python Script of Dynamic Sitemap XML
- If you are interested in getting the full version of the script and vibe coding prompt mentioned in this article, please subscribe to our Youtube Channel and comment “sitemap” in the following video (Dynamic Sitemap Script and Vibe Coding Prompt)
- Support our channel through PayPal (paypal.me/Easy2digital)
- Follow our Facebook page
- Sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software