The Dataiku Frontrunner Awards have just launched to recognize your achievements! Submit Your Entry

Download from DSS in a Webapp

Solved!
ioa2nis
Level 1
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?

 

0 Kudos
1 Solution
Andrey
Dataiker
Dataiker

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

Andrey Avtomonov
R&D Engineer @ Dataiku

View solution in original post

0 Kudos
4 Replies
CoreyS
Community Manager
Community Manager

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 you Download from Webapp to Local Downloads 

I hope this helps!

Looking for more resources to help you use DSS effectively and upskill your knowledge? Check out these great resources: Dataiku Academy | Documentation | Knowledge Base

A reply answered your question? Mark as ‘Accepted Solution’ to help others like you!
0 Kudos
ioa2nis
Level 1
Author

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

0 Kudos
Andrey
Dataiker
Dataiker

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

Andrey Avtomonov
R&D Engineer @ Dataiku

View solution in original post

0 Kudos
ioa2nis
Level 1
Author

Thank you so Much Andrey, you are a star, this is exactly what I was after. Really appreciate the help and the speedy responce.

0 Kudos
A banner prompting to get Dataiku DSS