我有一个Flask视图,它生成数据并将其保存为带有Pandas的CSV文件,然后显示数据.第二个视图用于生成的文件.我想在下载后删除该文件.我当前的代码引发了权限错误,可能是因为after_request
在文件提供之前删除了该文件send_from_directory
.如何在提供文件后删除文件?
def process_data(data)
tempname = str(uuid4()) + '.csv'
data['text'].to_csv('samo/static/temp/{}'.format(tempname))
return file
@projects.route('/getcsv/<file>')
def getcsv(file):
@after_this_request
def cleanup(response):
os.remove('samo/static/temp/' + file)
return response
return send_from_directory(directory=cwd + '/samo/static/temp/', filename=file, as_attachment=True)
Run Code Online (Sandbox Code Playgroud) 我希望设置一个日志数据库处理程序,它将保存 Flask 当前输出到控制台的所有信息(未处理的异常、werkzeug、sqlachemy),以及我的自定义日志消息。
我在设置这一切时遇到了麻烦。问题:
1) 为什么about视图中的模拟算术错误没有被任何记录器发现?
2) 为什么 werkzeug 记录器没有正确显示加载的服务器,并且对于模拟的 Aritmetic 错误只有输出“请求 %s 时出错”。
3)有没有更简单的方法可以将 Flask 服务器(及其组件)写入的所有内容通过管道传输到数据库?
感谢回复和任何其他建议。
到目前为止我所做的:
模型.py
class Log(DB.Model):
__tablename__ = 'logs'
id = DB.Column(DB.Integer, primary_key=True) # auto incrementing
logger = DB.Column(DB.String(100)) # the name of the logger. (e.g. myapp.views)
level = DB.Column(DB.String(100)) # info, debug, or error?
trace = DB.Column(DB.String(4096)) # the full traceback printout
msg = DB.Column(DB.String(4096)) # any custom log you may have included
created_at = DB.Column(DB.DateTime, default=DB.func.now()) # the current timestamp …
Run Code Online (Sandbox Code Playgroud) 我需要一些帮助来部署我的 Flask/Vue.js Web 应用程序。
当前设置: Vue.js 前端,带有静态页面的 vue-router + Flask 后端(通过 Ajax 进行通信,只需要通过表单发送电子邮件)。
问题:
我对如何在 VPS 上部署它感到困惑:
我的 Flask 后端/API:
from flask_cors import CORS
from flask_mail import Message
from datetime import datetime
import pytz
from flask_mail_sendgrid import MailSendGrid
from config import confreader
app = Flask(__name__)
app.config.from_object(confreader)
curdate = str(datetime.now(pytz.timezone("Europe/Bucharest")))
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
mail = MailSendGrid(app)
@app.route('/api/sendemail', methods=['POST'])
def send_email():
subject = '[Contact Form Message] ' …
Run Code Online (Sandbox Code Playgroud)