由于某些原因,这个特定的代码无法在 docker 中运行,但在开发环境中运行良好。我收到错误“ TypeError:send_file()得到了意外的关键字参数'attachment_filename' ”我已经检查了官方文档(https://tedboy.github.io/flask/ generated/flask.send_file.html),它说我们可以使用存在的“attachment_filename”。不知道为什么我会收到此错误。请帮忙。
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, request, jsonify, session, Response, send_file
from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from pymongo import MongoClient
import urllib.parse
import bcrypt
import requests
import json
from datetime import datetime
import pandas as pd
from pathlib import Path
from flask import send_file
import io
import yaml
@app.route('/download')
def dwnlnd()
jsnx = session['mydata']
df = pd.DataFrame.from_dict(jsnx)
caldf = df.to_csv(index=False, header=True)
buf_str = io.StringIO(caldf)
buf_byt = io.BytesIO(buf_str.read().encode("utf-8"))
return send_file(buf_byt,
mimetype="text/csv",
as_attachment=True,
attachment_filename="data.csv")
Run Code Online (Sandbox Code Playgroud)
我已经找到了这个问题的根本原因。我提到的上述问题是在最新版本的 python 中发生的。我的 docker 使用运行 Python 3.10.6,而我的开发 python 运行 Python 3.8.10。所以我对代码做了一个小的修改,如下所述。
return send_file (io.BytesIO(buf_str.read().encode("utf-8")), mimetype="text/csv", download_name="data.csv" )
Run Code Online (Sandbox Code Playgroud)
完整代码
@app.route('/download')
def dwnlnd()
jsnx = session['mydata']
df = pd.DataFrame.from_dict(jsnx)
caldf = df.to_csv(index=False, header=True)
buf_str = io.StringIO(caldf)
return send_file (io.BytesIO(buf_str.read().encode("utf-8")), mimetype="text/csv", download_name="data.csv" )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10558 次 |
| 最近记录: |