This is a project implementing the blog post by Mbithe Nzomo.
Prerequisites
-
Python 2.7
-
virtualenv
-
Mysql
$ brew update && brew install mysql
-
Flask
$ pip install Flask
Installation
Download the project
$ git clone git@gitlab.rc.uab.edu:louistw/get_start_flask.git # or your own fork
$ cd get_start_flask
Create and activate your virtualenv
$ virtualenv venv
$ source venv/bin/activate
Install packages needed via pip
$ pip install -r requirement.txt
Setup environment variables
$ export FLASK_ENV=development
$ export FLASK_APP=run.py
Setup your own configuration
$ cp instance/config.py.default instance/config.py
$ vim instance/config.py
Initialize you database
$ flask db init
$ flask db migrate
$ flask db upgrade
In case you are using SQLite, refer to the third problem in Known problem.
Create admin account
$ flask shell
from app.models import Employee
from app import db
#Remember to replace ADMIN_EMAIL, ADMIN_USERNAME, ADMIN_PASSWORD with correct value
admin = Employee(email="ADMIN_EMAIL",username="ADMIN_USERNAME",password="ADMIN_PASSWORD",is_admin=True)
db.session.add(admin)
db.session.commit()
Finally, you can run your app
$ flask run
Resources:
-
And might add some of the feature from The Flask Mega-Tutorial.
The following list is some of the features might be used for User Portal:
Known Problems
-
flask development mode WARNING
$ flask run * Serving Flask app "run.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off
Solution: Add environment variable
FLASK_ENV
and set it to development$ export FLASK_ENV=development
-
ValueError when click on Assign in employees admin page
file "APP_ROOT/venv/lib/python2.7/site-packages/wtforms/ext/sqlalchemy/fields.py", line 189, in get_pk_from_identity ValueError: too many values to unpack
Solution(for now): Modify line 189 in
fields.py
from188 def get_pk_from_identity(obj): 189 cls, key = identity_key(instance=obj) 190 return ':'.join(text_type(x) for x in key)
to
188 def get_pk_from_identity(obj): 189 cls, key = identity_key(instance=obj)[:2] 190 return ':'.join(text_type(x) for x in key)
-
When use SQLite as database
OperationalError: (sqlite3.OperationalError) no such table
Solution1: Move sqlite database file into app directory
$ mv app.db app/
Solution2(Preferred): Since the problem is because of relative path, using absolute path will solve it.
For instance:
SQLALCHEMY_DATABASE_URI='sqlite:////tmp/app.db'
-
FSADeprecationWarning while accessing database using SQLAlchemy
APP_ROOT/venv/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
Solution1: Set SQLALCHEMY_TRACK_MODIFICATIONS to False/True at line 789 in flask_sqlalchemy/__init__.py
788 track_modifications = app.config.setdefault( 789 'SQLALCHEMY_TRACK_MODIFICATIONS', False 790 )
Solution2(Preferred): Add
SQLALCHEMY_TRACK_MODIFICATIONS
inconfig.py
ecb8e620