python程式設計基礎:快速微服務框架指南:flask:2: 使用頁面模版
在flask中可以像go和angular那樣使用頁面模版(template),可以將HTML頁面顯示進行模版化,通過引數傳遞與頁面進行資料互動。
概要資訊
事前準備: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的資料互動和控制方式。