71
votes

I have just created a flask application and so far I have a router for my "Hello world!" template.

I would like to add a little (a lot) more functionality, but I wonder how I should structure the app directory.

What's the most common way of structuring a Flask app? For instance, should I create a routes.py for all my routes? Where does the SQLAlchemy stuff go? Should models be in models.py?

10

10 Answers

42
votes

You should check out the Larger Applications page in the Patterns section of the Flask docs: http://flask.pocoo.org/docs/patterns/packages/. It seems to be the model that most people follow when their application calls for a package instead of a module.

I believe views.py is what you are calling routes.py. After that, models would go in models.py, forms would go in forms.py, etc.

25
votes

An example of a FlaskApp directory:

    /yourapp  
        /run.py  
        /config.py  
        /app  
            /__init__.py
            /views.py  
            /models.py  
            /static/  
                /main.css
            /templates/  
                /base.html  
        /requirements.txt  
        /yourappenv

run.py - contains the actual python code that will import the app and start the development server.
config.py - stores configurations for your app.
__init__.py - initializes your application creating a Flask app instance.
views.py - this is where routes are defined.
models.py - this is where you define models for your application.
static - contains static files i.e. CSS, Javascript, images
templates - this is where you store your html templates i.e. index.html, layout.html
requirements.txt - this is where you store your package dependancies, you can use pip
yourappenv - your virtual environment for development

19
votes

I would say if you split the application use divisional rather than functional structure. I advocate this because you are more likely to work on 1 of these divisional components at any one time.

This type of structure lends itself well on marketplace or SaaS apps where different user groups use a different type of views. API only flask app I might use functional splitting.

Here are examples from Flask Blueprints. Blueprints are essentially documented advice how to split Flask application for more manageable pieces. More on this at : http://exploreflask.com/en/latest/blueprints.html

Here is an example of divisional splitting. See how each feature is grouped together.

yourapp/
    __init__.py
    admin/
        __init__.py
        views.py
        static/
        templates/
    home/
        __init__.py
        views.py
        static/
        templates/
    control_panel/
        __init__.py
        views.py
        static/
        templates/
    models.py

Here is the functional example >

yourapp/
    __init__.py
    static/
    templates/
        home/
        control_panel/
        admin/
    views/
        __init__.py
        home.py
        control_panel.py
        admin.py
    models.py
11
votes

I think flask is micro framework and now you must decide how create files and folders.

i use this way :

this is near Django structure

i suggest you see some project to give you what you want

6
votes

Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:

   |__movies 
     |__run.py 
     |__app     
        ├── templates
        │   └── index.html
        │   └── signup.html
        └── __init__.py
        └── routes.py

Here 'movies' is the name given for the main application. It contains 'run.py' and a folder called 'app'. 'app' folder contains all necessary flask files such as 'templates' folder, '__init __.py', and 'routes.py'.

Contents of:

run.py:

from app import app

__init.py__:

from flask import Flask

app = Flask(__name__)

from app import routes


app.run(debug=True)

routes.py:

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"
5
votes

You could get inspired by the cookiecutter templates here to jumpstart your app development

4
votes

Beauty of flask lies in its flexibility. You can build django like project-structure easily. Django popularized abstraction of features in apps and making them reusable but it can be a overkill for many projects.

But with flask you can go either way. Write reusable apps or write simple apps. Check these cookiecutter skeletons -

  1. minimal skeleton

    myproject ├── config.py ├── instance │   └── config.py ├── myproject │   ├── commands.py │   ├── controllers.py │   ├── extensions.py │   ├── forms.py │   ├── __init__.py │   ├── models.py │   ├── routes.py │   └── ui │   ├── static │   │   ├── css │   │   │   └── styles.css │   │   └── js │   │   └── custom.js │   └── templates │   └── index.html ├── README.md ├── requirements.txt └── wsgi.py

  2. django like skeleton

    myproject ├── config.py ├── development.py ├── instance │   └── config.py ├── myproject │   ├── auth │   │   ├── controllers.py │   │   ├── forms.py │   │   ├── __init__.py │   │   ├── models.py │   │   └── routes.py │   ├── helpers │   │   ├── controllers.py │   │   ├── __init__.py │   │   └── models.py │   ├── __init__.py │   └── ui │   └── templates │   ├── 404.html │   ├── 500.html │   └── base.html ├── README.md ├── requirements.txt ├── tests │   ├── auth │   │   ├── __init__.py │   │   └── test_controllers.py │   └── __init__.py └── wsgi.py

This is an excellent article on this.

2
votes

Here is the basic file structure for flask I use regularly.

yourapp/
static/
    js
    css
    img

templates/
    home.html
    index.html

app.py

static folder contains all the static files of the website. The templates folder contains all the HTML pages. and app.py contains your python views.

0
votes

In flask we can maintain the mvc structure like as separate all thinks for example 1 Templets folder contains all html files 2 Static folder contains all css and boostrap related files 3 User defined db folder for db connection and crud operations 4 User defined Model folder for accessing/ binding the data from Templets/front- end to db/back-end connectivity and after the main activity file
for reference flask file structure link as follow https://flask.palletsprojects.com/en/1.1.x/tutorial/layout/