flask使用总结

一. flask做restfulapi,使用sqlachemy连接查询数据库,marshmallow解析数据,flask-restful响应

1. flask-sqlachemy需要模型类,根据已有的数据表生成models.py可以使用flask-sqlacodegen

https://www.tonythorsen.com/generating-flask-sqlalchemy-models-with-flask-sqlacodegen/

安装:pip install flask-sqlacodegen

生成models.py

flask-sqlacodegen --flask --outfile models.py mysql+pymysql://root:root@localhost:3306/test

参数 --table  生成指定数据表的 model

2.Flask-Marshmallow

sqlachemy使用model查询的数据是模型对象,python中不能直接使用,需要解析

https://flask-marshmallow.readthedocs.io/en/latest/

sqlalchemy 返回数据,转换成JSON格式

文档:marshmallow 文档

https://www.jianshu.com/p/594865f0681b

 

Flask-marshmallow 实现RESTFUL 规范

https://blog.csdn.net/qq_36019490/article/details/98101611

 

3. flask-restful

 用restful api进行路由的注册

 

4.Flask-JWT 前端保存token,后端验证是否登录 

安装:https://flask-jwt-extended.readthedocs.io/en/stable/

pip install flask-jwt-extended

 

二. 开发环境和生产环境部署

1.开发环境使用pipenv, install会自动查找依赖,并创建 pipfile, pipenv install --dev 开发环境创建虚拟环境,安装开发环境的依赖

> python -m pip install pipenv
> python -m pipenv install 创建虚拟环境,安装依赖
> python -m pipenv shell

启动可以使用### 启动本地服务器命令

* 在当前目录下运行   
> python run.py
* 或者设置环境变量
> set FLASK_APP=run.py
> set FLASK_ENV=development
> flask run

 

2.生产环境,激活虚拟环境,使用uwsgi,依赖全在虚拟环境中安装

如果不需要nginx代理,可以直接启动 uswgi作为http服务器 

2. uwsgi启动, 监听端口5000,代码修改后自动重载(如果有nginxuwsgi之前作为代理的话应该配socket   如:  socket=127.0.0.1:5001)
> cd /www/pre/app
> python -m pipenv shell
> python -m pipenv install #创建虚拟环境,安装依赖
> python -m pipenv install uwsgi
> python -m pipenv install uwsgitop
> python -m pipenv shell
> uwsgi --http 127.0.0.1:5000 --daemonize /var/log/uwsgi.log --wsgi-file run.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191 --py-autoreload 1

3. 关闭uwsgi
> killall -s INT /root/.local/share/virtualenvs/app-FFKDacNo/bin/uwsgi

4. 查看监控
> uwsgitop 127.0.0.1:9191

5. 查看uwsgi日志
> cat /var/log/uwsgi.log

 

 

三:修改源码,上传中文名称的文件报错,可以修改源码解码方式来解决

### flask 问题
上传文件,中文名有问题
```
修改D:\ProgramFiles\Python37\Lib\site-packages\werkzeug\utils.py
if isinstance(filename, text_type):
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('utf-8', 'ignore') # 转码
if not PY2:
filename = filename.decode('utf-8') # 解码
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')

# myself define
# 正则增加对汉字的过滤
# \u4E00-\u9FBF 中文
#构建新正则

_filename_ascii_add_strip_re = re.compile(r'[^A-Za-z0-9_\u4E00-\u9FBF.-]')

# 使用正则
filename = str(_filename_ascii_add_strip_re.sub('', '_'.join(
filename.split()))).strip('._')
```

四 decimal数据json问题, 响应decimal数据的方法

1. 使用flask的jsonify方法, 安装simplejson, 这样响应decimal数据

2. json.dumps的时候decimal问题,

 

class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
super(DecimalEncoder, self).default(o)
test = {}
test['a'] = 1.1

print(json.dumps(test, cls=DecimalEncoder, ensure_ascii=False))
3.decimal和int或float加减问题,转为decimal再加减
from decimal import Decimal
money = Decimal(data.get('money'))
 

 

 

posted @ 2020-09-15 12:09  jackduan1  阅读(265)  评论(0编辑  收藏  举报