安装flask
创建一个flask虚拟环境
[root@shaoyu ~]# mkvirtualenv flask
进入flask虚拟环境并安装falsk
(flask) [root@shaoyu ~]# pip install flask #pip源如果是国外的,可能安装过程会很漫长,更换到国内pip源即可,另外一个因素受限于个人网络环境
#测试倒入flask是否成功
(flask) [root@shaoyu ~]# python
Python 3.6.10 (default, Jul 22 2020, 11:39:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> exit() #导入没问题
(flask) [root@shaoyu ~]#
测试flask
(flask) [root@shaoyu ~]# mkdir flask
(flask) [root@shaoyu ~]# vim flask/hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run()
运行hello.py
(flask) [root@shaoyu flask]# python hello.py
* Serving Flask app "hello" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
在另外一个终端中访问测试
[root@shaoyu ~]# elinks 127.0.0.1:5000 --dump
Hello World!
证明flask没问题
flask框架之路由规划
(flask) [root@shaoyu flask]# vim 01-hello.py
#-*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
@app.route("/api")
def index()
return "Index page"
@app.route("/api/hello")
def hello()
return "Hello World"
if __name__ == '__main__':
app.run()
#app.run( host='0.0.0.0' ) #监听所有端口,用于宿主机访问测试
运行01-hello.py
(flask) [root@shaoyu flask]# python 01-hello.py
在另一个终端中查看结果
[root@shaoyu ~]# elinks 127.0.0.1:5000 --dump
Hello World!
[root@shaoyu ~]# elinks 127.0.0.1:5000/api --dump
Index page
[root@shaoyu ~]# elinks 127.0.0.1:5000/api/hello --dump
Hello World
flask.register._blueprint方法
(flask) [root@shaoyu flask]# pwd
/root/flask
(flask) [root@shaoyu flask]# ls
01-hello.py hello.py
(flask) [root@shaoyu flask]# vim imooc.py
#-*- coding:utf-8 -*-
from flask import Blueprint
route_imooc = Blueprint( "imooc_page", __name__ )
@route_imooc.route('/')
def index():
return "imooc index page"
@route_imooc.route('/hello')
def hello():
return "imooc hello world"
(flask) [root@shaoyu flask]# cp 01-hello.py 02-hello-imooc.py
(flask) [root@shaoyu flask]# vim 02-hello-imooc.py
(flask) [root@shaoyu flask]# cat 02-hello-imooc.py
#-*- coding:utf-8 -*-
from flask import Flask
from imooc import route_imooc
app = Flask(__name__)
app.register_blueprint( route_imooc, url_prefix = '/imooc' )
@app.route('/')
def hello_world():
return "Hello World!"
@app.route("/api")
def index():
return "Index page"
@app.route("/api/hello")
def hello():
return "Hello World"
if __name__ == '__main__':
app.run()
#app.run( host='0.0.0.0' ) #监听所有端口,用于宿主机访问测试
(flask) [root@shaoyu flask]# ls
01-hello.py 02-hello-imooc.py hello.py imooc.py
运行02-hello-imooc.py
(flask) [root@shaoyu flask]# python 02-hello-imooc.py
* Serving Flask app "02-hello-imooc" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
在另外一个终端中测试
[root@shaoyu ~]# elinks 127.0.0.1:5000 --dump
Hello World!
[root@shaoyu ~]# elinks 127.0.0.1:5000/imooc/hello --dump
imooc hello world
[root@shaoyu ~]# elinks 127.0.0.1:5000/imooc/ --dump
imooc index page
flask之链接管理器(url_for)和版本管理器
链接管理器url_for
(flask) [root@shaoyu flask]# cp 02-hello-imooc.py 03-url_for.py
(flask) [root@shaoyu flask]# vim 03-url_for.py
#-*- coding:utf-8 -*-
from fl