librelist archives

« back to archive

Blueprint static_folder and send_static_file

Blueprint static_folder and send_static_file

From:
Malphas Wats
Date:
2012-07-10 @ 12:56
Hi,

  I've created a simple profile blueprint that either reads a user's
avatar from a blob field in a database, or sends a default .png file:

    from flask import (Blueprint, request, session, g, redirect, url_for,
                   abort, render_template, flash, jsonify, Response)

    from BlendedDimension.access_control import login_required
    import BlendedDimension.database as database

    mod = Blueprint('profile', __name__, static_folder='static')

    @mod.route('/profile/avatar/<int:user_id>')
    @login_required
    def avatar(user_id):
        avatar = database.query("""SELECT avatar FROM profiles WHERE
user_id=%s""", (user_id,))
        if avatar:
            r = Response(avatar[0]['avatar'], mimetype='image/png')
            return r
        else:
            mod.send_static_file('defaultAvatar.png')

but I get a 404 error - how do you specify a static folder for a
blueprint? is there a better way to do what I'm trying to do?

Re: [flask] Blueprint static_folder and send_static_file

From:
Anderson Cardoso
Date:
2012-07-10 @ 14:17
Have you looked at 'send_file' ?
Your 404 Is in which part? on the send_static_file instruction?

att

Anderson

http://flask.pocoo.org/docs/api/#flask.send_file

2012/7/10 Malphas Wats <malphas@subdimension.co.uk>

> Hi,
>
>   I've created a simple profile blueprint that either reads a user's
> avatar from a blob field in a database, or sends a default .png file:
>
>     from flask import (Blueprint, request, session, g, redirect, url_for,
>                    abort, render_template, flash, jsonify, Response)
>
>     from BlendedDimension.access_control import login_required
>     import BlendedDimension.database as database
>
>     mod = Blueprint('profile', __name__, static_folder='static')
>
>     @mod.route('/profile/avatar/<int:user_id>')
>     @login_required
>     def avatar(user_id):
>         avatar = database.query("""SELECT avatar FROM profiles WHERE
> user_id=%s""", (user_id,))
>         if avatar:
>             r = Response(avatar[0]['avatar'], mimetype='image/png')
>             return r
>         else:
>             mod.send_static_file('defaultAvatar.png')
>
> but I get a 404 error - how do you specify a static folder for a
> blueprint? is there a better way to do what I'm trying to do?
>



-- 
Anderson Pierre Cardoso
Computer Engineer - University of São Paulo
*
http://www.apierrecardoso.com*
twitter: *@apierre_cardoso* <https://twitter.com/#%21/apierre_cardoso>

Re: [flask] Blueprint static_folder and send_static_file

From:
Malphas Wats
Date:
2012-07-11 @ 10:56
On Tue, Jul 10, 2012 at 3:17 PM, Anderson Cardoso
<apierre.cardoso@gmail.com> wrote:
> Have you looked at 'send_file' ?
> Your 404 Is in which part? on the send_static_file instruction?
>
> att
>
> Anderson
>
> http://flask.pocoo.org/docs/api/#flask.send_file

Thanks Anderson,

I thought I'd seen a send_file function somewhere but couldn't find
the details! Still hitting these walls every time I come to a new
piece of the flask puzzle!

I did manage to get send_static_file working, by specifying the static
directory for the Blueprint, but I prefer send_file, as it keeps
things more generic.

Thanks