在flask中可以像go和angular那样使用页面模版(template),可以将HTML页面显示进行模版化,通过参数传递与页面进行数据交互。
概要信息
项目 | 说明 |
---|---|
官方网站 | https://www.palletsprojects.com/p/flask/ |
开源/闭源 | 开源 |
License类别 | BSD License |
代码管理地址 | https://github.com/pallets/flask |
开发语言 | Python |
支持平台 | 鉴于python的跨平台特性,可运行于等多种操作系统 |
当前版本 | 1.0.2 (2018/05/02) |
事前准备:flask
liumiaocn:flask liumiao$ which flask
/usr/local/bin/flask
liumiaocn:flask liumiao$ flask --version
Flask 1.0.2
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
liumiaocn:flask liumiao$
代码示例:嵌入式的HTML模版
像Angular一样,我们可以在flask中写前端的页面,python代码中混杂着HTML代码,在这里将前面的HelloWorld示例进行简单的修改,将显示的Hello World加上
的设置。
代码示例
liumiaocn:flask liumiao$ cat flask_1.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1>Hello World!</h1>"
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$
执行&确认
在HelloWorld示例中我们提到有两种方式启动flask的微服务进程,这里再添加一种,添加#!/usr/bin/python之后,同时对此文件添加可执行权限比如755,即可使用.启动
liumiaocn:flask liumiao$ chmod 755 flask_1.py
liumiaocn:flask liumiao$ ./flask_1.py
* Serving Flask app "flask_1" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-533-062
通过curl进行结果确认:
liumiaocn:flask liumiao$ curl http://localhost:7000
<h1>Hello World!</h1>liumiaocn:flask liumiao$
页面确认
代码示例
上面的示例过于简单,写一个简单的完整的页面来确认一下
liumiaocn:flask liumiao$ cat flask_1.py
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return '<!DOCTYPE html> \
<html> \
<head> \
<meta charset="utf-8"> \
<title>Hello</title> \
</head> \
<body>\
<h1>Hello World!</h1> \
</body>\
</html>'
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$
执行&确认
通过curl可以确认页面范围信息
liumiaocn:flask liumiao$ ./flask_1.py
* Serving Flask app "flask_1" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-533-062
也可以通过浏览器来确认title和页面显示
页面模版
嵌在python的代码中非常的麻烦,转义的连接,以及源码的查看都非常不方便。flask提供了Jinja2的模版渲染,只需要引入render_template即可使用。
import render_template
为了使用这个功能,首先需要在程序中做如下import
from flask import render_template
准备页面信息
比如将上文中嵌入的HTML页面独立成index.html,详细信息如下:
liumiaocn:flask liumiao$ ls templates/
index.html
liumiaocn:flask liumiao$ cat templates/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Template</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
liumiaocn:flask liumiao$
注意事项:flask会在当前目录的templates下搜索对应的模版文件,所以需要创建templates文件夹,然后将模版html文件放入其中。
页面调用
在页面上只需要调用render_template即可实现url与对应模版的关联,
render_template(“index.html”)
详细代码
liumiaocn:flask liumiao$ cat flask_2.py
#!/usr/bin/python
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route("/")
def hello():
return render_template("index.html")
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$
执行&确认
liumiaocn:flask liumiao$ python flask_2.py
* Serving Flask app "flask_2" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-533-062
使用curl可以看到详细的html代码,而且读起来方便多了
liumiaocn:~ liumiao$ curl http://localhost:7000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Template</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>liumiaocn:~ liumiao$
也可以通过浏览器确认并查看源码
总结
使用render_template,flask也可以像angular一样非常方便的创建用于展示的模版视图,我们已经说过render_template是基于Jinja2的模版,在下一篇文章中将继续介绍template的数据交互和控制方式。