Minimalistic extension to add support for SQLAlchemy to your Flask app.
Project description
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.
Set Up:
Install using pip:
pip install flask-sqlalchemy-bind
Setting up You Flask App
A simple Flask app setup using Flask-SQLAlchemy-Bind:
from flask import Flask
from flask_sqlalchemy_bind import SQLAlchemy_bind
app = Flask(__name__)
app.config["DATABASE"] = "sqlite:///:memory:"
db = SQLAlchemy_bind(app)
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
db.session.add(User(username="Hi", email="itsme@example.com"))
db.session.commit()
users = User.query.all()
Using an App Factory
You can also use Flask-SQLAlchemy bind with an app factory.
from flask import Flask
from flask_sqlalchemy_bind import SQLAlchemy_bind
# outside of app factory
db = SQLAlchemy_bind(app)
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:"
db.init_app(app)
return app
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for flask-sqlalchemy-bind-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dbce16724c107e3bb0c810e3ca0b647394824fc91ab1cd3b5573041e7b513f6 |
|
MD5 | bd294adbef1d3c7ee33ff8511524ef0c |
|
BLAKE2b-256 | 802148e76cfacf1b3f5f7186a6dc05b571c4d66dfbe81ac06fd0106be44c711d |
Close
Hashes for flask_sqlalchemy_bind-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd52e116c83529bb9aae23f1f0b0ede871d534e7feb23a99e1cfc7617a209dfe |
|
MD5 | c76cb2997badadffd879ba2aec81da84 |
|
BLAKE2b-256 | 14d0b241dfb554df1fc7d1ec6ced69a1000cbcd3537ba09dc9aea6e82f5d690e |