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

Overview

Flask-FileAlchemy

Flask-FileAlchemy is a Flask extension that lets you use Markdown or YAML formatted plain-text files as the main data store for your apps.

Installation

$ pip install flask-filealchemy

Background

The constraints on which data-store to use for applications that only have to run locally are quite relaxed as compared to the ones that have to serve production traffic. 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 Markdown or 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 data models

Define your data models using the standard (Flask-)SQLAlchemy API. As an example, a BlogPost model can defined as follows.

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)
   content = Column(Text, nullable=False)

Add some data

Next, 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.

We currently support three different ways to define data.

1. Multiple YAML files

The first way is to have multiple YAML files inside the data/<__tablename__>/ directory, each file corresponding to one record.

In case of the "blog" example, we can define a new BlogPost record by creating the file data/blog_posts/first-post-ever.yml with the following content.

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

Adding more such files in the same directory would result in more records.

2. Single YAML file

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.

For the "blog" example, this would look like the following.

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

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

3. Markdown/Frontmatter

It's also possible to load data from Jekyll-style Markdown files containing Frontmatter metadata.

In case of the blog example, it's possible to create a new BlogPost record by defining a data/blog_posts/first-post-ever.md file with the following content.

---
slug: first-post-ever
title: First post ever!
---

This blog post talks about how it's the first post ever!

Please note that when defining data using markdown, the name of the column associated with the main markdown body needs to be content.

4. Configure and load

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.

Owner
Siddhant Goel
Software Developer 👨🏻‍💻
Siddhant Goel
A Flask extension that enables or disables features based on configuration.

Flask FeatureFlags This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on

Rachel Greenfield 131 Sep 26, 2022
Harmony, a discord clone, allows users to chat with other users in real time via servers, channels, and direct messages

Harmony, a discord clone, allows users to chat with other users in real time via servers, channels, and direct messages

Seth Holland 3 Feb 03, 2022
A web application made with Flask that works with a weather service API to get the current weather from all over the world.

Weather App A web application made with Flask that works with a weather service API to get the current weather from all over the world. Uses data from

Christian Jairo Sarmiento 19 Dec 02, 2022
Find and notify users in your Active Directory with weak passwords

Crack-O-Matic Find and notify users in your Active Directory with weak passwords. Features: Linux-based Flask-based web app Hashcat or John cracker Au

Adrian Vollmer 92 Dec 31, 2022
Beautiful Interactive tables in your Flask templates.

flask-tables Beautiful interactive tables in your Flask templates Resources Video demonstration: Go to YouTube video. Learn how to use this code: Go t

Miguel Grinberg 209 Jan 05, 2023
Flask 文档中文翻译

Flask 中文文档 这里是 Flask 文档中文翻译项目,欢迎参与! 在开始翻译之前,请务必阅读下面的 Contributing Guide 了解贡献流程,然后阅读这个 Issue 了解翻译要求,在这个 Discussion 投票选出你认为合适的翻译词汇,在这个 Discussion 投票选出你喜

Grey Li 93 Nov 28, 2022
Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.

Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of

Max Countryman 310 Dec 14, 2022
Adds SQLAlchemy support to Flask

Flask-SQLAlchemy Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy

The Pallets Projects 3.9k Dec 29, 2022
A Python, Flask login system

Python Login System This is a basic login + authenticason system for flask using Flask_Login and Flask_SQLAlchemy Get started on your own To use this

MrShoe 0 Feb 02, 2022
Quick and simple security for Flask applications

Note This project is non maintained anymore. Consider the Flask-Security-Too project as an alternative. Flask-Security It quickly adds security featur

Matt Wright 1.6k Dec 19, 2022
Track requests to your Flask website with Matomo

Flask-Matomo Flask-Matomo is a library which lets you track the requests of your Flask website using Matomo (Piwik). Installation pip install flask-ma

Lucas Hild 13 Jul 14, 2022
Python Flask API service, backed by DynamoDB, running on AWS Lambda using the traditional Serverless Framework.

Serverless Framework Python Flask API service backed by DynamoDB on AWS Python Flask API service, backed by DynamoDB, running on AWS Lambda using the

Andreu Jové 0 Apr 17, 2022
A Flask app template with integrated SQLAlchemy, authentication, and Bootstrap frontend

Flask-Bootstrap Flask-Bootstrap is an Flask app template for users to clone and customize as desired, as opposed to a Flask extension that you can ins

Eric S. Bullington 204 Dec 26, 2022
This repo contains the Flask API to expose model and get predictions.

Tea Leaf Quality Srilanka Chapter This repo contains the Flask API to expose model and get predictions. Expose Model As An API Model Trainig will happ

DuKe786 2 Nov 12, 2021
This is a simple web application using Python Flask and MySQL database.

Simple Web Application This is a simple web application using Python Flask and MySQL database. This is used in the demonstration of development of Ans

Alaaddin Tarhan 1 Nov 16, 2021
A simple barcode and QR code generator built in Python with Flask.

✨ Komi - Barcode & QR Generator ✨ A simple barcode and QR code generator built in Python with Flask. 📑 Table of Contents Usage Installation Contribut

Bonnie Fave 2 Nov 04, 2021
Implement Instagram with flask

Blue club The place where manly men live and breathe. Move to Notion Move to Fig

3 Apr 07, 2022
With Flask. Everything in a JSON.

Little Library REST API py 3.10 The only one requeriment it's to have Flask installed. To run this, in ./src/(if you're in PS): $env:FLASK_APP="app

Luis Quiñones Requelme 1 Dec 15, 2021
Neo4j Movies Example application with Flask backend using the neo4j-python-driver

Neo4j Movies Application: Quick Start This example application demonstrates how easy it is to get started with Neo4j in Python. It is a very simple we

Neo4j Examples 309 Dec 24, 2022
Map Matching & Weight Completion service - Java (Springboot) & Python(Flask)

Map Matching service to match coordinates to roads using Java and Springboot. Weight Completion service to fill in missing weights in a graph, using Python and Flask.

2 May 13, 2022