1. Why Flask?
- 官方文档详细,学习成本低,有利于项目其他成员迅速投入开发
- 框架轻,可扩展性高,且有很多好用的扩展(extension)
- 该框架仍在积极地维护和开发
2. 将要介绍到的Flask的扩展
- flask blueprint 用于注册、管理路由
- flask_restful 用于支持构建REST APIs
- flask_migrate 用于管理数据库版本迁移
- flask_sqlalchemy orm, 用于映射数据库对象,提供数据操作方法
3. 项目结构
- project
- user
- helpers.py
- views.py
- migrations
- __init__.py
- blueprint.py
- bootstrap.py
- models.py
路径 | 功能 |
---|---|
user/ | 资源或者模块,此处为用户模块 |
migrations | 数据库迁移文件 |
user/views.py | 处理对资源的请求(如GET,POST) |
user/helpers.py | 模块涉及到的辅助函数 |
__init__.py
|
初始化Flask app |
blueprint.py | 路由 |
bootstrap.py | app的启动 |
models.py | 数据库实体的对象 |
4. 关键代码
初始化Flask app
# __init__.py
from
flask
import
Flask
app
=
Flask
(
__name__
)
路由注册
使用两种包
- flask blueprint 普通的路由,一个函数对应于一个api请求
# user/views.py
@blueprint
.
route
(
'/password'
,
methods
=
[
'PUT'
]
)
def
update_password
(
)
:
form
=
request
.
get_json
(
True
,
True
)
user_id
=
auth_helper
(
)
new_pass
=
form
.
get
(
"password"
)
if
not
new_pass
:
return
jsonify
(
error
=
"密码不能为空"
)
,
400
new_pass
=
encrypt_helper
(
new_pass
)
User
.
patch
(
user_id
=
user_id
,
password
=
new_pass
)
cookie
=
request
.
cookies
session_id
=
cookie
.
get
(
'fat-wallet'
)
session
.
pop
(
session_id
)
return
jsonify
(
data
=
"ok"
)
,
200
- flask_restful 单位为资源,支持GET, POST等http方法
# user/views.py
class
UserResource
(
Resource
)
:
def
get
(
self
)
:
student_id
=
request
.
args
.
get
(
"student_id"
)
username
=
request
.
args
.
get
(
"username"
)
offset
=
request
.
args
.
get
(
"offset"
)
limit
=
request
.
args
.
get
(
"limit"
)
users
=
User
.
get
(
student_id
=
student_id
,
username
=
username
,
offset
=
offset
,
limit
=
limit
)
result
=
[
{
"user_id"
:
user
.
id
,
"student_id"
:
user
.
student_id
,
"username"
:
user
.
username
,
"major"
:
user
.
major
,
"email"
:
user
.
email
,
"phone"
:
user
.
phone
,
"avatar"
:
user
.
avatar
.
decode
(
)
if
user
.
avatar
else
None
}
for
user
in
users
]
return
dict
(
data
=
result
,
count
=
len
(
result
)
)
,
200
为app注册以上两种路由
# blueprint.py
from
backend
.
user
.
views
import
blueprint
as
user_blueprint
from
backend
.
user
.
views
import
UserResource
def
setup
(
app
)
:
api
=
Api
(
app
)
app
.
register_blueprint
(
user_blueprint
,
url_prefix
=
'/users'
)
api
.
add_resource
(
UserResource
,
'/users/'
)
定义映射数据库的对象,通过对象操作数据
# models.py
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'mysql+pymysql://root:admin@mysql/money'
db
=
SQLAlchemy
(
app
)
migrate
=
Migrate
(
app
,
db
)
class
MyMixin
(
object
)
:
__table_args__
=
{
'mysql_engine'
:
'InnoDB'
,
'mysql_charset'
:
'utf8mb4'
,
}
class
User
(
db
.
Model
,
MyMixin
)
:
__tablename__
=
'users'
id
=
db
.
Column
(
BIGINT
(
unsigned
=
True
)
,
primary_key
=
True
)
student_id
=
db
.
Column
(
db
.
String
(
10
)
,
unique
=
True
)
username
=
db
.
Column
(
db
.
String
(
20
)
,
unique
=
True
,
nullable
=
False
)
password
=
db
.
Column
(
db
.
String
(
40
)
,
nullable
=
False
)
major
=
db
.
Column
(
db
.
String
(
20
)
)
email
=
db
.
Column
(
db
.
String
(
30
)
,
unique
=
True
)
phone
=
db
.
Column
(
db
.
String
(
20
)
,
unique
=
True
)
avatar
=
db
.
Column
(
db
.
LargeBinary
(
2
**
21
-
1
)
)
# 2M
# 通过model操作数据
user
=
User
(
username
=
username
,
password
=
pass_md5
,
email
=
email
)
db
.
session
.
add
(
user
)
db
.
session
.
commit
(
)
使用flask_migrate管理数据的迁移
flask db init
# 初始化(生成migrations文件夹)
flask db migrate
-
m
"msg"
# 根据当前的model生成迁移文件, 可添加信息
flask db upgrade
id
# 根据生成的迁移文件更新数据库表结构, id为指定的迁移版本,可以为head,表示最新的
flask db history
# 查看迁移历史
运行项目
# debug mode
flask run
-
-
host
=
0.0
.0
.0
Github项目地址