Using Flask to create a full-featured webapp for serving data products
Two ways to run the flask app
FLASK_APP=app.py FLASK_DEBUG=1 flask run
python app.py
withapp.run()
inapp.py
The decorator @app.route('/<endpoint>')
is used to declare functions
that return html (or rendered templates) when the client requests an endpoint.
render_template
helps pass raw html or rendered Jinja templates.
- it accepts data that can be used to render the page
Use url_for
as far as possible to access resources under static/
for example
You can also pass the function name associated with a route.
Create a SECRET_KEY using secrets.token_hex(16)
from the secrets
library
and assign it to app.config
flask_sqlalchemy
lets you declare table schemas as classes
and work seamlessly with any database backend.
Then, we can fill these tables using db.session.add(<Object of table-class>)
The class can be used to query data as User.query.all()
or User.query.filter_by(<condition>).all()
For security, passwords must be hashed using the flask-bcrypt
library.
Bcrypt
objects have methods like generate_password_hash
and check_password_hash
that are useful.
Use flask-login
to manage user sessions
from flask import (
Flask,
render_template,
url_for,
flash,
redirect,
request
)
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import (
LoginManager,
UserMixin,
login_user,
current_user,
logout_user,
login_required
)
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import (
StringField,
PasswordField,
SubmitField,
BooleanFielda
)
from wtforms.validators import (
DataRequired,
Length,
Email,
EqualTo,
ValidationError
)