赞
踩
1.1. 导入库函数
- from flask import Flask, render_template
- from flask import Flask, redirect, url_for, request
- app = Flask(__name__)
1.2. 定义函数
- @app.route('/des_name/<name>/')
- def des_name(name):
- return "%s"%name
-
- @app.route('/')
- def f():
- return render_template('login.html')
-
- @app.route('/',methods = ['POST', 'GET'])
- def login():
- if request.method == 'POST':
- user = request.form['nm']
- print(user)
- return redirect(url_for('des_name',name=user))
- else:
- user = request.args.get('nm')
- print(user)
- return redirect(url_for('des_name',name=user))
1.3. 调用
- if __name__ == '__main__':
- app.run(debug = True)
2.1. 引入库函数
- from flask import Flask
- from flask import render_template, request
- import base64
- import os
2.2. 定义函数
- def return_img_stream(img_local_path):
- """
- 工具函数:
- 获取本地图片流
- :param img_local_path:文件单张图片的本地绝对路径
- :return: 图片流
- """
- import base64
- img_stream = ''
- with open(img_local_path, 'rb') as img_f:
- img_stream = img_f.read()
- img_stream = base64.b64encode(img_stream).decode()
- return img_stream
-
- # 获取项目当前绝对路径
- # 比如我的项目在桌面上存放则得到——"C:\Users\asus\Desktop\shou"
- basedir = "C:/Users/jimore/Documents/Tencent Files/3457822025/FileRecv/yolov5-master"
-
- # 实例
- app = Flask(__name__)
-
-
- # 在根路由下返回上面的表单页面
- @app.route('/', methods=['GET'])
- def index():
- return render_template("dd.html")
-
-
- # 表单提交路径,需要指定接受方式
- @app.route('/getImg/', methods=['GET', 'POST'])
- def getImg():
- # 通过表单中name值获取图片
- imgData = request.files["image"]
- # 设置图片要保存到的路径
- path = basedir + "/static/upload/img/"
- print(path)
-
- # 获取图片名称及后缀名
- imgName = imgData.filename
-
- # 图片path和名称组成图片的保存路径
- file_path = path + imgName
-
- # 保存图片
- imgData.save(file_path)
-
- img_stream = return_img_stream(file_path)
- return render_template('hh.html',
- img_stream=img_stream)
2.3. 调用
app.run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。