1 Flask
1.1 認(rèn)識(shí)Flask
Web Application Framework
(Web
應(yīng)用程序框架)或簡(jiǎn)單的Web Framework
(Web
框架)表示一個(gè)庫(kù)和模塊的集合,使Web
應(yīng)用程序開(kāi)發(fā)人員能夠編寫(xiě)應(yīng)用程序,而不必?fù)?dān)心協(xié)議,線程管理等低級(jí)細(xì)節(jié)。
1.2 Pycharm安裝與簡(jiǎn)單測(cè)試
1.2.1 安裝
Pycharm
安裝Flask
框架File
→Settings
→Project: [project name]
→Project Interpreter
1.2.2 簡(jiǎn)單測(cè)試
運(yùn)行下面代碼,打開(kāi)http://127.0.0.1:5000
的鏈接
from flask import Flask
# __name__:代表當(dāng)前模塊,app為類(lèi)的實(shí)例
app = Flask(__name__)
# 創(chuàng)建一個(gè)路由和視圖函數(shù)的映射
@app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
#app.run(host='0.0.0.0', port=5000)
1.2.3 Debug模式(熱更新)
Debug
模式從控制臺(tái)可以看見(jiàn)Pycharm
專(zhuān)業(yè)版開(kāi)啟方法:
右上角的項(xiàng)目名稱(chēng) →
Edit Configurations
→ 勾選FLASK_DEBUG
選項(xiàng) → 重啟項(xiàng)目
Pycharm
社區(qū)版開(kāi)啟方法:
# 開(kāi)啟Debug模式 運(yùn)行時(shí)傳遞參數(shù)
app.run(debug=True)
1.2.4 社區(qū)版Pycharm建立Flask Project
文件夾 | 作用 |
---|---|
static |
存放靜態(tài)文件 |
templates |
存放模板文件 |
2 Flask模塊的語(yǔ)法與使用
2.1 Flask路由與路由參數(shù)
2.1.1 路由
Flask
中的route()
裝飾器用于將URL
綁定到函數(shù),下面代碼運(yùn)行在http://127.0.0.1:5000/hello
@app.route('/hello')
def hello_world():
return 'hello world'
application
對(duì)象的 a dd_url_rule()
函數(shù)也可用于將 URL
與函數(shù)綁定
from flask import Flask
app = Flask(__name__)
def hello_world():
return 'hello world'
app.add_url_rule('/', 'hello', hello_world)
app.run()
2.1.2 路由參數(shù)(動(dòng)態(tài)構(gòu)建UrL)
通過(guò)向規(guī)則參數(shù)添加變量部分,可以動(dòng)態(tài)構(gòu)建
URL
。
此變量部分標(biāo)記為<variable-name>
。
它作為關(guān)鍵字參數(shù)傳遞給與規(guī)則相關(guān)聯(lián)的函數(shù)。
from flask import Flask
app = Flask(__name__)
@app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
@app.route('/blog/<int:postID>')
def show_blog(postID):
return 'Blog Number %d' % postID
@app.route('/rev/<float:revNo>')
def revision(revNo):
return 'Revision Number %f' % revNo
if __name__ == '__main__':
app.run(debug = True)
2.1.3 URL構(gòu)建
url_for()
函數(shù)用于動(dòng)態(tài)構(gòu)建特定函數(shù)的URL
語(yǔ)法
url_for(函數(shù)名,關(guān)鍵字參數(shù))
舉例:
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/world')
def hello_world():
return 'Hello world?。?!'
@app.route('/test/<str>')
def hello_test(str):
return '%s !!!' % str
@app.route('/other/<oth>')
def hello_other(oth):
if oth =='world':
return redirect(url_for('hello_world'))
else:
return redirect(url_for('hello_test', str= '隨便拉'))
if __name__ == '__main__':
app.run(debug = True)
代碼解析:
在postman輸入http://127.0.0.1:5000/other/world網(wǎng)址,如果查接收的參數(shù)與world匹配,則重定向hello_world()函數(shù)
否則:
重定向到hello_test()函數(shù)
2.2 Flask與web交互
2.2.1 Flask和表單
html代碼
<style>
form{
margin:300px auto;
display:block;
}
</style>
<body>
<form action="http://localhost:5000/test" method="post" style="width:300px;height:30px">
<div class="">
<label for="exampleFormControlTextarea1" class="form-label">Example textarea</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="txt"></textarea>
</div>
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</body>
py代碼
from flask import Flask, redirect, url_for, request, render_template
app = Flask(__name__)
@app.route('/page')
def index():
return render_template("1.html")
@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name
@app.route('/test',methods = ['POST', 'GET'])
def test():
if request.method == 'POST':
txt = request.form['txt']
print(txt)
return redirect(url_for('success', name=txt))
else:
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
2.2.2 Flask模板
三種常用的定界符:
{{ ... }} 用來(lái)標(biāo)記變量。
{% ... %} 用來(lái)標(biāo)記語(yǔ)句,比如 if 語(yǔ)句,for 語(yǔ)句等。
{# ... #} 用來(lái)寫(xiě)注釋。
render_template
方法渲染的模板需要在 templates
文件夾下
hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
我的模板html內(nèi)容
<br />{{ my_str }}
<br />{{ my_int }}
<br />{{ my_array }}
</body>
</html>
test.py
from flask import Flask, redirect, url_for, request, render_template
app = Flask(__name__)
@app.route('/')
def index():
# 往模板中傳入的數(shù)據(jù)
my_str = 'Hello Word'
my_int = 10
my_array = [3, 4, 2, 1, 7, 9]
return render_template('hello.html',
my_str = my_str,
my_int = my_int,
my_array = my_array
)
if __name__ == '__main__':
app.run(debug=True)
2.2.3 靜態(tài)文件
例如引入static
文件下的 1.css
,在 html
中寫(xiě)入下面的代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-861411.html
<link rel="stylesheet" href="{{ url_for('static', filename='1.css') }}" type="text/css">
3 參考文檔
[1] W3CSchool教程
[2] 社區(qū)版Pycharm自建Flask項(xiàng)目
[3] Flask Request對(duì)象
[4] 靜態(tài)文件引用
[5] SQLAlchemy文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-861411.html
到了這里,關(guān)于Python第三方庫(kù) - Flask(python web框架)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!