简体   繁体   中英

Can someone explain to me (like I'm an idiot) what app.config does in Flask?

I have the following code in a Flask application

app = Flask(__name__)
app.config.from_object(app_config)
Session(app)

app_config.py is another file I have in my reposiotry with variables like CLIENT_ID, CLIENT_SECRET, AUTHORITY, and ENDPOINT. Can someone please explain to me what app.config does? And how configuration files work in general? Here is what the Flask documentation says:

class flask.Config(root_path, defaults=None)

Works exactly like a dict but provides ways to fill it from files or special dictionaries. There are two common patterns to populate the config.

Either you can fill the config from a config file:

app.config.from_pyfile('yourconfig.cfg')

Or alternatively you can define the configuration options in the module that calls from_object() or provide an import path to a module that should be loaded. It is also possible to tell it to use the same module and with that provide the configuration values just before the call:

DEBUG = True
SECRET_KEY = 'development key'
app.config.from_object(__name__)

In both cases (loading from any Python file or loading from modules), only uppercase keys are added to the config. This makes it possible to use lowercase values in the config file for temporary values that are not added to the config or to define the config keys in the same file that implements the application.

My main questions are what does it mean by "works exactly like a dict but provides ways to fill it form files or special dictionaries. Also what is the difference between a config file and a module?

For the record, if you're looking at this site, these are not the official Flask docs. Those are here.

Anyways, it's good practice to use config(uration) files (generally, overuse of config files is an anti-pattern ) to keep things like user settings. Config files are not unique to Flask.

All that the text you've copied is saying is that Flask includes an app object that can be configured and hold certain variables, for things like API or database access, using app.config . You can also keep these in a config.py file. Read the last link for more clarity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM