Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

flask

How to use app.config.from_envvar()?

I have look at Flask doc and search for this topic what I all knows is to do this.

DATABASE = 'flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'

app = Flask(__name__)
app.config.from_envvar(’FLASKR_SETTINGS’, silent=True)

Will this load the config from FLASKR_SETTINGS ? and how can the program knows what is FLASKR_SETTINGS? should I also set something like this (the path to the config file)?:

FLASKR_SETTINGS = desktop/my_flask_project/FlaskConfig

and move the first 3 lines into that file and when I run this file, it will be loaded in?

and I only choose to use of of these right? between the app.config.from_envvar (this one for load config from external file)or the app.config.from_object(name)(this one will load config within the file) ? Am i understand correctly?

like image 394
Team Avatar asked Nov 19 '13 02:11

Team


1 Answers

envvar is short for Environment Variable. If you are using a Linux-based OS (Ubuntu, Mac, etc.) then when you run a normal shell you are probably running bash. To set an environment variable in bash you simply do:

$ SOME_NAME=some_value

So in the case of a Flask application that configured itself from the FLASKR_SETTINGS environment variable, you would do:

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

What Flask does is simply import that file as if it was an ordinary Python file and pull out every UPPERCASE_ONLY name in the file (Any other caseCombination will be ignored).

The same is true for from_object - in fact, from_object can also take an importable string:

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

Finally, note that you do not have to only have one config call - multiple calls can be used:

app.config.from_object("your.package.default.config")
app.config.from_envvar("YOUR_APPS_ENVVAR", silent=True)
like image 98
Sean Vieira Avatar answered Nov 15 '22 17:11

Sean Vieira