Flask is a simple web framework written in Python.
In this article, we’ll look at how to develop simple Python web apps with Flask.
Update Configuration
We can set the configuration by setting some properties of the Flask object.
For example, we can write:
from flask import Flask
app = Flask(__name__)
app.testing = True
@app.route('/')
def hello_world():
return 'Hello, World!'
We set the app.testing
property to change the TESTING
config.
Also, we can set more than one config settings:
from flask import Flask
app = Flask(__name__)
app.config.update(
TESTING=True,
SECRET_KEY=b'secret'
)
@app.route('/')
def hello_world():
return 'Hello, World!'
The app.config.update
method updates multiple configs.
Environment and Debug Features
We can set the FLASK_ENV
environment variable to change the environment that the code runs in.
For example, we run:
$ export FLASK_ENV=development
$ flask run
in Linux and:
$ set FLASK_ENV=development
$ flask run
in Windows to run the code in development mode.
Configuring from Files
We can call app.config.from_objecr
to load the config from a module.
For example, we can write:
app.py
from flask import Flask
app = Flask(__name__)
app.config.from_object('settings')
@app.route('/')
def hello_world():
return 'Hello, World!'
settings.py
DEBUG = False
SECRET_KEY = b'_5#y2L"F4Q8znxec]/'
We read the config from settings.py
with the app.config.from_object
method.
Also, we can use the app.config.from_envvar
method to read the settings from the path to the file that we set from the environment variable.
For example, we can write:
app.py
from flask import Flask
app = Flask(__name__)
app.config.from_envvar('APP_SETTINGS')
@app.route('/')
def hello_world():
print(app.config)
return 'Hello, World!'
settings.cfg
DEBUG = False
SECRET_KEY = b'_5#y2L"F4Q8znxec]/'
Then in Windows, we run:
> set APP_SETTINGS=./settings.cfg
> python -m flask run
to run the app and read the settings from settings.cfg
.
On Linux, we run:
$ export APP_SETTINGS=./settings.cfg
$ python -m flask run
Configuring from Environment Variables
We can read config from environment variables with the os.environ.get
method.
For example, we can write:
from flask import Flask
import os
app = Flask(__name__)
SECRET_KEY = os.environ.get("SECRET_KEY")
print(SECRET_KEY)
@app.route('/')
def hello_world():
return 'Hello, World!'
to read the SECRET_KEY
environment variable from the OS’s environment variables.
Then we can set the environment variable and run it by running:
> set SECRET_KEY='5f352379324c22463451387a0aec5d2f'
> python -m flask run
on Windows.
On Linux, we run:
$ set SECRET_KEY='5f352379324c22463451387a0aec5d2f'
$ python -m flask run
Development / Production
We can use class inheritance to create a shared config class between different environments.
Then we can create child config classes for different environments.
For example, we can write:
app.py
from flask import Flask
import os
app = Flask(__name__)
app.config.from_object('settings.ProductionConfig')
print(app.config)
@app.route('/')
def hello_world():
return 'Hello, World!'
settings.py
class Config(object):
DEBUG = False
TESTING = False
DATABASE_URI = 'sqlite:///:memory:'
class ProductionConfig(Config):
DATABASE_URI = 'mysql://user@localhost/foo'
class DevelopmentConfig(Config):
DEBUG = True
class TestingConfig(Config):
TESTING = True
We call app.config.from_object
with the 'settings.ProductionConfig'
string to get the settings from the ProductionConfig
subclass.
Conclusion
There are several ways to read configs for our app with Flask.