Flask (Pallets Project)
CLI
## Set
export FLASK_APP=hello.py
# $env.FLASK_APP=hello.py
## Debug mode
export FLASK_ENV=development
## Run
flask run # python -m flask runbash
Views
A function to respond to requests.
("/hello")
def hello():
return "Hello!"python
Valid response:
-
string -
jsonfrom flask import jsonify def hello(): return jsonify({"time": time.time()})python -
render_template()def hello(): return render_template("hello.html")python
Blueprint
A way to organize a group of views. (Modularization)
from flask import Blueprint, request, redirect, url_for
bp = Blueprint('auth', __name__, url_prefix='/auth')
# register: app.register_blueprint(bp)
# add views
('/register', methods=('GET', 'POST'))
def register():
# request is imported
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
...
return redirect(...)
#elif request.method == 'GET':
return render_template(...)
('/login', methods=('GET', 'POST'))
def login():
pass
# other utils
def login_required(view):
(view)
def wrapped_view(**kwargs):
if g.user is None:
return redirect(url_for('auth.login'))
return view(**kwargs)
return wrapped_viewpython
Context
**How flask handle request: **
- Create APP Context
- Create Request Context
- Do some Logic
- Destroy Request Context
- Destroy APP Context
Difference:
APP context is cheap, and can be created without a request, for accessing current_app and g.
Request context is expensive.
URL arguments
-
simple arguments
fetch("api/a/1");js("api/<arg1>/<arg2>") def func(arg1, args): return jsonify({ 'arg1': arg1, 'arg2': arg2, })python -
form
let formData = new FormData(); formData.append('username', 'John'); formData.append('password', '123'); fetch("api/form", { method: "POST", body: formData, });javascript("api/form", methods=('GET','POST')) def login(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password')python -
URL parameters
fetch("login?username=alex&password=pw1")javascript("login", methods=("GET",)) def login(): username = request.args.get('username') password = request.args.get('password')python -
JSONfetch("test", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({name:"Alex"}) })javascript("test", methods=('GET','POST')) def login(): if request.method == 'POST': username = request.json.get('username') password = request.json.get('password')python -
filesrequest.filespython
flask.session
Store data in different requests from the same client. (signed cookies)
It is like a dict.
session['user_id'] = user['id']
session.clear()python
flask.g
Store data in the same request.
It works like a dict.
g.user = user
if g.user is not None:
passpython
Usual way to manage resource:
def get_db():
if 'db' not in g:
g.db = connect_to_database()
return g.db
def teardown_db():
db = g.pop('db', None)
if db is not None:
db.close()python
Jinja
A template library.
{% ... %}for Statements{{ ... }}for Expressions to print to the template output{# ... #}for Comments not included in the template output# ... ##for Line Statements