Download from DSS in a Webapp
Hi,
I am building a webapp with HTML/CSS/JS front end and Python back end. In the back end I am doing some calculations and I am creating an excel file which I am storing in DSS. Now what I want to do is have an HTML button that once pressed it will download that excel file from that folder for the user.
I found how to upload a file in the knowledge session but not how to download one
I also tried using get_download_stream to pick the file and pass to JS to download but again it did not work. Can you help please?
Best Answer
-
Hi @ioa2nis
,Here's how you can download a file from a managed folder by clicking on a button in a webapp:
in the Python part of the webapp define an endpoint like
import dataiku from flask import request from flask import send_file import io @app.route('/downloadFile') def first_call(): filename = 'filename.txt' stream = dataiku.Folder('FOLDER_ID').get_download_stream(filename) with stream: return send_file( io.BytesIO(stream.read()), as_attachment=True, attachment_filename=filename)
Then in the Javascript section you'd need a function like:
window.download = function(){ window.location.href = getWebAppBackendUrl('/downloadFile'); }
Finally in the HTML you can add a button to trigger that JS function:
<button onclick="download()">Download</button>
Hope this helps
Regards
Answers
-
CoreyS Dataiker Alumni, Dataiku DSS Core Designer, Dataiku DSS Core Concepts, Registered Posts: 1,150 ✭✭✭✭✭✭✭✭✭
Hi @ioa2nis
and welcome to the Dataiku Community. While you wait for a more detailed response, I want to point out this thread on the Community which may of some use to youI hope this helps!
-
HI @CoreyS
thank you for your response, I did a search before posting and I came across this but unfortunately this is for bookeh which I ma not using. I got my app on html/js -
Thank you so Much Andrey, you are a star, this is exactly what I was after. Really appreciate the help and the speedy responce.
-
CoreyS Dataiker Alumni, Dataiku DSS Core Designer, Dataiku DSS Core Concepts, Registered Posts: 1,150 ✭✭✭✭✭✭✭✭✭
These steps are now available in the Knowledge Base: Download from a Dataiku DSS Webapp
-
Hello All,
My team and I are currently working on a solution that needs to allow users to download files through their browser from a web app.
We found this solution and it "worked" for us on a local instance of dataiku, but it had some weird behavior. We then tried to implement it on a cloud instance of Dataiku and kept getting the following error:
TypeError: send_file() got an unexpected keyword argument 'attachment_filename'
After some digging, I believe the issue is that the "attachment_filename" parameter has been deprecated somewhere between the version we happened to have locally, 1.0.4, and the most recent version, 2.2.2, which we are using in our webapp in the cloud.
So, we are going to start the process of trying to figure out how to make this work, but wanted to let you know that the solution and the corresponding documentation in Dataiku Knowledge Base appears to be out of date.
-
So. that was fast. Here is the current code that is working for me.
Key changes are the need for a specified download_name it seems, and definitely need a mimetype parameter which will change with whatever type of file you are serving. We are serving a ".xlsx" file here.
Please note that we removed the parameter passed through the URL that was in the original question, but obviously that could be added back if needed.
@app.route('/downloadFile') def first_call(): filename = 'example_file.xlsx' stream = dataiku.Folder('folder_name').get_download_stream(filename) with stream: return send_file( io.BytesIO(stream.read()), as_attachment=True, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', download_name=filename)
io.BytesIO(stream.read()),
as_attachment=True,
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
download_name=filename) -
Hi,
I have a question regarding the file name. I am trying to use a variable I receive from the user to name my file, the backend is running correctly, but I am not able to download the file. Is there a way to customize the filename?
@app.route('/downloadFile') def first_call(): request_body = request.form['user_variable'] u_var = request.form['user_variable'] filename = f"{u_var}_Result.xlsx" print (filename) stream = dataiku.Folder('FOLDER').get_download_stream(f"{u_var}_Result.xlsx") print (stream) with stream: return send_file( io.BytesIO(stream.read()), as_attachment=True, attachment_filename=filename)