Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize a relatively large Flask application?

Tags:

python

flask

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?

like image 243
daniels Avatar asked Feb 22 '12 13:02

daniels


People also ask

Why Flask is not used for production?

Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.


4 Answers

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!

like image 97
imwilsonxu Avatar answered Oct 10 '22 07:10

imwilsonxu


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

like image 40
Alex Morega Avatar answered Oct 10 '22 08:10

Alex Morega


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).

like image 19
edsioufi Avatar answered Oct 10 '22 08:10

edsioufi


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 :)

like image 12
msiemens Avatar answered Oct 10 '22 08:10

msiemens