An Example Flask Project Structure For Google Cloud App Engine

An Example Flask Project Structure For Google Cloud App Engine

The Python Flask web framework is flexible. Unlike Django, it allows the developer to structure their projects in ways they deem fit. This can both be a curse and a blessing, because this can make moving between projects a bit tedious.

In this article, I suggest a Flask project structure that I find useful for Google Cloud App Engine projects.

Project Structure

Here’s the project structure with explanation below:

└── flask-project
    ├── app
    │   ├── app.py
    │   ├── blueprints
    │   │   └── user
    │   │       ├── forms.py
    │   │       ├── __init__.py
    │   │       ├── models.py
    │   │       ├── templates
    │   │       │   └── user
    │   │       └── views.py
    │   ├── extensions
    │   │   └── extensions.py
    │   ├── static
    │   │   ├── css
    │   │   ├── fonts
    │   │   ├── images
    │   │   └── js
    │   ├── templates
    │   │   ├── layouts
    │   │   └── macros
    │   └── tests
    ├── app.yaml
    ├── config
    │   └── settings.py
    ├── .env
    ├── .gcloudignore
    ├── .gitignore
    ├── index.yaml
    ├── instance
    │   └── settings.py_prod
    ├── README.md
    ├── requirements
    │   ├── common.txt
    │   ├── development.txt
    │   └── production.txt
    └── requirements.txt

flask-project: This is the project root directory. It will contain the actual Flask app together with the necessary App Engine yaml files. .gitignore, .gcloudignore and .env files will all be here.

app.yaml: App Engine configuration file.

index.yaml: Dastastore index configuration file.

README.md: Project readme.

requirements.txt: The file listing project package dependencies. This is used by App Engine to install dependencies.

app: The Flask application directory. Holds, static, templates, extensions, tests and blueprints directories.

config: The directory holding the app configurations.

instance: Directory holding production sensitive configs (e.g. encryption keys). Should not be in git the repo.

requirements: Directory holding the files containing list of project packages/dependencies.

common.txt: A list of common packages for both development and production environments.

development.txt: List of development specific packages. This file imports common.txt using -r common.txt.

production.txt: List of production specific packages. This file imports common.txt using -r common.txt. The top-level requirements.txt imports this using -r requirements/production.txt.

settings.py_prod: Production sensitive config. Uses a non-python extension so that Flask does not read/find it during development.

settings.py: Appication configs go here.

app.py: Application entry point holding the Flask object.

blueprints: Directory for Flask blueprints. This holds a blueprints with it’s templates, views.py, forms.py, models.py.



See below for sample code utilising the above project structure.

Code Snippets

The views.py file inside a blueprints directory will contain:

from flask import Blueprint

user = Blueprint('user',__name__,template_folder='templates')

# views go here...

The __init__.py file inside a blueprints directory will contain:

from app.blueprints.user.views import user

The settings.py file inside the settings directory will contain:

from flask_login import LoginManager

login_manager = LoginManager()

The app.py file inside the root Flask directory(ie app) will tie them all together:

from flask import Flask
from app.blueprints.user import user

from extensions.extensions import (
    login_manager
)

app = Flask(__name__,instance_relative_config=True)
app.config.from_object('config.settings')
app.config.from_pyfile('settings.py') # production config

#...

I hope you find the above information useful.
- SKS

Check out other guides

  1. Common Google Cloud CLI Commands
  2. How To Create Your Own Online Portfolio
  3. How To Make Your Resume Stand Out
  4. How To Create A Simple Website

Comments

comments powered by Disqus