Re: [flask] Return type dict
- From:
- Peter Ward
- Date:
- 2010-05-01 @ 00:33
I've been interested in this idea, too.
The usage I've designed is this:
from flasktest import app
from helpers import render_html, render_json
@app.route('/<name>', render_html('page.html'))
@app.route('/api/page/<name>', render_json)
def show_page(name):
page = load_page(name)
return dict(title=page.title, contents=page.contents)
This would make two routes for this function - one which renders using a
template, and one which provides the data in json format.
My implementation of this does deal with redirects correctly (and probably
other odd things you could throw at it), but is still slightly hacky. I'm
sure it could be implemented without the hacks I did.
On Wed, Apr 28, 2010 at 7:18 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:
> The purpose is that if you wanted to, it can intelligently fill in
> some reasonable defaults... you don't have to explicitly declare your
> template if it is the same name as the function.
>
> As a decorator, it would only intercept if the return is of type dict,
> otherwise it still passes it to make_response unchanged.
>
> This does not intercept make_response, it just adds one more simple
> check to it, so there is no need to code extra exceptions.
>
> There also is no change to the render_template function.
> --
> Thadeus
>
>
>
>
>
> On Tue, Apr 27, 2010 at 3:54 PM, Dan Jacob <danjac354@gmail.com> wrote:
> > Generally, "explicit is better than implicit". Using an explicit
> > template name (i.e. "index.html" instead of "index") is easier to
> > understand; it's one less rule to keep in your head. Again with a
> > render decorator; what if I want to return a redirect instead,
> > depending on what happens inside the view ? You would need to code in
> > the exceptions, and understand when and how these exceptions work.
> >
> > The render_template function is perhaps a little bit more typing, but
> > it's easy to understand and maintain.
> >
> > On 27 April 2010 20:45, Thadeus Burgess <thadeusb@thadeusb.com> wrote:
> >> Alternatively, this could be a decorator
> >>
> >> @render('my_template.html')
> >> def hello():
> >> return dict(hello='ehlo', world='World!')
> >>
> >> --
> >> Thadeus
> >>
> >
> >
> >
> > --
> > Dan Jacob
> > Skype: danjac40
> > Mobile: (++44) (0)777 290 8352
> >
>
--
Peter Ward
http://flowblok.id.au/
Re: [flask] Return type dict
- From:
- Armin Ronacher
- Date:
- 2010-05-01 @ 16:19
Hi,
On 2010-05-01 2:33 AM, Peter Ward wrote:
> This would make two routes for this function - one which renders using a
> template, and one which provides the data in json format.
Why don't you make it a decorator? No changes in flask are necessary
for that. Example:
def templated(template_name):
def decorator(f):
@wraps(f)
def decorated(*args, **kwargs):
d = f(*args, **kwargs)
return render_template(template_name, **d)
return decorated
return decorator
And use it like this:
@app.route('/')
@templated
def index():
return dict(vars=[1, 2, 3, 4])
Etc.
Regards,
Armin
Re: [flask] Return type dict
- From:
- Thadeus Burgess
- Date:
- 2010-05-01 @ 16:22
That is what I said in the very beginning. Since you are already
integrating jinja2, you might as well include this as a decorator that
comes with flask.
--
Thadeus
On Sat, May 1, 2010 at 11:19 AM, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 2010-05-01 2:33 AM, Peter Ward wrote:
>> This would make two routes for this function - one which renders using a
>> template, and one which provides the data in json format.
> Why don't you make it a decorator? No changes in flask are necessary
> for that. Example:
>
> def templated(template_name):
> def decorator(f):
> @wraps(f)
> def decorated(*args, **kwargs):
> d = f(*args, **kwargs)
> return render_template(template_name, **d)
> return decorated
> return decorator
>
> And use it like this:
>
> @app.route('/')
> @templated
> def index():
> return dict(vars=[1, 2, 3, 4])
>
> Etc.
>
>
> Regards,
> Armin
>
Re: [flask] Return type dict
- From:
- Armin Ronacher
- Date:
- 2010-05-01 @ 16:28
Hi,
On 2010-05-01 6:22 PM, Thadeus Burgess wrote:
> That is what I said in the very beginning. Since you are already
> integrating jinja2, you might as well include this as a decorator that
> comes with flask.
I don't like the idea of decorators for templating at all, so I don't
want to include it. But what I am willing to do is to add such a
decorator to the documentations in the pattern section.
Regards,
Armin
Re: [flask] Return type dict
- From:
- Sebastien Estienne
- Date:
- 2010-05-01 @ 17:57
Maybe flask would need a website where people could share these
'patterns' or code snippets.
A bit like http://www.djangosnippets.org/ but with a search engine,
this is where django snippets is lacking.
I've some snippets to share like flask integration with mongodb, or
code for pattern like pagination, using flask with gunicorn...
this website would also be usefull for jinja, to share macro and
custom filters...
Sebastien Estienne
On Sat, May 1, 2010 at 18:28, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 2010-05-01 6:22 PM, Thadeus Burgess wrote:
>> That is what I said in the very beginning. Since you are already
>> integrating jinja2, you might as well include this as a decorator that
>> comes with flask.
> I don't like the idea of decorators for templating at all, so I don't
> want to include it. But what I am willing to do is to add such a
> decorator to the documentations in the pattern section.
>
>
> Regards,
> Armin
>
Re: [flask] Return type dict
- From:
- Armin Ronacher
- Date:
- 2010-05-02 @ 09:58
Hi,
On 5/1/10 7:57 PM, Sebastien Estienne wrote:
> Maybe flask would need a website where people could share these
> 'patterns' or code snippets.
> A bit like http://www.djangosnippets.org/ but with a search engine,
> this is where django snippets is lacking.
That would be very cool, but I don't have the time for that. When this
is done it would be interesting to integrate that into the existing
website which is already powered by Flask for the mailinglist archive.
If anyone wants to work on that, that would be awesome :)
Regards,
Armin
Re: [flask] Return type dict
- From:
- Dan Jacob
- Date:
- 2010-05-02 @ 12:37
I was going to write a simple pastebin in Flask for work and as a
learning exercise, but ended up using a Trac plugin instead. Would
something like that suffice, with comments and a rating system
attached ?
On 2 May 2010 10:58, Armin Ronacher <armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 5/1/10 7:57 PM, Sebastien Estienne wrote:
>> Maybe flask would need a website where people could share these
>> 'patterns' or code snippets.
>> A bit like http://www.djangosnippets.org/ but with a search engine,
>> this is where django snippets is lacking.
> That would be very cool, but I don't have the time for that. When this
> is done it would be interesting to integrate that into the existing
> website which is already powered by Flask for the mailinglist archive.
> If anyone wants to work on that, that would be awesome :)
>
>
> Regards,
> Armin
>
--
Dan Jacob
Skype: danjac40
Mobile: (++44) (0)777 290 8352
Re: [flask] Return type dict
- From:
- Armin Ronacher
- Date:
- 2010-05-01 @ 16:22
Obviously usage looks like this:
@app.route('/')
@templated('index.html')
def index():
return dict(vars=[1, 2, 3, 4])
Regards,
Armin