Flask-Admin
Flask-Admin is a simple and extensible admin interface framework for Flask. It provides a batteries-included solution for adding administrative interfaces to Flask applications, allowing management of data models with auto-generated Create, Read, Update, Delete (CRUD) views. The current version is 2.0.2, and it maintains an active release cadence with regular updates and new feature additions as part of the Pallets-Eco organization.
Warnings
- breaking Flask-Admin dropped support for Python versions older than 3.10 starting with v2.0.0. Ensure your environment uses Python 3.10 or newer.
- breaking S3 file management (`S3FileAdmin`) has replaced the `boto` library with `boto3`. The constructor now requires an `s3_client` parameter (a `boto3.client('s3')` instance) instead of `aws_access_key_id`, `aws_secret_access_key`, and `region` parameters.
- breaking Azure Blob Storage (`AzureFileAdmin`) has upgraded its SDK from legacy v2 to v12. The constructor now requires a `blob_service_client` parameter (an instance of Azure's `BlobServiceClient`) instead of a `connection_string`.
- deprecated The `flask.ext` import pattern (e.g., `from flask.ext.admin import Admin`) is deprecated and should no longer be used. Direct imports (e.g., `from flask_admin import Admin`) are the correct approach.
- gotcha Flask-Admin does not install database or file storage backend dependencies (e.g., SQLAlchemy, boto3, azure-storage-blob) by default. You must install these separately, often using `pip install flask-admin[extra]` or by listing them in your `requirements.txt`.
- gotcha When customizing the admin interface, fully overriding built-in templates can make future upgrades difficult. It's generally recommended to extend the existing templates rather than replacing them entirely.
Install
-
pip install flask-admin -
pip install flask-admin[sqla] -
pip install flask-admin[mongoengine] -
pip install flask-admin[s3]
Imports
- Admin
from flask_admin import Admin
- ModelView (SQLAlchemy)
from flask_admin.contrib.sqla import ModelView
- BaseView
from flask_admin import BaseView, expose
Quickstart
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a_hard_to_guess_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///admin.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Define a simple model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = 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.name
# Create tables (if they don't exist)
with app.app_context():
db.create_all()
# Initialize Flask-Admin
admin = Admin(app, name='My Admin', template_mode='bootstrap4')
# Add views
admin.add_view(ModelView(User, db.session))
@app.route('/')
def index():
return '<p>Hello from Flask! Go to <a href="/admin/">/admin/</a> to manage users.</p>'
if __name__ == '__main__':
app.run(debug=True)