How to use app.config.from_envvar? (Flask)

How to use app.config.from_envvar ()?

I am looking at a Flask document and looking for this topic that I all know to do this.

DATABASE = 'flaskr.db' DEBUG = True SECRET_KEY = 'development key' app = Flask(__name__) app.config.from_envvar('FLASKR_SETTINGS', silent=True) 

Will the configuration be loaded from FLASKR_SETTINGS? and how can a program know what is FLASKR_SETTINGS? should I also install something like this (path to the configuration file) ?:

 FLASKR_SETTINGS = desktop/my_flask_project/FlaskConfig 

and transfer the first 3 lines to this file, and when I run this file, will it be loaded into?

and I just prefer to use these rights? between app.config.from_envvar (this one to configure loading from an external file) or app.config.from_object ( name ) (this will load the configuration into a file)? Do I understand correctly?

+8
python flask
source share
2 answers

Loads the Flask configuration from the file specified by the environment variable.

This is described in the documentation: http://flask.pocoo.org/docs/config/#configuring-from-files

 $ export YOURAPPLICATION_SETTINGS=/path/to/settings.cfg $ python run-app.py * Running on http://127.0.0.1:5000/ * Restarting with reloader... 

http://en.wikipedia.org/wiki/Environment_variable

+3
source share

envvar not suitable for Environment Variable . If you use a Linux-based OS (Ubuntu, Mac, etc.), then when you start a regular shell, you probably run bash . To set the environment variable in bash, you simply do:

 $ SOME_NAME=some_value 

So, in the case of a Flask application configured from the FLASKR_SETTINGS environment FLASKR_SETTINGS , you should:

 $ FLASKR_SETTINGS=/path/to/settings/file.ext $ python your_script.py 

What Flask does is simply import this file as if it were a regular Python file and pulled every UPPERCASE_ONLY name in the file (any other combination would be ignored).

The same is true for from_object - in fact, from_object can also accept an imported string:

 app.config.from_object("importable.configuration") 

Finally, note that you do not need to have only one configuration call - you can use several calls:

 app.config.from_object("your.package.default.config") app.config.from_envvar("YOUR_APPS_ENVVAR", silent=True) 
+5
source share

All Articles