Flask Series: Structure the Application

In the previous blog post of Flask Series I have described how to prepare your Flask environment, now it’s time to structure it.

Create your project structure using the following directory hierarchy:

│   run.py
│   │   __init__.py
│   ├───admin
│   │       controllers.py
│   │       __init__.py
│   └───main
│           controllers.py
│           __init__.py

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. This file (__init__.py) 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

Open a browser and nagivate to:

http://localhost:5000/ – to test the main application module


http://localhost:5000/admin – for the admin one

The complete demo application, described in this blog post, can be found here.

In the next blog post I will describe how to configure your Flask project.