提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
前言
在學(xué)習(xí)機(jī)器學(xué)習(xí)或者深度學(xué)習(xí)后,我們常常會將自己訓(xùn)練好的模型來進(jìn)行部署或者可視化出來,供自己或者別人來使用,那么python的flask框架就可以作為選擇之一。
一、Flask快速使用
安裝
# python環(huán)境的安裝方式
pip install flask
#conda環(huán)境安裝
conda install flask
二、快速使用flask
- 如何使用flask打印 hello world
from flask import Flask
# 實例化flask對象
app = Flask(__name__)
@app.route('/index')
def index():
return 'hello world'
if __name__ == '__main__':
app.run()
-
總結(jié):flask框架是基于werkzeug的swgi實現(xiàn),flask自己沒有wsg文章來源:http://www.zghlxwxcb.cn/news/detail-820723.html
-
用戶請求一旦到來,就會運行
app.__call__()
方法文章來源地址http://www.zghlxwxcb.cn/news/detail-820723.html
三、創(chuàng)建登錄頁面
- 要想獲得get或者post請求需要寫
@app.route('/login',methods= ['GET','POST'])
- 要想加載某個網(wǎng)頁需要導(dǎo)入flask庫里的render_template包
- 要想在網(wǎng)頁點擊某個東西跳轉(zhuǎn)頁面需要從flask庫里導(dǎo)入redirect包
- render_template:加載頁面
- jsonify:傳入json數(shù)據(jù)格式
- request:接收返回的信息
- redirect:跳轉(zhuǎn)網(wǎng)頁
from flask import Flask,render_template,jsonify,request,redirect
#實例化flask
app = Flask(__name__)
@app.route('/login',methods= ['GET','POST'])
def login():
#發(fā)送get請求
if request.method == 'GET':
# return '登錄' # HttpResponse
# return render_template('login.html') #render
# return jsonify({'code':1000,'data':[1,2,3]}) # JsonResponse
# 進(jìn)入login網(wǎng)頁
return render_template('login.html') #render
# 以下是發(fā)送post請求并用request來接受
user = request.form.get('user')
pwd = request.form.get('pwd')
if user == 'zhaowentao' and pwd =='zwt':
# 密碼正確跳轉(zhuǎn)index網(wǎng)頁
return redirect('/index')
# 輸入錯誤則會提示error
error = '用戶名或密碼錯誤'
return render_template('login.html',error=error)
@app.route('/index')
def index():
return '首頁'
if __name__ == '__main__':
app.run()
- 以下是login.html代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用戶登錄</h1>
<form method="post">
<input type="text" name="user">
<input type="text" name="pwd">
<input type="submit" name="提交"><span style="color:red;">{{error}}</span>
</form>
</body>
</html>
到了這里,關(guān)于模型部署flask學(xué)習(xí)篇(一)---- flask初始及創(chuàng)建登錄頁面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!