Python-Web框架flask使用示例教程
1.Web框架
1.1 flask
python的web框架,目錄結構如下:
1.static存放的是css,js的樣式文件
2.templates存放的是html文件
3.app.py是主要接口入口,默認創(chuàng)建就有如下代碼:
from flask import Flask app = Flask(__name__) #這是兩條短線 # 路由解析,通過用戶訪問的路徑,匹配相應的函數(shù) @app.route('/') def hello_world(): return 'hello world' if __name__ =='__main__': # 啟動Flask框架 # debug=True 開啟debug模式,在調(diào)式階段開啟debug不止更改東西不用重啟,還可以出現(xiàn)錯誤,界面直接給你展現(xiàn)錯誤原因 app.run()
右鍵啟動以后,則控制臺有以下信息
點擊藍色鏈接以后則瀏覽器打開訪問web,其實就是flask框架在啟動監(jiān)聽,默認給的端口是5000
1.1.1 debug調(diào)試
在沒有上線階段,最好用debug模式,默認debug模式是關閉的,如控制臺會寫Debug mode:off,開啟debug則需要在代碼app.run里寫debug=True,我這里如下圖片為on,表示啟動成功了,有的環(huán)境會debug不成功,是不同環(huán)境的原因,可以采用其他方式更改。
debug還有一個好處就是后臺業(yè)務出現(xiàn)問題,可以在前臺頁面展示
比如我更改如下代碼,輸出name,但是name變量沒有定義:
刷新界面:它會告訴你什么錯誤,也會告知錯誤在哪里,所以初學者一定在調(diào)試時使用debug模式,能更方便定義問題。
debug還可以不重新啟動更改業(yè)務直接界面刷新就會不一樣,在代碼更改nihao,如
# 路由解析,通過用戶訪問的路徑,匹配相應的函數(shù) @app.route('/') def hello_world(): return 'nihao'
自己定義一個web服務:
@app.route('/test') def mappingTest(): return 'test app'
訪問這個web服務,test
1.1.2 定義參數(shù)web服務 獲取字符串
1.獲取字符串,通過<> 后臺能夠獲取內(nèi)容,多個則/paramTest//,paramTest(name,age)
# 通過訪問路徑,獲取用戶的字符串參數(shù) @app.route('/paramTest/<name>') def paramTest(name): return 'test app,%s'%name
訪問結果
需要定義<int:paramname>則代表接收int型數(shù)據(jù),還可folat類型,則<folat:參數(shù)>
@app.route('/intTest/<int:id>') def intTest(id): return 'test app,%d'%id
結果:
1.1.3 html網(wǎng)頁渲染
咱們返回直接返回內(nèi)容了,如果直接返回網(wǎng)頁呢,需要導入包為render_template,通過render_template("index.html")直接返回html網(wǎng)頁內(nèi)容,前提是需要創(chuàng)建html
from flask import Flask, render_template # 訪問網(wǎng)頁 @app.route('/') def index(): return render_template("index.html")
在templates目錄下創(chuàng)建一個index.html,自動就有這些結構,在body下加你好啊幾個字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 你好啊 </body> </html>
網(wǎng)頁刷新
1.13.1 帶參數(shù)傳給網(wǎng)頁文件 普通元素
1.普通元素,通過定義var把time 傳給html文件里
import datetime #向頁面?zhèn)鬟f一個變量,html接收以{{定義的變量名來使用}} @app.route('/') def index(): time=datetime.date.today() #普通變量 return render_template("index.html",var=time)
在html里使用則需要{{變量名}}接收
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 時間是:{{var}},你好啊 </body> </html>
界面展示:
列表元素
2.列表元素,當然這個list可以隨便起名字,只要html對的上
@app.route('/') def index(): time = datetime.date.today() # 普通元素 names = ["哈哈", "喜喜", "dudu"] # 列表元素 return render_template("index.html", var=time, list=names)
html里循環(huán)list這樣使用:{% %}代表循環(huán)的開始,{%endfor%}代表循環(huán)結束,這個語法是jinga2方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 時間是:{{var}},你好啊<br> 跟您匹配的朋友有:<br> {% for data in list %} {{data}} {% endfor %} </body> </html>
結果:
字典元素
定義task字典元素,看看html怎么操作
@app.route('/') def index(): time = datetime.date.today() # 普通元素 names = ["哈哈", "喜喜", "dudu"] # 列表元素 # html里循環(huán)names這樣使用,{% %}代表循環(huán)的開始,{%endfor%}代表循環(huán)結束 task={"task":"學習","time":"2小時"} # 字典元素 return render_template("index.html", var=time, list=names,task=task)
這里用了table添加了兩行表格,表示行,表示列,第一行則是key鍵名稱所以循環(huán)得到key,第二行則為值,取到value
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 時間是:{{var}},你好啊<br> 跟您匹配的朋友有:<br> {% for data in list %} <li> {{data}}</li> {% endfor %} 任務:<br> <table border="1"> <tr> {% for key,value in task.items() %} <td>{{key}}</td> {% endfor %} </tr> <tr> {% for key,value in task.items() %} <td>{{value}}</td> {% endfor %} </tr> </table> </body> </html>
運行結果
1.13.2 input表單提交
我們添加一個inputform接口,并返回一個register的html網(wǎng)頁,這個表單網(wǎng)頁數(shù)據(jù)數(shù)據(jù)并提交就調(diào)用result接口,result接口里接收到頁面的表單信息并返回。
from flask import Flask, render_template,request # 訪問此接口返回表單頁面 @app.route('/inputform') def inputform(): return render_template("register.html") # input表單界面提交時進入的方法,方法方式為post @app.route('/result',methods=['post']) def result(): # 通過request獲取用戶表單數(shù)據(jù),request.form返回的是字典信息 if request.method=='POST': result = request.form return result
register.html網(wǎng)頁內(nèi)容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- action里最好不要寫死地址,可以采用url_for使用,自動幫你找到后臺方法,注意路由名和方法名一致,防止找不到 --> <form action="{{ url_for('result') }}" method="post"> <p>姓名:<input type="text" name="name"></p> <p>性別:<input type="text" name="gender"></p> <p>年齡:<input type="text" name="age"></p> <p>地址:<input type="text" name="address"></p> <p><input type="submit"></p> </form> </body> </html>
啟動并訪問如下界面內(nèi)容:
輸入完數(shù)據(jù)點擊提交,就會進入后臺的result接口,后臺接口就會拿到表單數(shù)據(jù),
到此這篇關于Python-Web框架flask使用的文章就介紹到這了,更多相關Python-Web框架flask內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python continue繼續(xù)循環(huán)用法總結
本篇文章給大家總結了關于Python continue繼續(xù)循環(huán)的相關知識點以及用法,有需要的朋友跟著學習下吧。2018-06-06TensorFlow人工智能學習數(shù)據(jù)合并分割統(tǒng)計示例詳解
這篇文章主要為大家介紹了TensorFlow人工智能學習數(shù)據(jù)合并分割及統(tǒng)計的示例詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11python pandas.DataFrame.loc函數(shù)使用詳解
這篇文章主要介紹了python pandas.DataFrame.loc函數(shù)使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Python 3.x基于Xml數(shù)據(jù)的Http請求方法
今天小編就為大家分享一篇Python 3.x基于Xml數(shù)據(jù)的Http請求方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python解析Excle文件中的數(shù)據(jù)方法
今天小編就為大家分享一篇Python解析Excle文件中的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python簡單實現(xiàn)兩個任意字符串乘積的方法示例
這篇文章主要介紹了Python簡單實現(xiàn)兩個任意字符串乘積的方法,結合實例形式分析了Python針對字符串、列表的切片、轉換、遍歷等相關操作技巧,需要的朋友可以參考下2018-04-04