How to organize a relatively large Flask application?

Question:

I’m building my first Flask app and I can’t figure out a good, clean Pythonic way of organizing my application.
I don’t want to have everything in a single .py file as in their example.
I would like to have each part of my app in a separate module.
What would be a good way to organize things?

Asked By: daniels

||

Answers:

Flask 0.7 implements Blueprints. They are great for using the route decorator without importing the main application object.

Answered By: Alex Morega

I have created a Flask boilerplate project called “Fbone“, please feel free to check it out and fork 🙂

Fbone (Flask bone) is a Flask (Python microframework) template/bootstrap/boilerplate application.

Overview

  • Well designed for big project using blueprint.
  • Integrate with hottest frontend framework: jQuery / html5boilerplate / bootstrap.
  • Backed by the famous SQLalchemy.
  • Implement tricky “remember me” by flask-login.
  • Handle web forms by flask-wtform.
  • Unit testing with flask-testing and nose.
  • Easily deploy via fabric and mod_wsgi (example included).
  • i18n by flask-babel

btw, I just found this wiki on building a large project with Flask useful, pls check it!

Answered By: imwilsonxu

I’m working on a (by my standards) big Flask project (5000 lines of Python code and it’s only half-finished). The customer wants the project to be modular, so I took this apporach:

My folder structure looks like this:

├── __init__.py
├── modules.yml
├── config
├── controllers
│   └── ...
├── lib: Common functions I use often
│   └── ...
├── models
│   └── ...
├── static: All static files
│   ├── css
│   ├── img
│   └── js
└── templates: Jinja2 templates
    └── ...

In modules.yml I define my modules including name and URL. This way the customer is able to enable/disable modules without touching a single Python file. In addition, I generate the menus based on the modules list. By convention every module has it its own Python-module in controllers/ that will load its model from models/. Every controller defines a Blueprint stored as the controller’s name. E.g. for a user module, I have in controllers/user.py:

# Module name is 'user', thus save Blueprint as 'user' variable
user = Blueprint('user', __name__)

@user.route('/user/')
def index():
    pass

This way, I can read the modules.yml in my __init__.py and load and register all enabled modules dynamically:

# Import modules
for module in modules:

    # Get module name from 'url' setting, exculde leading slash
    modname = module['url'][1:]

    try:
        # from project.controllers.<modname> import <modname>
        mod = __import__(
            'project.controllers.' + modname, None, None, modname
        )
    except Exception as e:
        # Log exceptions here
        # [...]

    mod = getattr(mod, modname)  # Get blueprint from module
    app.register_blueprint(mod, url_prefix=module['url'])

I hope, this can be some inspiration for you 🙂

Answered By: msiemens

Make sure to read Matt Wright’s wonderful post on the subject.

The post features:

  1. A description of a structure for large flask projects

  2. An example application on Github

  3. A description of best design practices in general when it comes to large web apps, like the MVC pattern, App factories, Services and Data Migration to name a few (most interesting feature IMHO).

Answered By: edsioufi

I have created a Flask app yapper from scratch and integrated it with gulp for both frontend and backend development.
It is a simple blog engine but can be easily modified for developing according to requirements. It is well structured using Blueprints.

Checkout the project page yapper

Answered By: brijeshb42

I worked on a social network built on top of Flask. The special thing about my project was that the server is purely serving API endpoints and the frontend is a one-page Backbone app. The Flask structure I took is the following:


├── app
│ ├── api
│ │ ├── auth.py
│ │ └── ...
│ ├── app.py
│ ├── common
│ │ ├── constants.py
│ │ ├── helpers.py
│ │ ├── response.py
│ │ └── ...
│ ├── config.py
│ ├── extensions.py
│ ├── frontend
│ │ └── controllers.py
│ ├── static
│ │ └── ...
│ ├── templates
│ │ ├── app.html
│ │ └── ...
│ └── users
│ ├── UserConstants.py
│ ├── UserForms.py
│ ├── UserHelpers.py
│ ├── UserModels.py
│ └── __init__.py
├── alembic
| ├── version
│ └── ...
├── tests
│ └── ...

You can read the more in-depth post I wrote on the topic here. I found it to be much more intuitive to separate different functional areas to its own folder.

I worked on the code a while ago and open sourced it completely! You can check it out on github.

Answered By: spchuang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.