flask 接口

import os
from flask_restful import Resource
from flask import Flask,jsonify, render_template, request,make_response, Response
from PIL import Image
import globalvar as gl
import _thread
import neural_style
#import neural_style

gl._init()
gl.set_value('isend', 'false')
gl.set_value('time', 0)
gl.set_value('is_start',0)

app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = 'upload'
basedir = os.path.abspath(os.path.dirname(__file__))

@app.route("/")
def index():
    return render_template("upload.html")

@app.route("/upload_1", methods= ['POST'])
def upload_1():
    target = os.path.join(APP_ROOT, 'images/')
    print(target)
    if not os.path.isdir(target):
        os.mkdir(target)
    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target, filename])
        print(destination)
        file.save(destination)
    return render_template("complete.html")


# 接口1:上传文图片,返回是否上传成功的提示
@app.route('/upload', methods=['POST'], strict_slashes=False)
def upload():
    # print (123)
    neural_style.per_col = request.form['iskeep']
    neural_style.STYLE_LAYER_WEIGHT_EXP = request.form['type']
    neural_style.ITERATIONS = int(request.form['iterations'])
    print(Response.status_code)
    print(Response.status)
    #print(response.getcode())
    Response_Code = str(Response.status_code)
    print(Response_Code)
    global is_start
    is_start = 0
    return "上传成功"
#接口2:返回gpu/cpu使用率,是否处理完成,处理结束之后图片的路径
#is_start = 0
# neural_style.is_start=0
@app.route('/progress', methods=['GET'], strict_slashes=False)
def progress():
 try:
    global is_start
    if is_start == 0:
        #import neural_style
        gl.set_value('isend', 'false')
        gl.set_value('time', 0)
        # get_imageName(file_dir)
        _thread.start_new_thread(neural_style.main, (neural_style.STYLE_LAYER_WEIGHT_EXP, neural_style.per_col,
                                                      neural_style.ABSTRACT_PATH,))
        # global ABSTRACT_PATH
        # neural_style.main(neural_style.STYLE_LAYER_WEIGHT_EXP, neural_style.per_col, ABSTRACT_PATH)
        is_start = 1
    isend = gl.get_value('isend')
    count = gl.get_value('time')
    return jsonify({"progress":count,"isend":isend, "path": "114.116.70.63:455"+"/show"+ "/pic_one.jpg","iterations":neural_style.ITERATIONS})
 except:
     print(7878541451)
     return "线程错误"


# show photo
@app.route('/show/<string:filename>', methods=['GET'])
def show_photo(filename):
    file_dir = os.path.join(basedir, 'Out_image')
    if request.method == 'GET':
        if filename is None:
            pass
        else:
            image_data = open(os.path.join(file_dir, '%s' % filename), "rb").read()
            resp = Response(image_data, mimetype="image/jpeg")
            print()
            return resp
    else:
        pass

from flask import send_file, send_from_directory
import os
from flask import make_response

"""
@app.route("/download/<filename>", methods=['GET'])
def download_file(filename):
    # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
    directory = os.getcwd()  # 假设在当前目录
    response = make_response(send_from_directory(directory, filename, as_attachment=True))
    response.headers["Content-Disposition"] = "attachment; filename={}".format(filename.encode().decode('latin-1'))
    return response

from flask import send_file, send_from_directory
import os
@app.route("/download/<filename>", methods=['GET'])
def download_file(filename):
    # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
    directory = os.getcwd()  # 假设在当前目录
    return send_from_directory(directory, filename, as_attachment=True)
"""
#下载文件
@app.route("/download/<path:filename>")
def downloader(filename):
    dirpath = os.path.join(app.root_path, 'upload')  # 这里是下在目录,从工程的根目录写起,比如你要下载static/js里面的js文件,这里就要写“static/js”
    return send_from_directory(dirpath, filename, as_attachment=True)  # as_attachment=True 一定要写,不然会变成打开,而不是下载

@app.route('/log', methods=['GET'])
def show_error():
        logdir = os.getcwd() + "/flask_test"+ "/flask.log"
        resp = make_response(open(os.path.join(logdir)).read())
        resp.headers["Content-type"] = "text/plan;charset=UTF-8"
        return resp
if __name__ == "__main__":
    app.run(port=4555, debug=True)
    print('jkhsdjhsak')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值