Flask: should my own config be added to app.config?

Consider a Flask application that imports a configuration class that generates configuration from a file (YAML, in this example):

from config import ConfigGenerator
app = Flask(__name__)

      

What's the right place for a config object in a Flask app?

Should it be in the module namespace:

config = ConfigGenerator('/etc/server/config.yaml')    

      

Or in app.config

:

app.config['my_config'] = ConfigGenerator('/etc/server/config.yaml')    

      

Or even right below app

:

app.myconfig = ConfigGenerator('/etc/server/config.yaml')    

      

I think all three will work, but I'm wondering if there are any style or flash specific considerations to take into account.

+3


source to share


1 answer


The first two parameters do not differ much from each other, except that you no longer need to import your config object separately. In other words, using app.config

, you can access the configuration throughout the Flask application where you already have access to current_app

.



The last parameter, however, should not be used, do not add attributes to the Flask object.

+4


source







All Articles