一、python-flask前提工作及简单测试
下载flask
pip install flask
测试
建立ceshi.py
from flask import Flask
app=Flask(__name__)
@app.route('/')
def index():
return render_template('ceshi.html')
#3
if __name__=='__main__':
app.run()
在templates 下建立ceshi.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ceshi</title>
</head>
<body>
python-flask
</body>
</html>
结果显示:
测试成功
二、python-flask实例模板
1.实现图片识别
建立index.py
@app.route('/k')
def index():
outname = detect('static/bao.jpg')
return render_template('index.html', outname=outname)
def detect(filename):
face_cascade = cv2.CascadeClassifier('C:\\Users\\86181\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
outname='static/face.jpg'
cv2.imwrite(outname,img)
return outname
if __name__=='__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>媒體大數據</title>
</head>
<body>
媒體大數據
人臉識別
<img src="{{outname}}" />
</body>
</html>
运行打开:
http://127.0.0.1:5000/k
显示结果:
2.链接数据库
建立shuju.py
from flask import Flask,render_template
import cv2
import pymysql
from flask import Flask,render_template,request
app=Flask(__name__)
@app.route('/')
def english():
db = pymysql.connect("localhost", "root", "Xz8231600", "sakila", charset="utf8")
cursor = db.cursor()
try:
sql = "select * from map_enword limit 200"
cursor.execute(sql)
rs = cursor.fetchall()
re = list(rs)
print(rs)
except:
rs ="db-error"
print("py-db-error")
db.close()
return render_template('english.html',rs=rs)
建立english.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>英语四六级词表</title>
</head>
<body>
<h1>英语四六级词表</h1>
<form action="/search">
<input type="submit" name="next" value="搜索">
</form>
{% for r in rs %}
{{r}}<br>
{% endfor %}
</body>
</html>
结果:
3.链接数据库并且添加html搜索功能
from flask import Flask,render_template,request
import pymysql
import cv2
app=Flask(__name__)
@app.route('/search')
def search():
wanted = request.args.get("wanted", type=str)
if wanted == None:
wanted = 'pineapple'
db = pymysql.connect("localhost", "root", "Xz8231600", "sakila", charset='utf8')
cursor = db.cursor()
try:
sql = "select * from map_enword where english like '%" + wanted + "%'"
cursor.execute(sql)
rs = cursor.fetchall()
rs = list(rs)
print(rs)
except:
rs = 'db-error'
print('py-db-error')
db.close()
return render_template('video.html',rs=rs)
def xianshi():
wanted = request.args.get("wanted", type=str)
if wanted == None:
wanted = 'pineapple'
db = pymysql.connect("localhost", "root", "Xz8231600", "sakila", charset='utf8')
cursor = db.cursor()
try:
sql = "select * from map_enword where english like '%" + wanted + "%'"
cursor.execute(sql)
rs = cursor.fetchall()
rs = list(rs)
print(rs)
except:
rs = 'db-error'
print('py-db-error')
if __name__=='__main__':
app.run()
显示结果:
总结
html 和py 文件分开写,数据放在static中,注意端口 @app.route('/)注意一定要加/