While configuring Flask Boilerplate App, I had a problem for configuring configuration.
So i found some ways to do configuration for flask:
I want to use a
config.py for saving all the conf variables.
Method 1: Using Instance folder
Sometimes you’ll need to define configuration variables that contain sensitive information. We’ll want to separate these variables from those in config.py and keep them out of the repository. You may be hiding secrets like database passwords and API keys, or defining variables specific to a given machine. To make this easy, Flask gives us a feature called instance folders. The instance folder is a sub-directory of the repository root and contains a configuration file specifically for this instance of the application. We don’t want to commit it into version control.
To load configuration variables from an instance folder, we use
app.config.from_pyfile(). If we set
instance_relative_config=True when we create our app with the Flask() call,
app.config.from_pyfile() will load the specified file from the instance/ directory.
app = Flask(__name__, instance_relative_config=True) app.config.from_pyfile('config.py')
Method 2: The simple case
A simple application may not need any of these complicated features. You may just need to put config.py in the root of your repository and load it in
The config.py file should contain one variable assignment per line. When your app is initialized, the variables in config.py are used to configure Flask and its extensions are accessible via the app.config dictionary – e.g.
app.config["DEBUG"] = True.
app.py or app/init.py
from flask import Flask app = Flask(__name__) app.config.from_object('config')
#Now we can access the configuration variables via app.config[“VAR_NAME”].
Method 3: Configuration Based on Environment Variables
The instance folder shouldn’t be in version control. This means that you won’t be able to track changes to your instance configurations. That might not be a problem with one or two variables, but if you have finely tuned configurations for various environments (production, staging, development, etc.) you don’t want to risk losing that.
Flask gives us the ability to choose a configuration file on load based on the value of an environment variable. This means that we can have several configuration files in our repository and always load the right one. Once we have several configuration files, we can move them to their own config directory.
In this listing we have different configuration files
config/default.py config/development.py config/production.py config/staging.py
To decide which configuration file to load, we’ll call app.config.from_envvar()
app = Flask(__name__)
Load the file specified by the APP_CONFIG_FILE environment variable
Variables defined here will override those in the default configuration
The value of the environment variable should be the absolute path to a configuration file.
How we set this environment variable depends on the platform in which we’re running the app. If we’re running on a regular Linux server, we can set up a shell script that sets our environment variables and runs run.py.
$ export APP_CONFIG_FILE=/var/www/yourapp/config/production.py ` $ python run.py `