librelist archives

« back to archive

Access Config in Blueprint outside of request

Access Config in Blueprint outside of request

From:
Sean Lynch
Date:
2012-05-28 @ 16:55
Is there a way to access the app.config from a Blueprint outside of a
request. Normally I would use:

from flask import current_app
> current_app.config['FOO']


but this only works when inside a request (view).  I would like to store
some consumer_key and consumer_secret used for Flask-OAuth in my config
file, and need to init the twitter/facebook oauth remote apps outside a
request.

Worst case, I can access it from my global app instance:

> from application import app
> app.config['FOO']


but this is tying the blueprint to the application's file/module structure.

Thanks,
-Sean

Re: [flask] Access Config in Blueprint outside of request

From:
Simon Sapin
Date:
2012-05-29 @ 13:23
Le 28/05/2012 18:55, Sean Lynch a écrit :
> Is there a way to access the app.config from a Blueprint outside of a
> request. Normally I would use:
>
>     from flask import current_app
>     current_app.config['FOO']
>
>
> but this only works when inside a request (view).  I would like to store
> some consumer_key and consumer_secret used for Flask-OAuth in my config
> file, and need to init the twitter/facebook oauth remote apps outside a
> request.
>
> Worst case, I can access it from my global app instance:
>
>     from application import app
>     app.config['FOO']
>
>
> but this is tying the blueprint to the application's file/module structure.


In general, there could be more that one app (instances of the Flask 
class). When accessing the config, you need some way to know on which app.

The two ways to do that are as you said. However, you can have a request 
context without a real request by using app.test_request_context()

Flask 0.9 will introduce "app contexts" that enable the use of 
flask.current_app when a request context does not really make sense. (In 
the meantime, I think there is no big problem in using a request context 
when you would only need an app context.)

http://flask.pocoo.org/docs/api/#flask.Flask.test_request_context
http://flask.pocoo.org/docs/api/#flask.Flask.app_context

Regards,
-- 
Simon Sapin

Re: [flask] Access Config in Blueprint outside of request

From:
Sean Lynch
Date:
2012-05-29 @ 19:56
It looks like both of those ways still requires I have access to the app
instance.  After more thought, I think it might be best if I change this
Blueprint into an extension instead, as that way I have access to the
original app when it's registered.  It's also a piece of functionality I
want to share across a lot of applications, and while Blueprints will work,
extensions may be a better fit.

Btw, I noticed when reading the "Modular Applications with Blueprints" the
following line:

> Blueprints instead provide separation at the Flask level,* share
> application config,* and can change an application object as necessary
> with being registered


This was confusing to me not to be able to do "bp.config" to access the
shared application config, but it makes since with the ability to register
multiple blueprints, etc, and not knowing which app instance it is.



On Tue, May 29, 2012 at 9:23 AM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 28/05/2012 18:55, Sean Lynch a écrit :
> > Is there a way to access the app.config from a Blueprint outside of a
> > request. Normally I would use:
> >
> >     from flask import current_app
> >     current_app.config['FOO']
> >
> >
> > but this only works when inside a request (view).  I would like to store
> > some consumer_key and consumer_secret used for Flask-OAuth in my config
> > file, and need to init the twitter/facebook oauth remote apps outside a
> > request.
> >
> > Worst case, I can access it from my global app instance:
> >
> >     from application import app
> >     app.config['FOO']
> >
> >
> > but this is tying the blueprint to the application's file/module
> structure.
>
>
> In general, there could be more that one app (instances of the Flask
> class). When accessing the config, you need some way to know on which app.
>
> The two ways to do that are as you said. However, you can have a request
> context without a real request by using app.test_request_context()
>
> Flask 0.9 will introduce "app contexts" that enable the use of
> flask.current_app when a request context does not really make sense. (In
> the meantime, I think there is no big problem in using a request context
> when you would only need an app context.)
>
> http://flask.pocoo.org/docs/api/#flask.Flask.test_request_context
> http://flask.pocoo.org/docs/api/#flask.Flask.app_context
>
> Regards,
> --
> Simon Sapin
>

Re: [flask] Access Config in Blueprint outside of request

From:
Simon Sapin
Date:
2012-05-30 @ 09:20
Le 29/05/2012 21:56, Sean Lynch a écrit :
> It looks like both of those ways still requires I have access to the app
> instance.

If you do not have a request context already I assume this is some kind 
of script. The idea is to create a request/app context at the beginning 
of the script; indeed you need the app object for that. But this is only 
at one point. The rest of your code can use flask.current_app.


> After more thought, I think it might be best if I change this
> Blueprint into an extension instead, as that way I have access to the
> original app when it's registered.  It's also a piece of functionality I
> want to share across a lot of applications, and while Blueprints will
> work, extensions may be a better fit.

Either can make sense, depending on what you want to do. Extensions are 
not even a "real thing" (there is no Extension class you can derive), 
just a pattern of how we usually organize stuff. Feel free to experiment 
with whatever API / packaging works for you.


> Btw, I noticed when reading the "Modular Applications with Blueprints"
> the following line:
>
>     Blueprints instead provide separation at the Flask level,*share
>     application config,* and can change an application object as
>     necessary with being registered
>
>
> This was confusing to me not to be able to do "bp.config" to access the
> shared application config, but it makes since with the ability to
> register multiple blueprints, etc, and not knowing which app instance it is.

Also, the same blueprint could be registered multiple times on different 
apps (or even on the same app at different URL prefixes).

The docs can always be better. They live in the same git repository as 
Flask itself, in case you feel like improving them.

Regards,
-- 
Simon Sapin

Re: [flask] Access Config in Blueprint outside of request

From:
Sean Lynch
Date:
2012-05-30 @ 12:38
Thanks. That helps to blest things up.
On May 30, 2012 5:21 AM, "Simon Sapin" <simon.sapin@exyr.org> wrote:

> Le 29/05/2012 21:56, Sean Lynch a écrit :
> > It looks like both of those ways still requires I have access to the app
> > instance.
>
> If you do not have a request context already I assume this is some kind
> of script. The idea is to create a request/app context at the beginning
> of the script; indeed you need the app object for that. But this is only
> at one point. The rest of your code can use flask.current_app.
>
>
> > After more thought, I think it might be best if I change this
> > Blueprint into an extension instead, as that way I have access to the
> > original app when it's registered.  It's also a piece of functionality I
> > want to share across a lot of applications, and while Blueprints will
> > work, extensions may be a better fit.
>
> Either can make sense, depending on what you want to do. Extensions are
> not even a "real thing" (there is no Extension class you can derive),
> just a pattern of how we usually organize stuff. Feel free to experiment
> with whatever API / packaging works for you.
>
>
> > Btw, I noticed when reading the "Modular Applications with Blueprints"
> > the following line:
> >
> >     Blueprints instead provide separation at the Flask level,*share
> >     application config,* and can change an application object as
> >     necessary with being registered
> >
> >
> > This was confusing to me not to be able to do "bp.config" to access the
> > shared application config, but it makes since with the ability to
> > register multiple blueprints, etc, and not knowing which app instance it
> is.
>
> Also, the same blueprint could be registered multiple times on different
> apps (or even on the same app at different URL prefixes).
>
> The docs can always be better. They live in the same git repository as
> Flask itself, in case you feel like improving them.
>
> Regards,
> --
> Simon Sapin
>