3
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?

orange1
  • 2,871
  • 3
  • 32
  • 58
  • Possible duplicate of [Common folder/file structure in Flask app](http://stackoverflow.com/questions/14415500/common-folder-file-structure-in-flask-app) – dirn Oct 13 '15 at 17:08

1 Answers1

2

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.

m1yag1
  • 819
  • 7
  • 11
  • I was going to write up an answer based off of Miguel Grinberg's book, so I would just like to +1 that recommendation. I come from a background with no web development experience prior to using Flask. I think it is an excellent book, and there is a chapter "Large application structure" devoted to this. In fact if you download https://github.com/miguelgrinberg/flasky and `git checkout 7a` you see an example of large application structure. – wgwz Oct 13 '15 at 15:53
  • Yes the book is fantastic. I used his book to learn Flask. I wrote some apps to complete some projects at work. 2 years later I've landed a job maintaining a pretty large flask app which is organized a little differently than others b/c of migrations and deployment strategies. The book though is a good starting point. – m1yag1 Oct 13 '15 at 15:57