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 urlparse
From 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 statics 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 server side, we need to use Jinj. a2 in frontend 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
Full Scripts & Commands of Flask App Deployment on AWS EC2
If you are interested in Chapter 74 – Flask App Dynamic Sitemap XML Using MongoDB, please subscribe to our newsletter by adding the message ‘Chapter 74 + flask dynamic sitemap’. We would send you the script immediately to your mailbox.
I hope you enjoy reading Chapter 74 – Flask App Dynamic Sitemap XML Using MongoDB. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support and Donate to our channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article on your social network with the hashtag #easy2digital
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount codes
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)