from flask import Flask, send_file, jsonify # 导入Flask类
from flask import render_template
app = Flask(__name__) #创建flask应用对象,app=application
# 第一中返回,返回一个响应
@app.route("/index") #flask应用对象增加路由
def index(): #与路由绑定视图函数,视图函数名尽可能保持唯一。
return 'hello word' #相当于HtppResponse
#第二种返回,返回一个html页面
@app.route("/home")
def home():
return render_template("home.html") #模板存放路径
#第三种跳转到某一页面
from flask import redirect
@app.route("/re")
def re():
return redirect('/home')
#Flask的特殊返回 send.file("")返回文件
@app.route("/get_file")
def get_file():
return send_file('learn01(简单实现一个flask).py')
#返回图片
@app.route("/get_file_picture")
def get_file_picture():
return send_file('阿瞎.jpg')
#返回一个程序或者MP3或者视频文件
@app.route("/get_file_exe")
def get_file_exe():
return send_file('程序.exe')
#录音文件
@app.route("/get_file_ly")
def get_file_ly():
return send_file('录音(已自动保存).m4a')
#jsonify文件,返回文件的格式为json格式
@app.route("/get_json")
def get_json():
data={
"name":"你好",
"pwd":"123"
}
return jsonify(data) #Content-Type:application/json ==标准格式
# return data #暂时不建议使用,兼容性低。直接返回dict,本质上还是执行的jsonify