Categories
Python Answers

How to serve robot.txt, sitemap.xml static files in Python Flask?

Spread the love

Sometimes, we want to serve robot.txt, sitemap.xml static files in Python Flask.

In this article, we’ll look at how to serve robot.txt, sitemap.xml static files in Python Flask.

How to serve robot.txt, sitemap.xml static files in Python Flask?

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

For instance, we write

from flask import Flask

app = Flask(__name__, static_folder='static', static_url_path='')

to create a Flask instance with the static_folder argument set to the 'static' folder in our Flask app project’s folder.

Then we can put robots.txt and sitemap.xml inside it.

Conclusion

To serve robot.txt, sitemap.xml static files in Python Flask, we can expose a static folder in our Flask app project’s folder.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *