Flask Series: Structure the Application
Wednesday, Apr 22, 2015 19:46 · 362 words · 2 minutes read
Flask Series
- Prepare the Environment
- Structure the Application
- Application Configuration
- Templating
- Model
- Testing
- Views and Web Forms
- Error Management
- Security
- Optimizations
- Healthcheck and Monitoring
- Internationalization
- Deployment
In the previous blog post of Flask Series I have described how to prepare your Flask environment, now it’s time to structure it.
Project Tree
Create your project structure using the following directory hierarchy:
run.py
▾ bin/
▾ bookshelf/
__init__.py
▾ admin/
controllers.py
__init__.py
▾ main/
controllers.py
__init__.py
▾ docs/
▾ tests/
Brief Description of the Application Structure
- bin – in this folder you could place your scripts, that will be executed on the command line;
- docs – in this folder you could place project related documentation files;
- tests – this folder contains your unit tests, I will prepare a blog post on this topic in the Flask Series;
- [app_name] – contains the application itself, in this demo application I will name it a bookshelf;
- run.py – this file is used to run the Flask application, where the contents of the file is:
from bookshelf import app | |
if __name__ == '__main__': | |
app.run() |
The Application Folder
The application is configured to be a modular, based on the blueprint concept in Flask. The Flask blueprints allows developers to simplify applications, better structure them and provide a central means for Flask extensions to register operations on applications. The blueprint is similar to the Flask application object, rather it is a mean to construct or extend an application.
main and admin are the Bookshelf application modules, make sure you have __init__.py files under the admin and main folders, it initializes them as python packages. Application module controllers are using the blueprint concept and are initialized as follows:
main/controllers.py
from flask import Blueprint | |
main = Blueprint('main', __name__) | |
@main.route('/') | |
def index(): | |
return "Main" |
admin/controllers.py
from flask import Blueprint | |
admin = Blueprint('admin', __name__) | |
@admin.route('/') | |
def index(): | |
return "Admin" |
Now that you have the admin and main application modules, you need to create the application object and register them.
bookshelf/init.py
from flask import Flask | |
from bookshelf.main.controllers import main | |
from bookshelf.admin.controllers import admin | |
app = Flask(__name__) | |
app.register_blueprint(main, url_prefix='/') | |
app.register_blueprint(admin, url_prefix='/admin') |
Test the Application
Activate your flask virtual environment created and configured in the the previous blog post and run the application:
workon flask
python run.py
Now you could test your Flask application:
- http://localhost:5000/ - main application module
- http://localhost:5000/admin – admin application module
In the next blog post I will describe how to configure your Flask project.
The complete demo application, described in this blog post, can be found here.