Skillversitas

Project Structure

The structure is the folder the 1: the app/ package (the blueprint, the model, the no the 1), the extensions.py (the db, the no the import the app), and the config.py (the 3, the env, the no the the). The __init__.py is the factory (the create_app, the no the global), the no is the cycle (the import, the 1, the no the the), and the requirements is the pin (the version, the no the 1). This chapter covers the app/ package (the blueprint, the model), the extensions.py (the no the import), the config.py (the 3, the no the the), and the no-cycle (the 1, the no the the).

The layout and the extensions.py

# the LAYOUT (the 1, the no
#   the 1, the no the the):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#
#   project/
#   |  requirements.txt
#   |  .env
#   |  .gitignore
#   |  README.md
#   |
#   |-- app/
#   |  |  __init__.py      # the
#   |  |                    #   factory
#   |  |  extensions.py    # the
#   |  |                    #   db, the
#   |  |                    #   login
#   |  |  config.py        # the
#   |  |                    #   3
#   |  |
#   |  |-- models.py       # the
#   |  |                   #   User,
#   |  |                   #   the Post
#   |  |-- views.py        # the
#   |  |                   #   the main
#   |  |-- api/
#   |  |  |  __init__.py
#   |  |  |  routes.py
#   |  |-- templates/
#   |  |  |  base.html
#   |  |  |  index.html
#   |  |-- static/
#   |  |  |  css/style.css
#   |  |
#   |-- tests/
#   |  |  __init__.py
#   |  |  test_feed.py
#   |
#   |-- run.py             # the
#                          #   the entry

# the EXTENSIONS (the 1, the
#   no import, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   app/extensions.py:
from flask_sqlalchemy import \
    SQLAlchemy
from flask_login import \
    LoginManager

db = SQLAlchemy()
login = LoginManager()
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The layout is the 1: the app/ package is the 1 (the blueprint, the model, the no the 1, the no the the), and the templates plus the static is the 2 (the 1, the no the the) - the extensions.py is the 1: the db is the no the import the app (the 1, the no the the), and the login is the no (the 1, the no the 1, the no the the) - the models.py is the 1 (the User, the Post, the no the 1), and the views.py is the no (the route, the 1, the no the the) - the config is the next: the 3 the no the the.

The config, the factory, and the no-cycle

# the CONFIG (the 3, the env,
#   the no the the, the no
#   the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   app/config.py:
import os

class Base:
    SECRET_KEY = os.environ.get(
        "SECRET_KEY")
    SQLALCHEMY_TRACK_MODIFICATIONS = \
        False

class Development(Base):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = \
        "sqlite:///dev.db"

class Production(Base):
    SQLALCHEMY_DATABASE_URI = \
        os.environ.get(
            "DATABASE_URI")

config = {
    "development": Development,
    "production": Production,
}
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the FACTORY (the __init__,
#   the no global, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   app/__init__.py:
def create_app(name=
        "development"):
    app = Flask(__name__)
    app.config.from_object(
        config[name])
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    db.init_app(app)
    login.init_app(app)
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
    return app
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The config is the 3: the Base is the 1 (the SECRET_KEY, the env, the no the the), the Development is the DEBUG (the SQLite, the no the 1), and the Production is the Postgres (the env, the no the the) - the config dict is the no (the name, the 1, the no the 1). The factory is the __init__: the create_app is the 1 (the no the global, the no the 1), the from_object is the config (the 1, the no the the), and the init_app is the 2 (the db, the login, the no the 1) - the no the cycle is the 1 (the import, the no the 1, the no the the) - the rule is the one: the app/ the 1, the extensions the no the import, the config the 3, and the factory the no the global - the Flask tutorial's end: the best the practice the 10 the no the 1.

The Working Rules

  • The layout is the 1, the no the 1. The app/ package the 1 (the blueprint, the model, the no the 1, the no the the), and the templates plus the static the 2 (the 1, the no the the) - the no the 2 (the no the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The extensions.py is the 1, the no the import. The db the no the app (the 1, the no the the), and the login the no (the 1, the no the 1, the no the the) - the models.py the 1 (the User, the Post, the no the 1), and the views.py the no (the route, the 1, the no the the).
  • The config is the 3, the env the no. The Base the 1 (the SECRET_KEY, the env, the no the the), and the Development the DEBUG (the SQLite, the no the 1) - the Production the Postgres (the env, the no the the), and the config dict the no (the name, the 1, the no the 1).
  • The factory is the __init__, the no the global. The create_app the 1 (the no the 1, the no the the), and the from_object the config (the 1, the no the the) - the init_app the 2 (the db, the login, the no the 1), and the no the cycle (the import, the no the 1, the no the the).

Summary

The structure is the folder the 1: the app/ package (the blueprint, the model, the templates, the static, the no the 1), the extensions.py (the db, the login, the no the import, the no the 1), the config.py (the 3, the env, the no the the, the config dict), and the __init__.py (the create_app, the no the global, the init_app, the no the cycle). The no-cycle is the 1 (the import, the no the 1, the no the the) - the rule is the one: the app/ the 1, the extensions the no the import, the config the 3, and the factory the no the global. The next chapter's practice is the 10: the factory, the config, the N+1, and the security (the 4, the no the the, the no the 1).