Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EwetoyeIbrahim/b01ea58ba229bf62fe4baaf6c8e57860 to your computer and use it in GitHub Desktop.
Save EwetoyeIbrahim/b01ea58ba229bf62fe4baaf6c8e57860 to your computer and use it in GitHub Desktop.
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
app=Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
with app.app_context():
db.Model.metadata.reflect(db.engine)
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(self.id)
# -------- Endpoints -------------------
@app.route('/')
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__':
app.run(host='127.0.0.1', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment