Skip to Content
Flask Web Development, 2nd Edition
book

Flask Web Development, 2nd Edition

by Miguel Grinberg
March 2018
Intermediate to advanced
312 pages
7h 22m
English
O'Reilly Media, Inc.
Book available
Content preview from Flask Web Development, 2nd Edition

Chapter 7. Large Application Structure

Although having small web applications stored in a single script file can be very convenient, this approach does not scale well. As the application grows in complexity, working with a single large source file becomes problematic.

Unlike most other web frameworks, Flask does not impose a specific organization for large projects; the way to structure the application is left entirely to the developer. In this chapter, a possible way to organize a large application in packages and modules is presented. This structure will be used in the remaining examples of the book.

Project Structure

Example 7-1 shows the basic layout for a Flask application.

Example 7-1. Basic multiple-file Flask application structure
|-flasky
  |-app/
    |-templates/
    |-static/
    |-main/
      |-__init__.py
      |-errors.py
      |-forms.py
      |-views.py
    |-__init__.py
    |-email.py
    |-models.py
  |-migrations/
  |-tests/
    |-__init__.py
    |-test*.py
  |-venv/
  |-requirements.txt
  |-config.py
  |-flasky.py

This structure has four top-level folders:

  • The Flask application lives inside a package generically named app.

  • The migrations folder contains the database migration scripts, as before.

  • Unit tests are written in a tests package.

  • The venv folder contains the Python virtual environment, as before.

There are also a few new files:

  • requirements.txt lists the package dependencies so that it is easy to regenerate an identical virtual environment on a different computer.

  • config.py stores the configuration settings. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Flask Web Development - Second Edition

Mastering Flask Web Development - Second Edition

Daniel Gaspar, Jack Stouffer

Publisher Resources

ISBN: 9781491991725Errata Page