librelist archives

« back to archive

from simple app to blueprint, initialising and getting a backend

from simple app to blueprint, initialising and getting a backend

From:
Daniele Sluijters
Date:
2013-08-15 @ 15:06
Hi,

I’m trying to rewrite an application to use the Blueprint pattern and
application factories which should make testing significantly easier
too.

However, my application uses a backend that needs to be initialised.
Doing so doesn’t connect to anything just yet and I don’t need to
explicitly open a connection to my backend either before I can query
data from it, it’s just another REST API that’s being consumed.

Right now setting up the backend happens in the app.py:

from mybackend import backend
backend = backend(host=app.config[‘BACKEND_HOST’] …. )

This however doesn’t work if you try to rebuild this into a Blueprint
as you’ll get errors about a missing app_context when you try
and start the application (at least that’s what happend to me).

I figured I should initialise that backend somehow and get
that into my views, somehow. I’m just not clear on the how.
There seems to be a common practice on doing such
setup in before_request handlers but I’m not exactly clear
how that would then make that backend available in my views.

I’ve tried looking at what flask-peewe and flask-sqlalchemy
do but I just can’t wrap my head around it.

I was hoping someone could light the way.

Kind regards,

-- 
Daniele Sluijters

Re: [flask] from simple app to blueprint, initialising and getting a backend

From:
Daniele Sluijters
Date:
2013-08-16 @ 16:06
Hey,

I hadn't realised I could just hook up my backend to
the 'g' object inside those functions. That solves the
issue quite nicely.

Thank you very much,

--
Daniele Sluijters

Re: [flask] from simple app to blueprint, initialising and getting a backend

From:
Jaime Wyant
Date:
2013-08-15 @ 15:53
You may want to check out this blueprint method:


before_app_first_request(*f*)¶<http://flask.pocoo.org/docs/api/#flask.Blueprint.before_app_first_request>

Like 
Flask.before_first_request()<http://flask.pocoo.org/docs/api/#flask.Flask.before_first_request>.
Such a function is executed before the first request to the application.
Have your callback function, f, initialize the backend for you.  Watch out
for any threading issues.  If initialization isn't expensive, then you
could do it with ...


before_app_request(*f*)<http://flask.pocoo.org/docs/api/#flask.Blueprint.before_app_request>

Like 
Flask.before_request()<http://flask.pocoo.org/docs/api/#flask.Flask.before_request>.
Such a function is executed before each request, even if outside of a
blueprint.
Inside of before_app_request you'll have access to `g`, so you can assign
your backend to that variable.


hth,
jw


On Thu, Aug 15, 2013 at 10:06 AM, Daniele Sluijters <
daniele.sluijters@nedap.com> wrote:

> Hi,
>
> I’m trying to rewrite an application to use the Blueprint pattern and
> application factories which should make testing significantly easier
> too.
>
> However, my application uses a backend that needs to be initialised.
> Doing so doesn’t connect to anything just yet and I don’t need to
> explicitly open a connection to my backend either before I can query
> data from it, it’s just another REST API that’s being consumed.
>
> Right now setting up the backend happens in the app.py:
>
> from mybackend import backend
> backend = backend(host=app.config[‘BACKEND_HOST’] …. )
>
> This however doesn’t work if you try to rebuild this into a Blueprint
> as you’ll get errors about a missing app_context when you try
> and start the application (at least that’s what happend to me).
>
> I figured I should initialise that backend somehow and get
> that into my views, somehow. I’m just not clear on the how.
> There seems to be a common practice on doing such
> setup in before_request handlers but I’m not exactly clear
> how that would then make that backend available in my views.
>
> I’ve tried looking at what flask-peewe and flask-sqlalchemy
> do but I just can’t wrap my head around it.
>
> I was hoping someone could light the way.
>
> Kind regards,
>
> --
> Daniele Sluijters
>



-- 
"Government does not solve problems; it subsidizes them."
Ronald Reagan