valhuber / fab_nw

Work in Progress - Generate Flask Application Builder views file - 1 page per table

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OLD VERSION

Please use fab-quickstart

fab_nw

Generates Flask Application Builder views.py file - to build a multi-page app (1 page per table) of multi-table pages (includes related_views for related child data).

This is a fab project for a sqlite version northwind (nw). The project contains the Fab Views Gen code (work in progress - see Explore, below), along with the nw project for illustration and testing.

Background

Flask Application Builder (FAB) provides a rapid means for building web pages for database apps, based on Python and Flask (QuickStart here).

FAB inputs are:

  1. models.py file - describes your database tables. You can build models with tools like sqlacodegen.

  2. views.py file - used by fab to generate pages. It consists of segments like this, one for each page:

class OrderModelView(ModelView):
   datamodel = SQLAInterface(Order)
   list_columns = ["ShipName", "Customer.CompanyName", ... "EmployeeId", "CustomerId"]
   show_columns = ["ShipName", "Customer.CompanyName", "OrderDate", ... "ShipCountry", "Id", "EmployeeId", "CustomerId"]
   edit_columns = ["ShipName", "OrderDate",... "ShipCountry", "Id", "EmployeeId", "CustomerId"]
   add_columns = ["ShipName", "OrderDate", ... "ShipCountry", "Id", "EmployeeId", "CustomerId"]
   related_views = [OrderDetailModelView]

appbuilder.add_view(
      OrderModelView, "Order List", icon="fa-folder-open-o", category="Menu")

This project generates the views.py file from the models.py file, to save time and reduce learning curve.

Key Features

  1. Generate views.py with 1 class per (not ab_) table

    a. "Favorite" field (called "name", or contains "name") first

       Eg, List of Products - ProductName is more interesting than ProductId, so show it first
    

    b. Join Fields (join in parents' favorite field)

       Eg, List of Order + OrderDetails: show ProductName (not id)
    

    b. Numeric keyfields last

  2. With Referenced for master/detail (e.g., Order before Customer)

    a. Generated child views first

  3. Predictive Joins (e.g, ProductName on Order+OrderDetail

    a. Note - not generated for edit/show, else you get fab "key errors"

Install

Pre Reqs

To get started, you will need:

  • Python3: run the windows installer; on mac/Unix, consider using brew
  • virtualenv - see here (e.g., pip install virtualenv)

Project Installation

open VSCode, and clone this repo.

In VSCode Python Debug Console:

virtualenv venv
# windows: .\venv\Scripts\activate
source venv/bin/activate
pip install -r requirements.txt

Note: Windows Powershell requires privileges as described here

Generate

Then, in VSCode, open the file fab_views_gen_run.py, and run it (e.g, under the debugger) using the launch config FAB Views Gen Run.

Copy the console output over the app/views.py file.

Run

cd nw
export FLASK_APP=app
flask run

Explore

The main code is fab_views_gen/fab_views_gen_base.py.

For customizations, it is extended by its subclass fab_views_gen/fab_views_gen.py, which is invoked by fab_views_gen_run.py

Screenshot

image

About

Work in Progress - Generate Flask Application Builder views file - 1 page per table


Languages

Language:Python 99.8%Language:HTML 0.2%