Flask Series: Structure the Application

Wednesday, Apr 22, 2015 19:46 · 361 words · 2 minutes read python flask flask series

Flask Series

  1. Prepare the Environment
  2. Structure the Application
  3. Application Configuration
  4. Templating
  5. Model
  6. Testing
  7. Views and Web Forms
  8. Error Management
  9. Security
  10. Optimizations
  11. Healthcheck and Monitoring
  12. Internationalization
  13. 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:

▾ bin/
▾ bookshelf/
    ▾ admin/
    ▾ main/
▾ 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:

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:



Now that you have the admin and main application modules, you need to create the application object and register them.


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.
comments powered by Disqus