Introduction
So what is Flask-Admin? I'm seeing a lot of misunderstanding about the project. Typical Flask advice regarding administrative interface looks like this - "just write one from scratch, all these CRUD frameworks are very limiting". Well, I can't agree with the opinion and going to explain why.
Just in case, Flask is a Python Web framework.
Framework Basics
Core of the framework is very simple: you get two-layer menu system and Flask views encapsulated into classes. Here's fully working administrative view:
from flask_admin import BaseView, expose
class MyAdmin(BaseView):
@expose('/')
def index(self):
users = api.get_users()
return self.render('custom_template.html', users=users)
You can define as many views as you want. All exposed routes will have /admin/ prefix added automatically.
This is more verbose but not too different than the following:
from flask import route
@route('/admin/myadmin/')
def index():
users = api.get_users()
return render_template('custom_template.html', users=users)
When you plug your administrative view into the Admin object, the new menu item will be created and Flask routes will be registered for you.
admin = Admin(app)
admin.add_view(MyAdmin(name='My Admin'))
So, why should one use Flask-Admin to build administrative UIs? For a few reasons:
- Reusable code
- Extensible components
- Redistributable administrative UIs
Lets take typical CRUD interface as an example. There are 4 views and each view is doing exactly one action with a model. The framework encapsulates all CRUD logic into a class with 4 routes and accepts model in the class constructor. By passing different models, we'll get different UIs scaffolded for us. And because it is a class, we can inherit from it, change some properties, and make it do whatever we need it to do.
That's how Flask-Admin was built - multiple layers, each providing different functionality. It is up for you to decide which one to use to build your UI.
Some core ideas I had while designing and building the framework:
- Very simple foundation
- Highly extensible architecture
- Add new functionality by extending the framework
- Avoid making assumptions as much as possible
- Avoid tightly coupled components
- Provide sensible defaults, give ability to use the framework as a black box in order to have something working quickly without too much effort
- Have tools to build UIs that can be used by non tech-savvy people
Simple Foundation
Lets check what Flask-Admin core has to offer:
Flask-Admin uses Bootstrap CSS framework to build UIs
- It is well known framework,
- Lots of ready-to-use building blocks,
- There are lots of custom skins out there.
Two-level menu system
- Not limited by two levels, just an assumption that it should be good enough for most of the cases.
Class with view methods as a basic building block
- Multiple instances of the class can be instantiated with different configuration but same code,
- Allows code reuse.
Batteries (CRUD interfaces for different ORMs, file admin, redis console, etc) are built on top of core APIs and are completely optional.
Highly Extensible
It is possible to change almost every single aspect of the Flask-Admin, from templates to model scaffolding logic.
No monkey patching required - in all cases customizations are done by inheriting from various classes and overriding their methods or properties.
Also, current structure endorses decoupled and distributable components. Lets say Flask package maintainer decides to have administrative interface for his own module. All he'll have to do is to add his own administrative classes and distribute them with the package.
Making Assumptions
Flask-Admin uses default Bootstrap skin for a simple reason: I did not want to make strong assumptions for the developer related to look and feel of his UI and didn't want to complicate his life when he'll decide to make his interface look differently.
Other points:
- Bootstrap is lesser evil when compared to custom CSS framework and gives lots of building blocks right away,
- Yes, default Bootstrap skin looks boring (and maybe unprofessional),
- Yes, it is easy to switch to a custom Bootstrap skin,
- No, "better" skin should not be part of the Flask-Admin. It can be hosted as a separate project by anyone. Can be even hosted in flask_admin.contrib package as long as someone volunteers to maintain it,
- Technically it is possible to use any other CSS framework, but Flask-Admin only supports Bootstrap versions 2 and 3 out of the box. We just don't have capacity to support and maintain templates for other CSS frameworks.
Flask-Admin does not care what kind of authentication system you're using. Want to use PAM authentication? No problem, go ahead. Or maybe plain HTTP authentication - sure, just check necessary headers.
The framework was not build around CRUD interface. CRUD layer is just one of the supplied batteries. You can use Flask-Admin to build fully custom user interface. The framework will handle menu structure and you'll have to write your own views.
Sensible Defaults
Sure, Flask-Admin will work just fine if you don't have any special needs. You can certainly use it as a black box without digging into its source code. The CRUD interface is customizable with the predefined properties (a-la Django admin).
On other hand, it is highly recommended to check the framework source code to see how it works and get better understanding what you can do with the framework.
Nice Looking UIs
First of all, I'd like thank everyone at Campus Bubble, my current employer. They gave me the chance to work on a great product we're building together. It is B2B platform that simplifies communication between students and school administration. Essentially it's a private social network built from the ground up to solve for the needs of a university. Admissions, campus life, announcements, this kind of stuff.
Campus Bubble is a single page app with React.js on a frontend and Python services on a backend. We use Flask, SQLAlchemy, Tornado (SockJS push notifications), PostgreSQL, Redis - a pretty standard Flask stack. And yes, the administrative interface is built with help of Flask-Admin, which saved us a lot of development time.
Here's how it looks like:
What was done to make it look so pretty:
- Custom Bootstrap skin
- Customized menu system - there are three levels in menu hierarchy instead of default two levels
- Very flexible permission system integrated with application business logic
- Contextual help everywhere
- Various custom fields and widgets (like drag'n'drop widget for inline models, etc)
Even though it looks completely different, it is built on top of Flask-Admin and there were no modifications to the framework itself. One of the reasons why I didn't really work on Flask-Admin lately is because Flask-Admin was good enough for our purposes.
Short list of Flask-Admin features
As a short summary, here's what Flask-Admin has to offer:
- CRUD interface
- Supports multiple ORMs: SQLAlchemy, MongoEngine, Peewee, raw PyMongo and basic support for Google AppEngine (DB and NDB).
- It is easy to add support for a new ORM
- Has almost everything you would expect from decent CRUD interface:
- Automatic (but fully configurable) list view and form generation
- Sorting, Paging, Quick search
- Customizable column filters
- AJAX foreign key loaders
- Inline editor of related models
- Advanced SQLAlchemy support:
- Can work with related models, figures out intermediate JOINs necessary to run the query
- Works with inherited models
- Handles one-to-one, one-to-many and many-to-many relations automatically
- GeoAlchemy2 support
- MongoEngine backend allows working with arbitrary nested documents
- Supports multiple ORMs: SQLAlchemy, MongoEngine, Peewee, raw PyMongo and basic support for Google AppEngine (DB and NDB).
- File management interface
- Redis CLI
- Built-in localization support
- Good documentation and lots of examples
Flask-Admin Future
Unfortunately, I didn't have much time to work on Flask-Admin lately (mostly because of work), so Flask-Admin was moved into its own organization and got two more maintainers:
Thank you guys, your help is invaluable.
Flask-Admin started to show up relatively large deployments and being used by large companies. For example, Airbnb released their Airflow tool recently and it uses Flask-Admin to build administrative interface.
Anyway, Flask-Admin is open source project and pull requests are always welcome (but subject to careful review)!