Getting an error attachment_filename does not exist in my docker environment

Viewed 352

Due to some reasons this particular code is not working in docker but it works fine in development environment. I am getting error "TypeError: send_file() got an unexpected keyword argument 'attachment_filename'" I have checked the official documents (https://tedboy.github.io/flask/generated/flask.send_file.html) and it says that we can use "attachment_filename" which exists. Not sure why I am getting this error. Plz help.

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")
2 Answers

I have found the root cause of this issue. The above issue which I have mentioned is happening in the latest version of python. My docker is using running Python 3.10.6 whereas my development python is running Python 3.8.10. So i made a small modification in the code as mentioned below.

return send_file (io.BytesIO(buf_str.read().encode("utf-8")), mimetype="text/csv", download_name="data.csv" )

full code

@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" )

This question comes up on search for this error so I'm adding a more explicit answer.
The explanation is that attachment_filename have been renamed download_name.

Quoting from the github issue:

Old names for some send_file parameters have been removed. download_name replaces attachment_filename, max_age replaces cache_timeout, and etag replaces add_etags. Additionally, path replaces filename in send_from_directory.

Related