Flask App Builder
repository·master·Indexed 26 days ago
https://github.com/dpgaspar/flask-appbuilderA rapid application development framework built on Flask, specializing in automated CRUD generation, robust security/RBAC, and integrated data visualization. Version 1.0.0 includes tools for database migrations, password hashing, and custom action decorators for ModelViews.
What's inside flask-appbuilder
- Flask App Builder (FAB) is a rapid application development framework built on top of Flask. It provides automated features for security, CRUD (Create, Read, Update, Delete) generation from SQLAlchemy models, and data visualization using Google Charts. It is designed to be easily integrated with standard Flask and Jinja2 development workflows.
Overview of Flask App Builder features
masterFlask App Builder (FAB) is a development framework designed to simplify web application development by adhering to the DRY (Don't repeat yourself) principle. It provides a highly configurable foundation that goes beyond simple admin scaffolding, allowing for custom pages and flows using standard Flask/Jinja2.
Key capabilities include:
- Database Integration: Uses SQLAlchemy with support for multiple databases (SQLite, MySQL, ORACLE, MSSQL, DB2, etc.), vertical partitioning (multiple connections), and easy audit mixins for tracking created/changed users and timestamps.
- Security: Features role-based access control (RBAC), automatic permission lookup based on exposed methods, and support for OAuth, Database, LDAP, and
REMOTE_USERauthentication. It also supports public/private permissions and self-user registration. - Views and Widgets: Provides automatic menu and CRUD generation, various list filters, and multiple view widgets (lists, master-detail, thumbnails). Includes built-in support for Select2, Datepicker, and DateTimePicker.
- Forms: Automatically generates Add, Edit, and Show forms from database models. It includes automatic base validators, custom validators, field sets (Django-style), and handles image/file uploads and database field associations.
- Internationalization: Supports multi-language applications via Babel.
- Frontend Assets: Includes Bootstrap 3.3.1, Bootswatch Themes, Font-Awesome icons, and Google Charts.
Overview of Flask-AppBuilder
masterFlask-AppBuilder (FAB) is a framework designed for simple and rapid application development. It is built on top of
Flaskand provides several high-level features including:- Detailed security management
- Automatic CRUD (Create, Read, Update, Delete) generation for your models
- Google Charts integration
- Support for multiple databases and generic data sources
- Internationalization (i18n)
- REST API support
Understand the Class View hierarchy
masterFlask App Builder's view layer is organized into a hierarchy where each layer handles specific responsibilities. Developers typically interact with
BaseViews,IndexViews, and leaf nodes likeModelView, chart views, and form views.Key view classes include:
BaseView: The foundation that collects exposed methods, creates Flask blueprints, registers URLs, and initializes base permissions.IndexView: A specialized view for rendering the index page.SimpleFormView: A subclass used to render WTForms.PublicFormView: Similar toSimpleFormViewbut restricted to public access.BaseModelView: Handles the initial data model layer, including search forms and filters.BaseChartView: Provides basic chart functionality.GroupByChartView: A subclass for rendering Google charts using group-by queries.DirectByChartView: A subclass for rendering Google charts with direct queries.BaseCRUDView: Implements core functionality for add, edit, and delete operations, and creates all necessary forms.ModelView: The primary subclass used to render views based on models with full CRUD UI functionality.MasterDetailView: Renders a masterModelViewalongside multiple related detailModelViews.MultipleView: Used to render multiple views on a single page (e.g., aModelViewand aGroupByChartView).
Understand the Security implementation architecture
masterSecurity in Flask App Builder is managed through a hierarchy of Manager classes. These classes are responsible for registering security views, implementing authentication methods, and managing permissions (inserting/removing permissions on the backend).
Security components:
BaseManager: The base class for all Manager classes; it holds theAppBuilderinstance.AbstractSecurityManager: An abstract class that defines the required methods for security managers.BaseSecurityManager: The base class for security that registers views, implements authentication, manages roles/users/views, and handles database permission synchronization.sqla.SecurityManager: The SQLAlchemy-specific implementation ofBaseSecurityManager.
Understand the Data Access layer
masterThe data access layer is designed to abstract the backend. All data access classes implement a unique API layer via the
BaseInterface.Supported interfaces:
SQLAInterface: Provides data access specifically for SQLAlchemy.GenericInterface: Provides data access for custom data structures.
User Registration with LDAP Authentication
masterWhen using LDAP authentication, user registration is automatic. No 'Register' option is presented on the login screen. All users are registered upon their first login, and their required profile information is fetched directly from the LDAP server.Core Features of Flask App Builder
masterFlask App Builder includes several high-level features for application development:
Database Support
- Uses SQLAlchemy with support for SQLite, MySQL, ORACLE, MSSQL, DB2, etc.
- Partial support for MongoDB via MongoEngine.
- Supports multiple database connections (Vertical partitioning).
- Provides mixins for auditing models (timestamps and user tracking).
Security & Authentication
- Role-Based Access Control (RBAC) with automatic permission lookup based on exposed methods.
- Supports OAuth, OpenID, Database, LDAP, and REMOTE_USER environment variables.
- Supports public (no authentication) and private permissions, as well as self-user registration.
Views, Widgets, and Forms
- Automatic CRUD generation and menu generation.
- Various view widgets: lists, master-detail, and thumbnails.
- Integrated UI components: Select2, Datepicker, and DateTimePicker.
- Automatic form generation from database models with built-in validators, labels, and descriptions.
- Support for file and image uploads.
API and Internationalization
- Automatic CRUD RESTful APIs with integration for
flask-jwt-extendedto protect endpoints. - Support for multi-language applications via Babel (i18n).
Create a custom ShowWidget
masterTo customize the detail/show page (e.g., to add action buttons to the top of a long page):
- Create a template override: Extend
appbuilder/general/widgets/show.html. Override specific blocks (likecolumns) and use{{ super() }}to preserve existing content. - Create a Python class: Inherit from
flask_appbuilder.widgets.ShowWidgetand set thetemplateattribute. - Apply to a View: Assign your custom widget class to the
show_widgetattribute of yourModelView.
# 1. The Template: templates/widgets/my_show.html {% extends "appbuilder/general/widgets/show.html" %} {% block columns %} <div class="well well-sm"> {{ lib.render_action_links(actions, pk, modelview_name) }} {{ lib.lnk_back() }} </div> {{ super() }} {% endblock %} # 2. The Python Class from flask_appbuilder.widgets import ShowWidget class MyShowWidget(ShowWidget): template = 'widgets/my_show.html' # 3. The View class MyModelView(ModelView): datamodel = SQLAInterface(MyModel) show_widget = MyShowWidget- Create a template override: Extend
Replace RestCRUDView with ModelView
masterThe deprecated
RestCRUDViewclass has been removed. To maintain REST functionality or standard views, inherit fromModelViewinstead.# After (v5.x) from flask_appbuilder import ModelView class MyView(ModelView): datamodel = SQLAInterface(MyModel, db.session)Initialize AppBuilder with db.session for v0.9.x migration
masterIn version 0.9.x, the initialization of
AppBuilder(formerlyBaseApp) changed. You must now pass the SQLAlchemysessionobject instead of thedbobject.# Change from: BaseApp(app, db) # To: AppBuilder(app, db.session)Configure Flask-Talisman for CSP nonces
masterFlask App Builder can use
csp_nonce()in Jinja2 templates ifFlask-Talismanis installed. However, FAB does not initialize Talisman automatically. You must initialize it manually in your application setup.from flask import Flask from flask_appbuilder import AppBuilder, SQLA from flask_talisman import Talisman app = Flask(__name__) app.config.from_object('config') db = SQLA(app) appbuilder = AppBuilder(app, db.session) Talisman(app)