A simple web app showcasing flask with back-end SQLite database integration.
Catalog App will work in Python 2 and Python 3. But the optional data faking functions in
module catalog/populate.py will not work in Python 2.
- Flask v0.12
- Flask-WTF v0.14
- Flask-Restless v0.17
- SQLAlchemy v1.1
- Requests-Oauthlib v0.8
- Mimesis v1.0 (requires Python 3)
-
➜ git clone https://github.com/klazich/catalog.git project ➜ cd project -
➜ pip install Flask Flask-WTF Flask-Restless SQLAlchemy requests_oauthlibIf you are using Python 3 and want to use the apps database populating features, install mimesis as well...
➜ pip3 install Flask Flask-WTF Flask-Restless SQLAlchemy requests_oauthlib mimesisor with requirements.txt...
➜ pip install -r requirements.txt -
The flask app will load the "development" configs by default (see config.py). The development config turns on debug. Load different flask configs by setting the
FLASK_CONFIGenvironment variable todev,test, orprod.➜ export FLASK_CONFIG=prodor with a python script...
>>> import os >>> os.environ['FLASK_CONFIG'] = 'prod' # set Flask to load production configurationsto start the server enter...
➜ python run.pythen open up a browser to http://localhost:5000/
Using Mimesis and helper functions from
catalog/populate.py we can seed the database with fake data. This is
useful for testing as well as demonstration purposes.
catalog.populaterequires Python 3 or later. If you try to import the module with Python 2 the app will raise aAssertionError. The populate functions require the Mimesis package and Mimesis will fail on import with Python 2.
➜ python3
>>> from catalog.populate import populate_db
>>> populate_db()
Note - Be aware that
populate_dbwill calldrop_dbwhich will drop all the database tables.
populate_db()will create the Item, Category and User tables (catalog/models.py) and populate them with simulated data using the other function is the database module.
➜ python3
>>> from catalog.populate import init_db, drop_db
>>> init_db() # to create database tables
>>> drop_db() # to drop all tables in database
init_db()will create all tables found in the metadata if they are not already created.drop_db()will drop all tables in the database.
➜ python3
>>> from catalog.populate import populate_users, populate_categories, populate_items
>>> populate_users(10) # add 10 User objects to database
>>> populate_categories() # add Category objects to database
>>> populate_items(90) # add 90 Item objects to database
populate_users(n)will create and commitnusers to the database (defaults ton=100).populate_categories()will create and commit the simulated categories.populate_items(n)will create and commitnitems to the database (defaults ton=600).
| Request URL Path | View Function/Flask Extension | |
|---|---|---|
/catalog |
catalog.index() | Renders site index, listing categories |
/auth/login |
auth.login() | Renders the login page |
/auth/logout |
auth.logout() | Logs out user and redirects to referrer |
/auth/{provider} |
auth.oauth2_authorize(provider) | Initiates user authentication request to provider OAuth2 service |
/auth/callback |
auth.oauth2_callback() | Handles the callback from provider OAuth2 service and redirects to auth.login referrer |
/category/{id} |
category.read(id) | Renders a list of items from category with id (extends catalog.index) |
/item/new |
item.create() | Renders the ItemForm for item creation |
/item/{id} |
item.read(id) | Renders a summary of an item with id (extends category.read) |
/item/{id}/update |
item.update(id) | Renders the ItemForm for item with id updates |
/item/{id}/delete |
item.delete(id) | Removes item with id from database |
/api/categories |
Flask-Restless | Returns a list of all category objects in JSON format |
/api/categories/{id} |
Flask-Restless | Returns an individual category object with id in JSON format |
/api/items |
Flask-Restless | Returns a list of all item objects in JSON format |
/api/items/{id} |
Flask-Restless | Returns an individual item object with id in JSON format |
- Move the populate database functions into a cli.