Hello @h-janes
After reading "Dynamic urls with a function lambda" #12 , I suggest adding this dynamic route example to the wiki.
I've written a new sitemap example with dynamic urls using random data rather than a set list.
If you want to integrate it in the doc.
Thanks for your module, it's really useful.
I can create a pull request if that's more convenient.
"""
This script sets up a Flask web application with a sitemap generator.
The application includes the following routes:
- `/user/<int:user_id>`: Displays an HTML header for a user with the given user ID.
- `/sitemap.xml` and `/`: Generates and returns a sitemap.
Functions:
- generate_user_ids(): Generates a dictionary containing a list of 5 random user IDs.
- r_user(user_id): Generates an HTML header for a user.
- r_sitemap(): Generates and returns a sitemap.
Modules:
- random: Provides functions to generate random numbers.
- flask: A micro web framework for Python.
- flask_sitemapper: A Flask extension for generating sitemaps.
Classes:
- Sitemapper: A class from the `flask_sitemapper` module used to generate sitemaps.
Attributes:
- sitemapper: An instance of the `Sitemapper` class.
- app: An instance of the `Flask` class.
"""
import random
from flask import Flask
from flask_sitemapper import Sitemapper
def generate_user_ids():
"""
Generates a dictionary containing a list of 5 random user IDs.
The function generates a list of 5 random integers between 1 and 10 (inclusive)
and returns a dictionary with the key 'user_id' and the list of random integers as the value.
Returns:
dict: A dictionary with a single key 'user_id' and a list of 5 random integers as the value.
"""
random_integers = [random.randint(1, 10) for _ in range(5)]
print(random_integers)
return {"user_id": random_integers}
sitemapper = Sitemapper()
app = Flask(__name__)
sitemapper.init_app(app)
@sitemapper.include(url_variables=generate_user_ids)
@app.route("/user/<int:user_id>")
def r_user(user_id):
"""
Generate an HTML header for a user.
Args:
user_id (int): The ID of the user.
Returns:
str: An HTML string containing the user ID in an <h1> tag.
"""
return f"<h1>User #{user_id}</h1>"
@app.route("/sitemap.xml")
@app.route("/")
def r_sitemap():
"""
Generates and returns a sitemap.
This function calls the `generate` method from the `sitemapper` module
to create a sitemap.
Returns:
object: The generated sitemap.
"""
return sitemapper.generate()
app.run("127.0.0.1", 8000)
Hi thanks for the suggestion! I can include this into the wiki when I get back from vacation. If you create a pull request I may be able to merge it sooner. Thanks!
Hello @h-janes After reading "Dynamic urls with a function lambda" #12 , I suggest adding this dynamic route example to the wiki. I've written a new sitemap example with dynamic urls using random data rather than a set list. If you want to integrate it in the doc. Thanks for your module, it's really useful. I can create a pull request if that's more convenient.