Flask
flask
- 概念:flask ‘微’框架
- django –> 完善完整高集成的框架
- flask –> Flask 不包含数据库抽象层微框架,database,templetes需要自己去组装
安装
- 创建虚拟环境virtualenv –no-site-packages flaskenv
- cd flaskenv
- cd Script
- 启动虚拟环境:activate
- pip install flask
运行flask
- python xxx.py —> 启动默认127.0.0.1:5000端口
运行参数
- debug = True 调试
- port = ‘8000’ 端口
- host = ‘0.0.0.0’ IP
修改启动方式
- pip install flask-script
- python hello.py runserver -p 端口 -h IP -d 表示开启debug
配置debug
- 在工具栏Run下,选择debug,添加python
- 找到对应的文件,runserver配置如下

在html中导入静态文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/hello.css">
</head>
<body>
<p>欢迎</p>
</body>
</html>
from flask import Flask
from flask_script import Manager
app = Flask(__name__)
manager = Manager(app=app)
@app.route('/')
def hello():
return 'Hello World'
@app.route('/hello/<name>/')
def hello_man(name):
print(name)
print(type(name))
return 'hello name: %s ' % name
@app.route('/helloint/<int:id>/')
def hello_int(id):
print(id)
print(type(id))
return 'hello int %d' % id
if __name__ == '__main__':
manager.run()
实现VMC模式
- 具体文件结构如下

- 导入蓝图
- pip install blueprint
创建manage.py文件
from flask_script import Manager
from App import create_app
app = create_app()
manager = Manager(app=app)
if __name__ == '__main__':
manager.run()
APP
- 在主项目目录下创建app文件夹
- 设置初始化文件init.py文件
- 里面注册路由
from flask import Flask
from App.views import blue
def create_app():
app = Flask(__name__)
app.register_blueprint(blueprint=blue)
return app
views.py文件
import uuid
from flask import render_template
from flask import send_file
from flask import Blueprint
blue = Blueprint('frist', __name__)
@blue.route('/', methods=['POST', 'GET'])
def hello():
return 'Hello World'
@blue.route('/hello/<name>/')
def hello_man(name):
print(name)
print(type(name))
return 'hello name: %s ' % name
@blue.route('/helloint/<int:id>/')
def hello_int(id):
print(id)
print(type(id))
return 'hello int %d' % id
@blue.route('/getfloat/<float:price>/')
def hello_float(price):
return 'float: %s' % price
@blue.route('/getpath/<path:url_path>')
def hello_path(url_path):
return 'path: %s' % url_path
@blue.route('/getuuid/')
def getuuid():
a = uuid.uuid4()
return str(a)
@blue.route('/getbyuuid/<uuid:uu>/')
def hello_uuid(uu):
return 'uuid: %s' % uu
@blue.route('/index/')
def index():
return send_file('../templates/hello.html')