librelist archives

« back to archive

Flask REST API /w frontend

Flask REST API /w frontend

From:
ol
Date:
2012-06-27 @ 15:55
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all of you fine gentlemen and gentlewomen flaskers.

Im setting up a new flask project, i want to develop a rest api. But i
also want a front end using the same api.

I see a few obvious takes on this:

* render json in any case with a javascript heavy frontend (i love my
terminals and lynx, so not really loving it)
* render to template or json based on x (not really loving this either
as is not proper api access and output types can fragment)

So do you guys have any other takes on this, or can help me decide
over witch road to go of the above?

class TakeOneAPI():
  def get():
    return jsonify(...
    # cry

class TakeTwoAPI(MethodView):
   def get():
     if response.is_xhr:
       return jsonify(...
     return render_to_template(...
    # cry more

Happy flasking!

- -- ol
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJP6yzsAAoJENnD1Z3aOEszmesH/RzFQoIfcSl0KT3no3JLx8b3
h4w+VQQUBttq6exHp/hUJld31MjiCv+CS6NL15S6Do5wfqFWEWCtPsa+pUFETQOu
svl9aA+LAJKSPoVJ7SHeA2IEoJUUDWd/MBcglgjhLrNBwTsjwbNI0LseoVGEqG1H
7WdUVPMK4FmJ47jWQBXLIxSjlDDXMmpY3dou9z8IMs8c1ZhiMN6VSkXC0eFy5Glz
cJYjct8/Ova3helHKStJCMp7GncXzmh4utz40b0JmDycxB/QSB5M3pSKasg2888u
ee/X4JaD1f88pyXmbPLB92UJreJmAZ3Srrj+gZXSw7eBX7Fqj+1o2AYwSYP5XpE=
=3Kvp
-----END PGP SIGNATURE-----

Re: [flask] Flask REST API /w frontend

From:
Steven Kryskalla
Date:
2012-06-27 @ 16:40
On Wed, Jun 27, 2012 at 8:55 AM, ol <ol@o1.no> wrote:
> So do you guys have any other takes on this, or can help me decide
> over witch road to go of the above?
>
> class TakeOneAPI():
>  def get():
>    return jsonify(...
>    # cry
>
> class TakeTwoAPI(MethodView):
>   def get():
>     if response.is_xhr:
>       return jsonify(...
>     return render_to_template(...
>    # cry more
>

What if you created the REST API, but then consumed it server-side for
the frontend app, instead of consuming it client-side in the browser.

Since it's all within the same flask app, you can even avoid the HTTP
request/response and just call into the view method directly. Like
this:

https://gist.github.com/3005268

Other than that, I would go with the client-side approach (your first
bullet point, "javascript heavy frontend"). If you really like the
terminal, build a command line client from the REST API.

-Steve

Re: [flask] Flask REST API /w frontend

From:
Audrius Kažukauskas
Date:
2012-06-27 @ 17:55
On Wed, 2012-06-27 at 09:40:13 -0700, Steven Kryskalla wrote:
> What if you created the REST API, but then consumed it server-side for
> the frontend app, instead of consuming it client-side in the browser.
> 
> Since it's all within the same flask app, you can even avoid the HTTP
> request/response and just call into the view method directly. Like
> this:
> 
> https://gist.github.com/3005268

This reminds me of a blog post I've read recently which talks about
similar approach:


http://techspot.zzzeek.org/2012/06/18/server-side-templates-and-api-centric-development/

It's not directly related to Flask, an example given there is a Pyramid
app (haven't used Pyramid at all, I found it pretty straightforward).
Still, I liked the idea of applying different renderers for different
routes while using the single API method to calculate the results.  I
guess something similar could be done in Flask as well.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Flask REST API /w frontend

From:
ol
Date:
2012-07-03 @ 10:38
Hi, thanks for both of your input, ill do some experimenting and report
back/do a writeup.

-- ol

On Wed, Jun 27, 2012 at 7:55 PM, Audrius Kažukauskas <audrius@neutrino.lt>wrote:

> On Wed, 2012-06-27 at 09:40:13 -0700, Steven Kryskalla wrote:
> > What if you created the REST API, but then consumed it server-side for
> > the frontend app, instead of consuming it client-side in the browser.
> >
> > Since it's all within the same flask app, you can even avoid the HTTP
> > request/response and just call into the view method directly. Like
> > this:
> >
> > https://gist.github.com/3005268
>
> This reminds me of a blog post I've read recently which talks about
> similar approach:
>
>
> 
http://techspot.zzzeek.org/2012/06/18/server-side-templates-and-api-centric-development/
>
> It's not directly related to Flask, an example given there is a Pyramid
> app (haven't used Pyramid at all, I found it pretty straightforward).
> Still, I liked the idea of applying different renderers for different
> routes while using the single API method to calculate the results.  I
> guess something similar could be done in Flask as well.
>
> --
> Audrius Kažukauskas
> http://neutrino.lt/
>