- Published on
Flask Extension for Real Application
- Authors
- Name
- Wuttichai Kaewlomsap
- @wuttichaihung
List of useful Flask extensions
- Flask App Builder
- Flask SQL Alchemy
- Flask Admin
- Flask Monitoring Dashboard
1. Flask App Builder
Flask App Builder is development framework, built on top of Flask. Includes flask, flask-sqlalchemy, flask-login, flask-openid, flask-wtform, flask-babel Read
- Step 1: Install flask-appbuilder
pip install flask-appbuilder
- Step 2: Create your application
export FLASK_APP=app
flask fab create-app
<your_app_name>
├─ app
│ ├─ templates
│ │ └─ 404.html
│ ├─ transalations
│ ├─ __init__.py
│ ├─ model.py
│ └─ views.py
├─ babel
├─ app.db
├─ config.py
├─ README.rst
└─ run.py
- Step 3: Create admin account
cd <your_app_name>
flask fab create-admin
- Step 4: Run your app
flask run
- The application will show you with simple login page as figure below
2. Flask SqlAclchemy
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application Read
- Install
pip install -U Flask-SQLAlchemy
main.py
import names
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = "SetSecretHere"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
# Create Model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return '<User %r>' % self.username
db.create_all()
# Add Admin
@app.route('/')
def index():
username = names.get_first_name()
user = User(username=username, email=f'{username}@example.com')
db.session.add(user)
db.session.commit()
return jsonify({"msg", "added", "username": username})
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
3. Flask Admin
Flask-Admin build an admin interface on top of an existing data model.
- install
pip install Flask-Admin
- Flask admin path /admin
main.py
from flask import Flask, request, jsonify, render_template
from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# Flask Sqlalchemy
app.config['SECRET_KEY'] = "SetSecretHere"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return '<User %r>' % self.username
db.create_all()
# Flask Admin
app.config['FLASK_ADMIN_SWATCH'] = 'slate'
admin = Admin(app, name='Admin', template_mode='bootstrap4')
admin.add_view(ModelView(User, db.session))
@app.route('/')
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
4. Flask Monitoring Dashboard
- Flask Monitoring Dashboard allows you to see which endpoints process a lot of requests and how fast
- install
pip install Flask-MonitoringDashboard
- Flask monitoring dashboard path /dashboard
main.py
from flask import Flask, request, jsonify, render_template
import flask_monitoringdashboard as dashboard
app = Flask(__name__)
# Flask Mornitoring Dashboard
dashboard.config.init_from(file=r'config.cfg')
dashboard.bind(app)
@app.route('/')
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
config.cfg
[dashboard]
APP_VERSION=1.0
GIT=/<path to your project>/.git/
CUSTOM_LINK=dashboard
MONITOR_LEVEL=0
OUTLIER_DETECTION_CONSTANT=2.5
SAMPLING_PERIOD=20
ENABLE_LOGGING=True
BRAND_NAME=Flask Monitoring Dashboard
TITLE_NAME=Flask-MonitoringDashboard
DESCRIPTION=Automatically monitor the evolving performance of Flask/Python web services
SHOW_LOGIN_BANNER=False
SHOW_LOGIN_FOOTER=False
[authentication]
USERNAME=admin
PASSWORD=admin
SECURITY_TOKEN=YourSecret
[database]
TABLE_PREFIX=fmd
DATABASE=sqlite:///dashboard.db
[visualization]
TIMEZONE=Asia/Bangkok
COLORS={'main':'[0,97,255]',
'static':'[255,153,0]'}