Flask-Admin

2.0.2 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart sets up a basic Flask application with Flask-Admin, using Flask-SQLAlchemy and a SQLite database. It defines a `User` model and registers it with the admin interface, providing instant CRUD functionality. Ensure `Flask-SQLAlchemy` is also installed for this example.

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)

view raw JSON →