Understanding Flask Application Structure and Configuration
Course Title: Mastering Flask Framework: Building Modern Web Applications Section Title: Introduction to Flask and Development Environment Topic: Understanding Flask’s application structure and configuration
Overview
In the previous topics, we introduced Flask and its ecosystem, and set up a development environment. In this topic, we'll delve into the application structure and configuration of Flask. Understanding these concepts is crucial for building robust and maintainable web applications with Flask.
Application Structure
A Flask application is typically structured into several folders and files, each with its own specific purpose. Here's a typical structure for a Flask application:
/your_app
/app
/templates
/static
__init__.py
routes.py
config.py
requirements.txt
run.py
Let's explore each folder and file in detail:
/app
: This folder contains the core application code./templates
: This folder stores HTML templates for your application./static
: This folder stores static files such as CSS, JavaScript, and image files.__init__.py
: This file initializes the Flask application.routes.py
: This file defines the URL routes for your application.
config.py
: This file stores configuration settings for your application.requirements.txt
: This file lists the dependencies required by your application.run.py
: This file runs the Flask development server.
Configuration
Flask provides several ways to configure your application. Here are some key configuration settings:
DEBUG
: This setting enables or disables debug mode.SECRET_KEY
: This setting sets the secret key for session management.TEMPLATE_FOLDER
: This setting sets the template folder.STATIC_FOLDER
: This setting sets the static folder.
You can configure these settings in several ways:
- Using a configuration file (
config.py
) - Using environment variables
- Using a configuration dictionary
Here's an example of how to configure Flask using a configuration file:
# config.py
import os
class Config:
DEBUG = True
SECRET_KEY = os.urandom(24)
TEMPLATE_FOLDER = 'templates'
STATIC_FOLDER = 'static'
class ProductionConfig(Config):
DEBUG = False
You can then load this configuration file in your __init__.py
file:
# __init__.py
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
Environment Variables
Flask also supports environment variables for configuration. You can set environment variables using the following syntax:
export FLASK_APP=run.py
export FLASK_DEBUG=1
You can then access these variables in your application code:
# __init__.py
import os
app = Flask(__name__)
app.config['DEBUG'] = os.environ.get('FLASK_DEBUG')
Practical Takeaways
Here are some practical takeaways from this topic:
- Use a consistent folder structure for your Flask application.
- Use a configuration file (
config.py
) to store configuration settings. - Use environment variables for sensitive configuration settings.
- Use a configuration dictionary for runtime configuration changes.
Additional Resources
For more information on Flask's application structure and configuration, please refer to the following resources:
- Flask Documentation: Application Structure
- Flask Documentation: Configuration
- 12-Factor App: Configuration
What's Next
In the next topic, we'll create our first Flask application. Please provide any questions or feedback you may have in the comments below.
Do you have any questions about Flask's application structure and configuration? What would you like to know more about?
Images

Comments