-
Notifications
You must be signed in to change notification settings - Fork 8
Home
Python Flask Application Builder (fab) creates "basic" applications for database crud operations quickly, with minimal coding. Typical fab pages can look like this:
- Multi-page: apps include 1 page per table
-
Multi-table: pages include
related_views
for each related child table, and join in parent data - Favorite field first: first-displayed field is "name", or contains "name" (configurable)
-
Predictive joins: favorite field of each parent is shown (product name - not the foreign key
product_id_
) - Ids last: such boring fields are not shown on lists, and at the end on other pages
This is the FAB Quick Start Guide. In about 10 minutes, we'll
- Install Python and FAB, and
-
Create the application above using an existing database called
Northwind
(customers, orders, items, etc). The app is 52 pages, for 13 underlying tables.
To get started, you will need: Python3
-
Python
- On Windows: just run the installer
- On Mac / Unix: install with brew as described here.
- You may encounter some adventures - try these tips
-
virtualenv - a best practice for keeping Python / libraries separate for each project. Install as described here (basically,
pip install virtualenv
) - IDE: Python works with various debuggers; here are setup instructions for two popular ones. The screen shot below is VSCode (recommended but not required for this Quick Start).
Follow the procedure below to create a new FAB project.
Or, you can download the completed project by following these instructions, and then just read on to see how it was originally created.
From the FAB docs, this creates an empty project folder, with a venv
for dependencies.
We'll use nw
for our project directory name.
mkdir nw
cd nw
virtualenv venv
# windows .env\Scripts\activate
source venv/bin/activate
Note: You can deactivate
this venv
, but you must reactivate using the last command above whenever you wish to work on the project again.
# install fab (and dependencies), and create a default empty fab app
pip install flask-appbuilder
flask fab create-app
# pip freeze > requirements.txt -- lists venv dependencies, for rebuild
You will then be prompted for the project name and your db engine type. With the default engine and a project named nw-app
, you should see a structure as shown below.
We now have a well-formed empty project. We now need to acquire and configure a database, set up SQLAlchemy ORM models.py
, and define our pages with views.py
.
To get the database:
- Download this file, a sqlite version of Northwind.
- Copy it to your
nw-app
folder - Update your
nw-app/config.py
file to denote this database name (illustrated below):Northwind_small.sqlite
Your project will look something like this:
FAB requires that we edit 2 key files to make our "empty" project interact with the database. These can get tedious, due to per-page code required for each table / page.
The following sections show how to use generators to avoid the tedious hand creation of the views.py
and the models.py
files.
You must provide model classes for SQLAlchemy. That's a bit of work (13 classes in this small example), but we can automate this with sqlacodegen, like this:
cd nw-app
pip install sqlacodegen
sqlacodegen sqlite:///Northwind_small.sqlite --noviews
Copy the console output over your nw/nw-app/app/models.py
module.
Finally, we need to define some pages. That's also a bit of work to do that by hand, so let's use fab_quick-start
to create the views.py
file from the app/models.py
file:
pip install -i https://test.pypi.org/simple/ fab-quick-start
fab-quick-start run
Copy the console output over your nw/nw-app/app/views.py
file.
The FAB system can create tables in your database for authenticating and authorizing users (tables such as ab_user
, ab_user_role
, etc). You create these as follows:
(venv)$ export FLASK_APP=app
(venv)$ flask fab create-admin
Username [admin]:
User first name [admin]:
User last name [user]:
Email [admin@fab.org]:
Password:
Repeat for confirmation:
Ignore the error "user already exists", since the admin data was pre-loaded.
You can verify your data and admin data like this (mac/unix only):
sqlite3 Northwind_small.sqlite # mac only
> .tables
> .schema Order
> select * from ab_user;
> select * from Territory;
> .quit
You've now created a app with a dozen pages or so; run it like this:
(venv)$ # still cd'd to nw-app
(venv)$ export FLASK_APP=app
(venv)$ flask run
So, you’ve seen how to generate a basic app. That’s just the basic fab flow. To discover more:
- Explore Samples: FAB contains a number of samples you can explore. Here are setup instructions.
-
Explore Docs: you can edit the generated
views.py
file for charts and many other options. The full doc is here.