Flask-AppBuilder Tutorial
Flask-AppBuilder is a "Simple and rapid application development framework, built on top of Flask, that includes detailed security, auto CRUD generation for your models, google charts, and much more." We are using F.A.B. in CS 374 to limit the amount of web programming you need to do (and learn) for rapid prototyping, especially for CRUD operations.
In this tutorial, you will create a web application for managing the Conference Review System tables you generated in HW4.
The application will connect to your schema in the sec1/sec2 database.
Part 1. Getting Started¶
- Install F.A.B. in your virtual environment:
pip install Flask-AppBuilder - Create a folder named
fabnear your homework files (outside of your project repo). - Download the following files from the
profsrepository into yourfabfolder: - Create two (blank) files named
models.pyandviews.py(in yourfabfolder):touch models.py views.py - Edit the
config.pyfile:- Delete the
TEAMglobal variable – you'll use that later for your group project. - Change the
SQLALCHEMY_DATABASE_URIto the URI you used for HW4. - Change the
APP_NAMEto"Conference Review System"(or similar).
- Delete the
- Run the following command to create an admin user account for your web app:
flask fab create-admin - Run the following command to start a local web server for development:
flask run --debug - Open http://127.0.0.1:5000 in a new tab, and sign in to your application.
- When running
fabfor the first time, 11 new tables starting withab_were automatically created in your database. These tables are used to manage user accounts and permissions in the web application. Open your schema in pgAdmin and look at the new tables that were created.
Part 2. Edit models.py¶
Note
This part of the tutorial will walk you through the steps of GP4.
For simplicity, we will focus on only five of the tables from hw4:
paper, history, conference, topic, and paper_topic.
- Replace
models.pywith the file you generated (and didn't edit) during HW4. - Make each class extend F.A.B. instead of SQLAlchemy:
- Add
from flask_appbuilder import Model - Remove
class Base(DeclarativeBase) - Find and replace
(Base)with(Model) - Find and replace
Base.metadatawithModel.metadata
- Add
- Add the following
__str__()method to thePaperclass:def __str__(self): return f"Paper #{self.paper_id}: {self.title}" - Add the following
__str__()method to theHistoryclass:def __str__(self): return f"History for #{self.paper_id} at {self.timestamp}" - Add
__str__()methods to theConferenceandTopicclasses. - Rename the following attributes (on both sides of the relationship):
- In the
Conferenceclass, renamepapertopapers.- In the
Paperclass, changeback_populatesofconferencetopapers.
- In the
- In the
Paperclass, renametopictotopics.- In the
Topicclass, changeback_populatesofpapertotopics.
- In the
- In the
Topicclass, renamepapertopapers.- In the
Paperclass, changeback_populatesoftopicstopapers.
- In the
Important!
Take a minute to understand what you just did:
- Conference → Paper is a 1-to-many relationship, so
Conference.papers(plural) is a list ofPaperobjects. On the other side of the relationship,Paper.conference(singular) is aConferenceobject. - Paper → Topic is a many-to-many relationship with the
paper_topictable in between. SoPaperhas a list ofTopicobjects, andTopichas a list ofPaperobjects. Notice thatsecondary='paper_topic'is in therelationship().
- In the
Part 3. Edit views.py¶
- Edit the
mvstubs.pyfile:- Delete the
TEAMglobal variable – you'll use that later for your group project. - Edit
psycopg.connect()to use your username and database name (sec1orsec2). - In the SQL statement, change
table_schemato be your username instead of public.
- Delete the
- Run
mvstubs.pyand redirect the output toviews.py:This script generates apython mvstubs.py > views.pyModelViewclass for every table in your database. That way, you don't have to write so much starter code by hand. - In
views.py:- Delete all classes except for
Conference,History,Paper, andTopic. - Delete the corresponding
appbuilder.add_view()statements as well. - Add the following import statements at the top:
import models from app import appbuilder from flask_appbuilder import ModelView from flask_appbuilder.models.sqla.interface import SQLAInterface - Also find and replace
HistoryswithHistory(since "Historys" is not a word). Themvstubs.pyscript adds ansto nouns, but the plural is not always correct.
- Delete all classes except for
- At this point, refresh your application and browse each of the four tables.
- If needed, restart with
flask run --debugin the Terminal. - Click the Edit button on the conference record, and notice how the papers are shown.
- If needed, restart with
- Add the following to the
Conferenceclass:Move theadd_exclude_columns = edit_exclude_columns = show_exclude_columns = ["papers"] related_views = [Paper]Paperclass definition above theConferenceclass, so that the code compiles. - Add the following to the
Paperclass:Reorder the class definitions so that the code compiles.add_exclude_columns = edit_exclude_columns = show_exclude_columns = ["topics", "history"] related_views = [Topic, History] - Edit a Paper record to make sure the History and Topics tabs are shown.
- Notice how "Paper Author" and "Review" are rendered. Ideally these would say "Authors" and "Reviews", and the objects would have a
__str__()method so they were human-readable. - In your actual project, you will complete the above steps for all tables, not just four. Have fun!
- Notice how "Paper Author" and "Review" are rendered. Ideally these would say "Authors" and "Reviews", and the objects would have a