Skip to main content

Flask SQLAlchemy Bind

Minimalistic extension to add support for the SQLAlchemy ORM to your Flask app. Adds most essential functionality - the database table construction is still be figured out. If you're interested in my reasoning behind how I built this and how it all works, read more here.

Set Up:

Install using pip:

pip install flask-sqlalchemy-bind

Setting up Your Flask App

Here's an example of a full-fledged (but small) Flask app that uses Flask-SQLAlchemy-Bind. Please poke around.

Using an App Factory

I recommend using Flask-SQLAlchemy-Bind with an app factory like so:

    from flask import Flask
    from flask_sqlalchemy_bind import SQLAlchemy_bind

    # outside of app factory
    db = SQLAlchemy_bind()

    # must be defined after db = SQLAlchemy_bind() if in same module
    from sqlalchemy import Column, Integer, String
    class User(db.Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        username = Column(String, unique=True)
        password = Column(String, unique=True)

        def __init__(self, username=None, password=None):
            self.username = username
            self.password = password

    # app factory
    def create_app():
        app = Flask(__name__)
        app.config["DATABASE"] = "sqlite:///:memory:"
        # import your database tables if defined in a different module
        # for example if the User model above was in a different module:
        from your_application.database import User
        db.init_app(app)
        return app

Without an App Factory

The following is an example of a Flask app that does not use the app factory pattern:

    from flask import Flask
    from flask_sqlalchemy_bind import SQLAlchemy_bind

    app = Flask(__name__)
    app.config["DATABASE"] = "sqlite:///:memory:"
    db = SQLAlchemy_bind()

    # define your database tables
    from sqlalchemy import Column, Integer, String
    class User(db.Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        username = Column(String, unique=True)
        password = Column(String, unique=True)

        def __init__(self, username=None, password=None):
            self.username = username
            self.password = password

    # set up SQLAlchemy for your app
    # you must import or define your database tables before running this
    db.init_app(app)

    db.session.add(User(username="Hi", email="itsme@example.com"))
    db.session.commit()

    users = User.query.all()

Adding a CLI Command to Reset the Database

If you'd like to add a command line tool to reset the database add the following code to the your application:

import click
from flask.cli import with_appcontext

# create convenient click command for resetting database
@click.command('reset-db')
@with_appcontext
def reset_db_command():
    """Clear the existing data and create new tables."""
    db.empty_db()
    db.init_db()
    click.echo('Reset the database.')

Using the new CLI command:

flask reset-db

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-sqlalchemy-bind-1.0.4.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_sqlalchemy_bind-1.0.4-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file flask-sqlalchemy-bind-1.0.4.tar.gz.

File metadata

  • Download URL: flask-sqlalchemy-bind-1.0.4.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for flask-sqlalchemy-bind-1.0.4.tar.gz
Algorithm Hash digest
SHA256 efe567c37095b9890a7b167f4f514061aab8bd0930bd50b943a47c31a2cc2f9b
MD5 d84c8341298d0280c9d99786404f28c5
BLAKE2b-256 97567ad82e27d25b9f35d49877925c0f10a0b4a887b9cb535e199b6ab144a5bb

See more details on using hashes here.

File details

Details for the file flask_sqlalchemy_bind-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: flask_sqlalchemy_bind-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0

File hashes

Hashes for flask_sqlalchemy_bind-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7eb168cb876f5e2873fc6dd8af4e884eba5da3587c84605aa81fa87762d8108a
MD5 900cb9b347592847ca1c5b37871ed4b1
BLAKE2b-256 709b71b45a5f427d5cd2eb4e1231a786d09d5d015a18160c2da95b78bb75e14e

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 files

1.0.3

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page