Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Showing how to feed a Flask application with an existing database
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
# --------- Configurations ------------
class Config:
# Change to your Database_URI, here we are using sqlite database
# Note that postgres URI looks like 'postgresql://UserName:password@host:port/DatabaseName'
SQLALCHEMY_DATABASE_URI = 'sqlite:///equimolar.db' # database
DEBUG = True
def create_app():
# Simple Application Factory
with app.app_context():
return app
app = create_app()
# -------- Models of interest ----------
class Article(db.Model):
What is really neede is just the table name
In this case, I will be looking at the articles table'''
__table__ = db.Model.metadata.tables['articles']
def __repr__(self):
return '<Article {}>'.format(
# -------- Endpoints -------------------
def index():
all_articles = Article.query.order_by(Article.last_mod_date.desc())
return render_template('articles_table.html', articles=all_articles)
if __name__ == '__main__':'', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment