Configuring Your Flask App
Tags: flask, python, software
Published: 2020-05-18 23:46:54

---
title: Configuring Your Flask App
published: true
date: 2020-05-18 18:46:00 EDT
tags: Flask,Python,Software
series: Build Flask Python Apps
canonical_url: https://hackersandslackers.app/configure-flask-applications/
---

 ![Configuring Your Flask App](https://hackersandslackers-cdn.storage.googleapis.com/2020/05/flaskconfig-1.jpg)

The software industry has seen no shortage of new web frameworks over the years, but I certainly wouldn't be alone in saying that none. Discovering Flask is to rediscover instant gratification itself: all it takes is a mere few seconds for anybody to deploy a Python application, alive and well on a development server. I believe the cliche that the hardest part of anything is getting started, so it's no surprise that Flaks can get the imagination brewing.

Configuring apps isn't the sexiest topic to cover in the realm of hot new frameworks, which is precisely why _most_ newcomers skip over this aspect of Flask development _entirely_. This level of short-term procrastination seems harmless to the untrained eye. From my perspective, it's like watching a generation of grown professionals postponing their child's potty-training by one more day: both cases ultimately end with grown adults shitting their pants.

Every web app needs to be configured with things like database URIs and secret keys to enable critical functionality. Unlike Django, there's no monolithic **settings.py** file in Flask _(thank god)_. Instead, we can choose to configure Flask via several methods. Today we're covering two topics in Flask configuration: best practices in configuration structure and configuration settings themselves.

## What Not To Do: Inline Configuration

The fastest, dirtiest, and most dangerous method for configuring Flask is by directly setting config vars in source code using plain text. Amongst tutorials, it also seems to be the most common. Stop me if you've seen something like this before:

```python
from flask import Flask

app = Flask(__name__)
app.config['FLASK_ENV'] = 'development'
```
<figcaption>app.py</figcaption>

From the perspective of an author writing tutorials for strangers, it's convenient to pretend that setting config values this way is acceptable. Surely nobody who is clicking into _"How To Do This Sensational Thing in Flask"_ tutorials care to learn best software development practices, which encourages a lot of bad behavior and leaves many questions unanswered. For instance, why would we wait until _after_ we create the Flask **app** object to set something as important as FLASK\_ENV? Isn't the point of configuration to inform our app on how to function before it starts... _functioning_?

As the Flask docs promise, your app's config can be modified as we did above and can be modified/changed at any time. Let's set aside the horrendous readability this creates down the line. We'll even turn a blind eye to the unimaginable shit show we create by mutating configs _while_ our app is running. To make matters worse, it encourages committing sensitive values such as secret keys in the middle of your codebase:

```python
from flask import Flask

# Flask app creation
app = Flask(__name__)

# Ugly and confusing tangent of in-line config stuff
app.config['TESTING'] = True
app.config['DEBUG'] = True
app.config['FLASK_ENV'] = 'development'
app.config['SECRET_KEY'] = 'GDtfDCFYjD'
app.config['DEBUG'] = False  # actually I want debug to be off now

# Actual app logic 
@app.route('/')
def home():
    return render_template('/index.html')
```
<figcaption>app.py</figcaption>

We could update these settings in bulk instead, which is a slightly prettier way of executing the same horrible configuration pattern:

```python
...
# Ugly and confusing tangent of in-line config stuff
app.config.update(
    TESTING=True,
    DEBUG= True,
    FLASK_ENV='development'
    SECRET_KEY='GDtfDCFYjD',
    DEBUG=False,
)
...
```
<figcaption>app.py</figcaption>

Config variables belong in _config files_ for a reason. They're an organized set of instructions that are defined _before_ our app is live. Moreover, it's much easier to avoid compromising sensitive secrets such as AWS or database credentials when we manage these things in one place. Let's explore our better options.

## Configuration from a .py File

The simplest way to configure a Flask app is by setting configuration variables directly in a config file such as **config.py**.

```python
from flask import Flask

app = Flask(__name__)
app.config.from_pyfile('config.py')
```
<figcaption>app.py</figcaption>

This allows us to avoid the mess in the previous example by isolating our configuration to a file separate from our app logic:

```python
TESTING = True
DEBUG = True
FLASK_ENV = 'development'
SECRET_KEY = 'GDtfDCFYjD'
```
<figcaption>config.py</figcaption>

Flask immediately recognizes these variables as being Flask-specific and implements them without any further effort. This is a big win in terms of organization, but we still haven't solved the issue of exposing stuff like **SECRET\_KEY** in a file that might make its way onto a public Github repo by accident someday. A standard solution is to pull these values from **environment variables.**

An "environment variable" is a value stored in the system memory of the device running your app. Environment variables can be temporarily created via the terminal like so:

```shell
$ export SECRET_KEY='GDtfDCFYjD'
```
<figcaption>Setting and environment variable.</figcaption>

`SECRET_KEY` will exist as long as you keep your terminal open.  You can retrieve variables like these on your local machine like so:

```shell
$ echo $SECRET_KEY
>> 'GDtfDCFYjD'
```
<figcaption>Setting and environment variable.</figcaption>

It would be annoying to set these variables every time we open our terminal, so we can set environment variables in a local file called **.env** instead and grab those variables using a Python library like [python-dotenv](https://pypi.org/project/python-dotenv/):

```python
"""Flask config."""
from os import environ, path
from dotenv import load_dotenv

basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))

TESTING = True
DEBUG = True
FLASK_ENV = 'development'
SECRET_KEY = environ.get('SECRET_KEY')
```
<figcaption>config.py</figcaption>

Our app is now both significantly cleaner and _more secure_ as long as sensitive values live in **.env** :

```
SECRET_KEY='GDtfDCFYjD'
```
<figcaption>.env</figcaption>
## Configuring Flask From Class Objects

We can take our configuration a step further by setting different configuration values depending on which environment we're running in (production, development, etc). We accomplish this by splitting configurations into Python classes in **config.py** :

```python
from flask import Flask

app = Flask(__name__)
app.config.from_object('config.Config')
```
<figcaption>app.py</figcaption>

Here's what **config.py** looks like now:

```python
"""Flask config."""
from os import environ, path
from dotenv import load_dotenv

basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))


class Config:
    """Set Flask config variables."""

    FLASK_ENV = 'development'
    TESTING = True
    SECRET_KEY = environ.get('SECRET_KEY')
    STATIC_FOLDER = 'static'
    TEMPLATES_FOLDER = 'templates'
    
    # Database
    SQLALCHEMY_DATABASE_URI = environ.get('SQLALCHEMY_DATABASE_URI')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    
    # AWS Secrets
    AWS_SECRET_KEY = environ.get('AWS_SECRET_KEY')
    AWS_KEY_ID = environ.get('AWS_KEY_ID')
```
<figcaption>config.py</figcaption>

Let's spice things up:

```python
"""Flask config."""
from os import environ, path
from dotenv import load_dotenv

basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))


class Config:
    """Base config."""
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SESSION_COOKIE_NAME = os.environ.get('SESSION_COOKIE_NAME')
    STATIC_FOLDER = 'static'
    TEMPLATES_FOLDER = 'templates'
    
    
class ProdConfig(Config):
    FLASK_ENV = 'production'
    DEBUG = False
    TESTING = False
    DATABASE_URI = os.environ.get('PROD_DATABASE_URI')


class DevConfig(Config):
    FLASK_ENV = 'development'
    DEBUG = True
    TESTING = True
    DATABASE_URI = os.environ.get('DEV_DATABASE_URI')
```
<figcaption>config.py</figcaption>

Instead of a one-size-fits-all config, we can now swap configurations based on which environment Flask is running in. **ProdConfig** and **DevConfig** contain values specific to _production_ and _development_ respectively. Both of these classes extend a base class **Config** which contains values intended to be shared by both. Swapping between configs is now this easy:

```python
# Using a production configuration
app.config.from_object('config.ProdConfig')

# Using a development configuration
app.config.from_object('config.DevConfig')
```
<figcaption>app.py</figcaption>
## Configuration Variables

Now, what do all these variables mean anyway? We've polished our app to have a beautiful config setup, but what do **SECRET\_KEY** or **SESSION\_COOKIE\_NAME** even mean anyway? We won't go down the rabbit hole of exploring _every_ obscure setting in Flask, but I'll give you a crash course on the good parts:

- **FLASK\_ENV**: The environment the app is running in, such as _development_ or _production_. Setting the environment to _development_ mode will automatically trigger other variables, such as setting **DEBUG** to `True`. Flask plugins similarly behave differently when this is true.
- **DEBUG**: Extremely useful when developing! _DEBUG_ mode triggers several things. Exceptions thrown by the app will print to the console automatically, app crashes will result in a helpful error screen, and your app will auto-reload when changes are detected.
- **TESTING**: This mode ensures exceptions are propagated rather than handled by the app's error handlers, which is useful when running automated tests.
- **SECRET\_KEY**: Flask "secret keys" are random strings used to encrypt sensitive user data, such as passwords. Encrypting data in Flask depends on the randomness of this string, which means _decrypting_ the same data is as simple as getting a hold of this string's value. Guard your secret key with your life; ideally, even you shouldn't know the value of this variable.
- **SERVER\_NAME**: If you intend your app to be reachable on a custom domain, we specify the app's domain name here.

### Static Asset Configuration

Configuration for serving static assets via the Flask-Assets library:

- **ASSETS\_DEBUG** : Enables/disables a debugger specifically for static assets.
- **COMPRESSOR\_DEBUG**: Enables/disables a debugger for asset compressors, such as LESS or SASS.
- **FLASK\_ASSETS\_USE\_S3**: Boolean value specifying whether or not assets should be served from S3.
- **FLASK\_ASSETS\_USE\_CDN**: Boolean value specifying whether or not assets should be served from a CDN.

### Flask-SQLAlchemy

Flask-SQLAlchemy is pretty much _the_ choice for handling database connections in Flask:

- **SQLALCHEMY\_DATABASE\_URI**: The connection string of your app's database.
- **SQLALCHEMY\_ECHO**: Prints database-related actions to console for debugging purposes.
- **SQLALCHEMY\_ENGINE\_OPTIONS**: Additional options to be passed to the SQLAlchemy engine, which holds your app's database connection.

### Flask-Session

Flask-Session is excellent for apps that store user information per session. While Flask supports storing data in cookies by default, Flask-Session builds on this by adding functionality and additional methods for where to store session information:

- **SESSION\_TYPE**: Session information can be handled via Redis, Memcached, filesystem, MongoDB, or SQLAlchemy.
- **SESSION\_PERMANENT**: A True/False value, which states whether or not user sessions should last forever.
- **SESSION\_KEY\_PREFIX**: Modifies the key names in session key/value pairs to always have a certain prefix.
- **SESSION\_REDIS**: URI of a Redis instance to store session information.
- **SESSION\_MEMCACHED**: URI of a Memcached client to store session information.
- **SESSION\_MONGODB**: URI of a MongoDB database to store session information.
- **SESSION\_SQLALCHEMY**: URI of a database to store session information.

## Last Thoughts

We've spent way more time on the topic of configuring Flask than anybody could enjoy. Instead of making a dry technical post longer, why not learn from example by cruising our collection of countless Flask tutorials? We've pumped out at least a dozen Flask projects - feel free to find an example Flask project that meets your needs and take it from there.

https://github.com/hackersandslackers