-
Notifications
You must be signed in to change notification settings - Fork 0
Large app how to
This document is an attempt to describe the first step of a large project structure with flask and some basic modules:
- SQLAlchemy
- WTF (What The Form)
Please feel free to fix and add you own tips.
[http://flask.pocoo.org/docs/installation/](Flask Installation) I recommend using virtual env: easy and allow multiple environment on the same machine and doesn't even require you to have super user right on the machine (as the libs are localy installed).
SQL provide an easy and advanced way to serialize your object to different type of relational database. In your virutal env, install SQLAlchemy from pip:
pip install flask-sqlalchemy
[http://packages.python.org/Flask-SQLAlchemy/](More here about SQL Alchemy flask package)
WTF (What the Form) Provides a easy way to handle user's data submission.
pip install Flask-WTF
[http://packages.python.org/Flask-WTF/](More here about What The Form flask package)
Ok, so from now, we should have all the libs ready. Here the folder structures:
config.py
run.py
shell.py
app.db
app/__init__.py
app/constants.py
For every module (or sub app... ) well have this file structure (here for the users module)
app/users/__init__.py
app/users/views.py
app/users/forms.py
app/users/constants.py
app/users/models.py
app/users/decorators.py
app/templates/users/login.html
app/templates/users/register.html
...
We'll create 4 modules, a user module (manage user's registration, login, password lost, profile edit and maybe Third party Login/Registration) an email sub module intended to be used by a queuing server, and a post and comments modules
run.py will be used to launch the web server.
from tol import app
app.run(debug=True)
shell.py will allow you to get a console and enter commands within your flask environment. Maybe not as nice as debugging with pdb, but always usefull (when you will initialize your database)
#!/usr/bin/env python
import os
import readline
from pprint import pprint
from flask import *
from app import *
os.environ['PYTHONINSPECT'] = 'True'
config.py will be storing all the module configurations. Here, the database is setup to use SQLite, because it's a very convenient dev env database. Most likely config.py won't be a part of your repository and will be different on your test and production servers.
import os
_basedir = os.path.abspath(os.path.dirname(__file__))
DEBUG = False
ADMINS = frozenset(['youremail@yourdomain.com'])
SECRET_KEY = 'SecretKeyForSessionSigning'
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(_basedir, 'app.db')
DATABASE_CONNECT_OPTIONS = {}
THREADS_PER_PAGE = 8
CSRF_ENABLED=True
CSRF_SESSION_KEY="somethingimpossibletoguess"
RECAPTCHA_USE_SSL = False
RECAPTCHA_PUBLIC_KEY = 'blahblahblahblahblahblahblahblahblah'
RECAPTCHA_PRIVATE_KEY = 'blahblahblahblahblahblahprivate'
RECAPTCHA_OPTIONS = {'theme': 'white'}
del os
-
_basediris a trick for you to get the folder where the script runs -
DEBUGindicate that it is a dev environment, you'll get the very helpful error page from flask when an error occur. -
SECRET_KEYwill be use to sign the cookies. Change it and all your user will have to login again. ADMINS-
SQLALCHEMY_DATABASE_URIandDATABASE_CONNECT_OPTIONSare SQLAlchemy connection options (hard to guess ) -
THREAD_PAGEmy understanding was 2/core... might be wrong :) -
CSRF_ENABLEDCSRF_SESSION_KEYis protecting against form post fraud - WTF comes with REPCAPTCHA field ready to use... just need to go to repcatcha website and get your public and private key.