librelist archives

« back to archive

RE : [flask] Sitemap automatic generation

RE : [flask] Sitemap automatic generation

From:
Brosseau Valentin
Date:
2011-10-27 @ 20:56
Yep you can do this using internal routes like :

sitemap.xml. ( the template):

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {% for rule in rules %}
            <url>
              <loc>{{url_root}}{{rule}}</loc>
              <changefreq>weekly</changefreq>
            </url>
    {% endfor %}
</urlset>

The python function :

@app.route('/sitemap.xml')
def sitemap():
    url_root = request.url_root[:-1]
    rules = app.url_map.iter_rules()
    return render_template('sitemap.xml', url_root=url_root, rules=rules)

Best regards

Le 27 oct. 2011 22:49, "Manel Villar" <manelvf@gmail.com> a écrit :

Hi,

Is there a way to generate a sitemap for an app? Maybe accesing routes
internal register...

This question was asked before (
http://librelist.com/browser//flask/2011/1/18/flask-generating-sitemaps/)
but not answered.

Thanks,

-- 
Manel Villar
Cerca,74 1ºD
15365 Cariño
A Coruña (Spain)
Tel: (+34) 636863621
manelvf@gmail.com

*"Ars longa, vita brevis"*

Re: RE : [flask] Sitemap automatic generation

From:
Simon Sapin
Date:
2011-10-27 @ 23:30
Le 27/10/2011 22:56, Brosseau Valentin a écrit :
> Yep you can do this using internal routes like :
>
> sitemap.xml. ( the template):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
>      {% for rule in rules %}
> <url>
> <loc>{{url_root}}{{rule}}</loc>
> <changefreq>weekly</changefreq>
> </url>
>      {% endfor %}
> </urlset>
>
> The python function :
>
> @app.route('/sitemap.xml')
> def sitemap():
>      url_root = request.url_root[:-1]
>      rules = app.url_map.iter_rules()
>      return render_template('sitemap.xml', url_root=url_root, rules=rules)
>

Hi,

I think this will just print the URL rule as-is which is not what you 
want if the rule has parameters. If you want to list all possible URLs, 
you need all values that these parameters can take and this is a bit 
more complicated.

Frozen-Flask has several mechanisms to know which URLs to build. This is 
all documented:

http://packages.python.org/Frozen-Flask/

Namely, it will automatically find URLs for rules without parameters, 
static files, as well as calls to url_for() while rendering other URLs.
For everything else, you need to write "URL generator" functions to tell 
what are the possible values for an URL parameter.

I guess this could be re-used to build a sitemap. Start with the 
Freezer.all_urls() method:

http://packages.python.org/Frozen-Flask/#flask_frozen.Freezer.all_urls

Regards,
-- 
Simon Sapin
http://exyr.org

Re: RE : [flask] Sitemap automatic generation

From:
Manel Villar
Date:
2011-10-28 @ 21:08
Good answers. Thank you.

2011/10/28 Simon Sapin <simon.sapin@exyr.org>

> Le 27/10/2011 22:56, Brosseau Valentin a écrit :
> > Yep you can do this using internal routes like :
> >
> > sitemap.xml. ( the template):
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
> >      {% for rule in rules %}
> > <url>
> > <loc>{{url_root}}{{rule}}</loc>
> > <changefreq>weekly</changefreq>
> > </url>
> >      {% endfor %}
> > </urlset>
> >
> > The python function :
> >
> > @app.route('/sitemap.xml')
> > def sitemap():
> >      url_root = request.url_root[:-1]
> >      rules = app.url_map.iter_rules()
> >      return render_template('sitemap.xml', url_root=url_root,
> rules=rules)
> >
>
> Hi,
>
> I think this will just print the URL rule as-is which is not what you
> want if the rule has parameters. If you want to list all possible URLs,
> you need all values that these parameters can take and this is a bit
> more complicated.
>
> Frozen-Flask has several mechanisms to know which URLs to build. This is
> all documented:
>
> http://packages.python.org/Frozen-Flask/
>
> Namely, it will automatically find URLs for rules without parameters,
> static files, as well as calls to url_for() while rendering other URLs.
> For everything else, you need to write "URL generator" functions to tell
> what are the possible values for an URL parameter.
>
> I guess this could be re-used to build a sitemap. Start with the
> Freezer.all_urls() method:
>
> http://packages.python.org/Frozen-Flask/#flask_frozen.Freezer.all_urls
>
> Regards,
> --
> Simon Sapin
> http://exyr.org
>



-- 
Manel Villar
Cerca,74 1ºD
15365 Cariño
A Coruña (Spain)
Tel: (+34) 636863621
manelvf@gmail.com

*"Ars longa, vita brevis"*