Python編程flask使用頁面模版的方法
在flask中可以像go和angular那樣使用頁面模版(template),可以將HTML頁面顯示進(jìn)行模版化,通過參數(shù)傳遞與頁面進(jìn)行數(shù)據(jù)交互。
概要信息

事前準(zhǔn)備: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示例進(jìn)行簡單的修改,將顯示的Hello World加上的設(shè)置。
代碼示例
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$
執(zhí)行&確認(rèn)
在HelloWorld示例中我們提到有兩種方式啟動(dòng)flask的微服務(wù)進(jìn)程,這里再添加一種,添加#!/usr/bin/python之后,同時(shí)對(duì)此文件添加可執(zhí)行權(quán)限比如755,即可使用.啟動(dòng)
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進(jìn)行結(jié)果確認(rèn):
liumiaocn:flask liumiao$ curl http://localhost:7000 <h1>Hello World!</h1>liumiaocn:flask liumiao$
頁面確認(rèn)

代碼示例
上面的示例過于簡單,寫一個(gè)簡單的完整的頁面來確認(rèn)一下
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$
執(zhí)行&確認(rèn)
通過curl可以確認(rèn)頁面范圍信息
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
也可以通過瀏覽器來確認(rèn)title和頁面顯示

頁面模版
嵌在python的代碼中非常的麻煩,轉(zhuǎn)義的連接,以及源碼的查看都非常不方便。flask提供了Jinja2的模版渲染,只需要引入render_template即可使用。
import render_template
為了使用這個(gè)功能,首先需要在程序中做如下import
from flask import render_template
準(zhǔn)備頁面信息
比如將上文中嵌入的HTML頁面獨(dú)立成index.html,詳細(xì)信息如下:
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$
注意事項(xiàng):flask會(huì)在當(dāng)前目錄的templates下搜索對(duì)應(yīng)的模版文件,所以需要?jiǎng)?chuàng)建templates文件夾,然后將模版html文件放入其中。
頁面調(diào)用
在頁面上只需要調(diào)用render_template即可實(shí)現(xiàn)url與對(duì)應(yīng)模版的關(guān)聯(lián),
render_template(“index.html”)
詳細(xì)代碼
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$
執(zhí)行&確認(rèn)
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可以看到詳細(xì)的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$
也可以通過瀏覽器確認(rèn)并查看源碼

小結(jié)
使用render_template,flask也可以像angular一樣非常方便的創(chuàng)建用于展示的模版視圖,我們已經(jīng)說過render_template是基于Jinja2的模版,在下一篇文章中將繼續(xù)介紹template的數(shù)據(jù)交互和控制方式。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
pytorch 數(shù)據(jù)加載性能對(duì)比分析
這篇文章主要介紹了pytorch 數(shù)據(jù)加載性能對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
windows上徹底刪除jupyter notebook的實(shí)現(xiàn)
這篇文章主要介紹了windows上徹底刪除jupyter notebook的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
使用python+Flask實(shí)現(xiàn)日志在web網(wǎng)頁實(shí)時(shí)更新顯示
日志是一種可以追蹤某些軟件運(yùn)行時(shí)所發(fā)生事件的方法,下面這篇文章主要給大家介紹了關(guān)于使用python+Flask實(shí)現(xiàn)日志在web網(wǎng)頁實(shí)時(shí)更新顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
用Python的線程來解決生產(chǎn)者消費(fèi)問題的示例
這篇文章主要介紹了用Python的線程來解決生產(chǎn)者消費(fèi)問題的示例,包括對(duì)使用線程中容易出現(xiàn)的一些問題給出了相關(guān)解答,需要的朋友可以參考下2015-04-04
numpy使用技巧之?dāng)?shù)組過濾實(shí)例代碼
這篇文章主要介紹了numpy使用技巧之?dāng)?shù)組過濾實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
詳解利用python-highcharts庫繪制交互式可視化圖表
本文主要和大家分享一個(gè)超強(qiáng)交互式可視化繪制工具-python-highcharts。python-highcharts就是使用Python進(jìn)行Highcharts項(xiàng)目繪制,簡單的說就是實(shí)現(xiàn)Python和Javascript之間的簡單轉(zhuǎn)換層,感興趣的可以了解一下2022-03-03
Python函數(shù)之zip函數(shù)的介紹與實(shí)際應(yīng)用
zip() 函數(shù)用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)元組,然后返回由這些元組組成的對(duì)象(python2 返回的是這些元組組成的列表 ),下面這篇文章主要給大家介紹了關(guān)于Python函數(shù)之zip函數(shù)實(shí)際應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-03-03
Python3.5 Pandas模塊之DataFrame用法實(shí)例分析
這篇文章主要介紹了Python3.5 Pandas模塊之DataFrame用法,結(jié)合實(shí)例形式詳細(xì)分析了Python3.5中Pandas模塊的DataFrame結(jié)構(gòu)創(chuàng)建、讀取、過濾、獲取等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-04-04

