librelist archives

« back to archive

Flask configuration best practices

Flask configuration best practices

From:
Desmond Rivet
Date:
2012-10-18 @ 00:19
Hi all,

I'm wondering if there are any best practices in regards to Flask configuration.

For example, how do you guys prevent Flask plugins from stepping on
each other's toes with respect to configuration keys?  Do you just
make sure each plugin's config keys begins with a different string?

Is it considered bad practice to "nest" configuration,  i.e. store a
piece of config that points to another dictionary?  Or do you guys
prefer to keep everything flat?  Or do you guys have no opinion one
way or the other?

At the moment I'm writing a flask application that has its own
non-standard "plugin" structure. Each plugin can have it's own
configuration.  I'm tempted to nest the config in the global flask
config object to prevent conflicts, but I just wanted to see what
everyone thought.

Thanks in advance for any advice,

Desmond

Re: [flask] Flask configuration best practices

From:
Simon Sapin
Date:
2012-10-18 @ 08:27
Le 18/10/2012 02:19, Desmond Rivet a écrit :
> Hi all,
>
> I'm wondering if there are any best practices in regards to Flask configuration.
>
> For example, how do you guys prevent Flask plugins from stepping on
> each other's toes with respect to configuration keys?  Do you just
> make sure each plugin's config keys begins with a different string?
>
> Is it considered bad practice to "nest" configuration,  i.e. store a
> piece of config that points to another dictionary?  Or do you guys
> prefer to keep everything flat?  Or do you guys have no opinion one
> way or the other?
>
> At the moment I'm writing a flask application that has its own
> non-standard "plugin" structure. Each plugin can have it's own
> configuration.  I'm tempted to nest the config in the global flask
> config object to prevent conflicts, but I just wanted to see what
> everyone thought.

Hi,

I’m not sure what exactly you call a "plugin", but I think that the 
closest we have in Flask are extensions:

http://flask.pocoo.org/docs/extensions/
http://flask.pocoo.org/extensions/

The convention for extension configuration is to use config keys with a 
prefix based on the name of the extension. For example, Flask-SQLAlchemy 
uses SQLALCHEMY_DATABASE_URI rather than just DATABASE_URI.

For run-time objects like a database connection pool, there is the 
app.extensions dict:

http://flask.pocoo.org/docs/api/#flask.Flask.extensions

Cheers,
-- 
Simon Sapin