Skip to main content

Flask-SQLAlchemy-Project-Template

Ready to use template to understand Flask SQLAlchemy basic implementation, with examples. With Flask Blueprints and application factory. Avoids circular imports.

Minimalistic code and folder structure in the package.

Introduction

Many tutorials on this topic are either old and non-working or complex. SQLAlchemy is an Object Relational Mapper (ORM). You can work with Python class attributes instead of raw SQL statements.

Example

See the package in multi_server action as one of the PyPi eisenmp_examples

How to create the SQLite Database?

Flask-SQLAlchemy can create the whole database file, tables and schema from your Python class definition. The SQLALCHEMY_DATABASE_URI is used to create the database in the correct location with the given name.

models.py :

class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)
    email = db.Column(db.String, unique=True, nullable=False)
    profile = db.Column(db.String, unique=False, nullable=False)

__ init __.py :

this_dir = path.abspath(path.join(path.dirname("__file__")))
db_path = path.join(this_dir, 'database.db')

def create_app():
    ...
    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + db_path
    db.init_app(app)  
    ...

def setup_database(flask_app):
    with flask_app.app_context():
    # alchemy creates the db from SQLALCHEMY_DATABASE_URI and models.py classes
    db.create_all()

That's it!

Flask documentation says, that db.init_app(app) MUST be there, if application factory style is used.

important Imports

database.py :

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

models.py :

from .database import db

__ init __.py :

from .database import db

def create_app():
    # flask initializes Alchemy with the application
    db.init_app(app)

/routes_users/routes_support.py :

     from Flask_SQLAlchemy_Project_Template.database import db
     from Flask_SQLAlchemy_Project_Template.models import Users

trip wires

Install flask-sqlalchemy and python interpreter into venv via pycharm file,settings,project,interpreter if it will not take place via the IDE window.

Circular import dilemma. Flask-SQLAlchemy is a candidate for hours of research and frustration.

tips

Flask-sqlalchemy is dependent of the current context in the app. This will not show up in one file mini setups. For bigger Flask apps, with multiple modules, it is obviously necessary to work with route(s) and blueprints. This package also uses flask application factory. This is flask as a function ( def create_app(): ).

run

$ pip3 install Flask_SQLAlchemy_Project_Template
$ python3 -m Flask_SQLAlchemy_Project_Template

location

$ pip3 show Flask_SQLAlchemy_Project_Template

GitHub package

Just for the sake of my art. Not really useful, since you had to open the image to get the directory structure.

sudo docker run -v $(pwd):/alchemy -it --network host ghcr.io/44xtc44/flask_sqlalchemy_project_template:latest

Connects your current dir with the docker image /alchemy dir. The app must write the database.

Cheers

Metadata

Release files for Flask-SQLAlchemy-Project-Template 1.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-SQLAlchemy-Project-Template 1.5.3
File Size Uploaded
Flask_SQLAlchemy_Project_Template-1.5.3.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-SQLAlchemy-Project-Template 1.5.3
File Interpreter ABI Platform
Flask_SQLAlchemy_Project_Template-1.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 27.4 kB

Release files / Flask_SQLAlchemy_Project_Template-1.5.3.tar.gz

Download URL Flask_SQLAlchemy_Project_Template-1.5.3.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f6bebcc9df4a17a5467b6cea4fb546de039e18715c8bc161ae07a61fa3d70af8
BLAKE2b-256 checksum
How to use checksums
bf22c3346ba8844bebe55d123cb05e7f9abf659537a1081e709ee802e0e3ba5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.3

Release files / Flask_SQLAlchemy_Project_Template-1.5.3-py3-none-any.whl

Download URL Flask_SQLAlchemy_Project_Template-1.5.3-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91700295f71edace13cb09338db2cc9036c00c8d068d46d840fa4d5b08dd75eb
BLAKE2b-256 checksum
How to use checksums
26ea9314fefb98e8f0d148c8dbfc31a73b4f4fbb9112e56143661d85534ebd8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.3

Release history Release notifications | RSS feed

This release

1.5.3 This release

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page