Composition of views and models

As we go big, we might want to structure our application in a modular manner. We will do this by restructuring our Hello World application.

How to do it…

  1. First, create a new folder in our application and move all our files inside this new folder.
  2. Then, create __init__.py in our folders, which are to be used as modules.
  3. After that, create a new file called run.py in the topmost folder. As the name implies, this file will be used to run the application.
  4. Finally, create separate folders to act as modules.

Refer to the following file structure for a better understanding:

flask_app/
    - run.py
    - my_app/
        – __init__.py
        - hello/
            - __init__.py
            - models.py
            - views.py

First, the flask_app/run.py file will look something like the following ...

Get Flask Framework Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.