Flask App Builder

repository·master·Indexed 26 days ago

https://github.com/dpgaspar/flask-appbuilder

A 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.

Tokens
48K
Snippets
125
Records
210
Agent score
90%

What's inside flask-appbuilder

  1. Overview of Flask App Builder

    master
    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.
  2. Overview of Flask App Builder features

    master

    Flask 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_USER authentication. 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.
  3. Overview of Flask-AppBuilder

    master

    Flask-AppBuilder (FAB) is a framework designed for simple and rapid application development. It is built on top of Flask and 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
  4. Understand the Class View hierarchy

    master

    Flask 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 like ModelView, 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 to SimpleFormView but 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 master ModelView alongside multiple related detail ModelViews.
    • MultipleView: Used to render multiple views on a single page (e.g., a ModelView and a GroupByChartView).
  5. Understand the Security implementation architecture

    master

    Security 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 the AppBuilder instance.
    • 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 of BaseSecurityManager.
  6. Understand the Data Access layer

    master

    The 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.
  7. Core Features of Flask App Builder

    master

    Flask 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-extended to protect endpoints.
    • Support for multi-language applications via Babel (i18n).
  8. Create a custom ShowWidget

    master

    To customize the detail/show page (e.g., to add action buttons to the top of a long page):

    1. Create a template override: Extend appbuilder/general/widgets/show.html. Override specific blocks (like columns) and use {{ super() }} to preserve existing content.
    2. Create a Python class: Inherit from flask_appbuilder.widgets.ShowWidget and set the template attribute.
    3. Apply to a View: Assign your custom widget class to the show_widget attribute of your ModelView.
    # 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
  9. Configure Flask-Talisman for CSP nonces

    master

    Flask App Builder can use csp_nonce() in Jinja2 templates if Flask-Talisman is 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)