Use Public DSS API in a JavaScript WebApp
In a Dataiku WebApp, I want to put a link to "export" a dataset in CSV.
This is covered by the "dataset / get data" entrypoint in the public API ( https://doc.dataiku.com/dss/api/6.0/rest/#datasets-dataset-data-get ).
But if I just put a link `<a href="https://mydataikuinstance/public/...">` the browser ask for credentials when I click on the link.
How could I proceed ? Is it possible to handle this entrypoint with the JavaScript dataiku library, as it is possible in Python with the dataikuapi library ?
Best Answer
-
Hi,
Indeed if you use the REST HTTP API you need to pass the API Key as explained in Public REST API Authentication. You will find an example in the provided webapp template: New webapp > Code webapp > Standard > Advanced web app using Dataiku REST API
The function of interest here is:
function dataikuREST(path, callback) { let url = '/public/api' + path; // We use fetch API (https://developer.mozilla.org/fr/docs/Web/API/Fetch_API/Using_Fetch) fetch(url, { headers: { 'Authorization': 'Basic ' + btoa(dataiku.defaultAPIKey + ':' + '') } }) .then(response => { if (response.ok) { response.json().then(callback); } else { response.json().then( err => displayMessage(err.message, 'error-message') ); } }); }
Else you can also use the python API: https://doc.dataiku.com/dss/latest/python-api/datasets.html#dataiku.Dataset.raw_formatted_data
You will find a code snippet here: https://doc.dataiku.com/dss/latest/python-api/datasets.html#getting-a-dataset-as-raw-bytes
I hope this helps.
Regards,
Answers
-
Hi !
Thanks for your answer ! I've used the python backend, by more or less doing the same thing as explained in the link you provided.from datetime import datetime import dataiku import pandas as pd from flask import request, send_file, redirect, url_for @app.route('/<studyId>/users/export') def export_study_users(studyId): # ... Prepare the dataset according to studyId, with variables, jobs ... now = datetime.now() now = now.strftime("%Y%m%d_%H%M%S") filename = f'users_{studyId}_{now}.xlsx' return redirect(url_for('export_study_users_file', studyId=studyId, filename=filename)) @app.route('/<studyId>/users/export/<filename>') def export_study_users_file(studyId, filename): consumers = dataiku.Dataset(`{studyId}_consumers`) response = send_file( consumers.raw_formatted_data(format="excel"), attachment_filename=filename, as_attachment=True, cache_timeout=-1, ) response.headers["x-filename"] = filename response.headers["x-suggested-filename"] = filename response.headers["Access-Control-Expose-Headers"] = 'x-filename' return response
But the `x-filename`headers seems blocked by the Flask configuration, so I'm not able to handle "properly" the download client-side ; I have to cheat