Skip to main content

YAML-formatted plain-text file based models for Flask backed by Flask-SQLAlchemy

Project description

https://github.com/siddhantgoel/flask-filealchemy/workflows/flask-filealchemy/badge.svg https://badge.fury.io/py/flask-filealchemy.svg https://img.shields.io/badge/code%20style-black-000000.svg

Flask-FileAlchemy lets you use YAML-formatted plain-text files as the data store for your Flask app.

Installation

$ pip install flask-filealchemy

Background

While there are better data stores to use in production than plain-text, the constraints on data stores for applications that only have to run locally are much more relaxed. For such applications, it’s normally OK to sacrifice on performance for ease of use.

One very strong use case here is generating static sites. While you can use Frozen-Flask to “freeze” an entire Flask application to a set of HTML files, your application still needs to read data from somewhere. This means you’ll need to set up a data store, which (locally) tends to be file based SQLite. While that does the job extremely well, this also means executing SQL statements to input data.

Depending on how many data models you have and what types they contain, this can quickly get out of hand (imagine having to write an INSERT statement for a blog post).

In addition, you can’t version control your data. Well, technically you can, but the diffs won’t make any sense to a human.

Flask-FileAlchemy lets you use an alternative data store - plain text files.

Plain text files have the advantage of being much easier to handle for a human. Plus, you can version control them so your application data and code are both checked in together and share history.

Flask-FileAlchemy lets you enter your data in YAML formatted plain text files and loads them according to the SQLAlchemy models you’ve defined using Flask-SQLAlchemy This data is then put into whatever data store you’re using (in-memory SQLite works best) and is then ready for your app to query however it pleases.

This lets you retain the comfort of dynamic sites without compromising on the simplicity of static sites.

Usage

Define your data models using the standard (Flask-)SQLAlchemy API.

app = Flask(__name__)

# configure Flask-SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'

db = SQLAlchemy(app)

class BlogPost(db.Model):
    __tablename__ = 'blog_posts'

    slug = Column(String(255), primary_key=True)
    title = Column(String(255), nullable=False)
    contents = Column(Text, nullable=False)

Then, create a data/ directory somewhere on your disk (to keep things simple, it’s recommended to have this directory in the application root). For each model you’ve defined, create a directory under this data/ directory with the same name as the __tablename__ attribute.

In this example, we’ll add the following contents to data/blog_posts/first-post-ever.yml.

slug: first-post-ever
title: First post ever!
contents: |
   This blog post talks about how it's the first post ever!

For “smaller” models which don’t have more than 2-3 fields, Flask-FileAlchemy supports reading from an _all.yml file. In such a case, instead of adding one file for every row, simply add all the rows in the _all.yml file inside the table directory.

In this example, this could look like the following.

- slug: first-post-ever
  title: First post ever!
  contents: This blog post talks about how it's the first post ever!
- slug: second-post-ever
  title: second post ever!
  contents: This blog post talks about how it's the second post ever!

Finally, configure Flask-FileAlchemy with your setup and ask it to load all your data.

# configure Flask-FileAlchemy
app.config['FILEALCHEMY_DATA_DIR'] = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), 'data'
)
app.config['FILEALCHEMY_MODELS'] = (BlogPost,)

# load tables
FileAlchemy(app, db).load_tables()

Flask-FileAlchemy then reads your data from the given directory, and stores them in the data store of your choice that you configured Flask-FileAlchemy with (the preference being sqlite:///:memory:).

Please note that it’s not possible to write to this database using db.session. Well, technically it’s allowed, but the changes your app makes will only be reflected in the in-memory data store but won’t be persisted to disk.

Contributing

Contributions are most welcome!

Please make sure you have Python 3.5+ and Poetry installed.

  1. Git clone the repository - git clone https://github.com/siddhantgoel/flask-filealchemy.

  2. Install the packages required for development - poetry install.

  3. That’s basically it. You should now be able to run the test suite - poetry run py.test.

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

flask-filealchemy-0.4.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

flask_filealchemy-0.4.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file flask-filealchemy-0.4.1.tar.gz.

File metadata

  • Download URL: flask-filealchemy-0.4.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.3 Linux/4.19.0-8-amd64

File hashes

Hashes for flask-filealchemy-0.4.1.tar.gz
Algorithm Hash digest
SHA256 6d5d8a9217fe1818e32db827e64b586b38e6ca310a5ae4b22c57fdb2d6661d3f
MD5 cfc6780e18cfb3b44eae036d31ec177a
BLAKE2b-256 39c68e05506995e4b4f27c7d5d4c282db21a8b8ed49a1d2966ffc3f48976f51d

See more details on using hashes here.

File details

Details for the file flask_filealchemy-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: flask_filealchemy-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.3 Linux/4.19.0-8-amd64

File hashes

Hashes for flask_filealchemy-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7135f0bee7b748d065a29704fd087658b19bf25609a93c47f35a71028307f7e
MD5 a1a53351540815251f25d5ed8cf7f434
BLAKE2b-256 67f5cd2ee0568a601561bf9d754b19f1f1e243817de65fe1214e62b9033308f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page