Eat at Joe's

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:

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:

Simple Foundation

Lets check what Flask-Admin core has to offer:

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:

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:

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:

  1. 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
  2. File management interface
  3. Redis CLI
  4. Built-in localization support
  5. 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)!

blog comments powered by Disqus