0

I have an application where, through a server, the user needs to download a CSV file and an image that is stored in the same directory as the .py file but it doesn´t work. Here are my python code:

app = Flask(__name__)

@app.route('/', methods= ['POST', 'GET'])
def result():
    if request.method == 'GET':
        return render_template('result.html')
    elif request.method == 'POST':
             
            if request.form['submit_button'] == 'Download CSV':
                return send_file('name.csv', as_attachment=True, download_name='name.csv')
                

            elif request.form['submit_button'] == 'Download plot':
                return Flask.send_static_file('static/plot.png', as_attachment=True, download_name='plot.png')
                              
    

if __name__ == '__main__':
    app.run(debug = True, host='0.0.0.0')  

Here are my html code:

<div class = 'wrap'>
     
 
    <form method="POST">

        <input type="submit" name="submit_button" value="Download CSV">
        <input type="submit" name="submit_button" value="Download plot">

    </form>
</div>

And here are my directories:

enter image description here

However, when I try to download it, I get the following error:

enter image description here

Thank you very much!!! I hope you can help me!

1 Answers1

0

The error message tells you exactly what is wrong.

The function either returned None or ended without a return statement.

You are not returning any response from the view function when the request is a POST. The send_file function returns a Response object, that you then need to return from the view function. So, you just need to do

                return send_file('name.csv', as_attachment=True, download_name='name.csv')
gla3dr
  • 2,179
  • 16
  • 29