Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring large Flask application? [closed]

Tags:

python

flask

run.py
static/
templates/
tests/
config.py
models.py

I create an instance of my Flask class within run.py, as well as define the views. I am planning on moving the views so they are separate from the run.py file, and just have the run.py file instantiate the Flask class. However, I also have quite a few non-view functions that are called by each of the view functions and implement the application logic.

What is a way to re-organize these view and logic functions to create a good application structure? Should I have a views/ folder with separate view files for each view, including the logic functions that correspond to each of the view functions? Should I group the logic functions together in another, separate folder?

like image 854
orange1 Avatar asked Dec 20 '25 21:12

orange1


1 Answers

There is some good guidance in the docs. Flask doesn't impose a lot of forced structure but your on the right track for getting in the habit of structuring your projects for your own sanity. This could be entirely left to you how you would like to do this. Probably a lot of opinions out there

What I tend to do is create a structure typical of most python projects. You may want to use an application factory pattern and blueprints.

myapp
/myapp
    /home
        views.py
    /templates
    /static
    factory.py
    core.py
    models.py
runserver.py
config.py

Mattupstate has a good blog article on this subject. There is also Fbone. Miguel Grinberg has a chapter in his Flask book dedicated to this too.

like image 57
m1yag1 Avatar answered Dec 23 '25 11:12

m1yag1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!