✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Downloading file issues

Hello!

I’m trying to add a button in my dash application (which is embedded in a Flask server) to download a file previously generated containing data and graphs extracted from a SQL database from my XAMPP Server in my laptop to another device.

I’ve already tried it in html and it works:

<a href="file.docx" download><button>Download file</button></href>

And now I want to include it in dash language inside the code, in the shape of a button. I tried the following:

html.A(html.Button('Download file'), href="file.docx", download=True)

But what I get is an empty file which I can’t open, it says there is a problem with the content. But I checked several times that the original file is in the folder and it can be opened and see the content.

Any idea or help is appreciated

Hello,
if you want to download a file, you should use the Flask routing with one of the functions (send_file or send_from_directory), more information can be found here

here is a simple example :

import dash
import dash_bootstrap_components as dbc
import dash_html_components as html 
from dash.dependencies import Input, Output
from flask import Flask,send_from_directory


server = Flask(__name__)
app = dash.Dash(server=server)

# leave it blank if your file is in the root
#otherwise add name of the folder here
FILE_DIRECTORY = ""

app.layout = html.A(html.Button('Download file'), href="Download/File.xlsx")

@server.route("/Download/<path:path>")
def download(path):
    return send_from_directory(FILE_DIRECTORY, path, as_attachment=True)
    

if __name__ == '__main__':
    app.run_server(debug=True)

You can also check this thread where send_file is used to do the same

1 Like

BOOM! It worked! Thank you (again) so much. I thought path:path was an operator to substitute but it was literal!
I’m a newbie at Flask and I have a stupid doubt to be answered if you don’t mind

How does the href connect to the download function of @server.route to obtain the value of path through the path:path thing?

Hello,

i don’t have such experience with Flask, but for the href and the path:path thing, it is useful when dealing with many files to be downloaded not only one, that path:path is the way used in Flask to pass a parameter (in this case the name of the file) to the function , in your example you have only one file to donwload so you don’t need to have the “/Download/path:path”, you can put anything in href (for example just Download or getFIle etc) as long as you keep the same thing in server.route so Flask can execute the function related to that route when you click on the link. and remove the path argument from the function since its only one file which is known, and this way it will also work.

even other web frameworks use the same routing concept. Please if your issue with downloading files is solved dont forget to close the thread by selecting it as the correct answer.

Thank you

1 Like

Ok, thank you for all. I owe you one.

1 Like