Add second iteration of json download

This commit is contained in:
Stephan Porada 2020-01-21 14:50:27 +01:00
parent d6fdf69e81
commit 2142fb14af
2 changed files with 58 additions and 29 deletions

View File

@ -94,6 +94,7 @@ class CQiWrapper(CQiClient):
cpos match positions, produced by the query cpos match positions, produced by the query
query -- query written in cqp query language query -- query written in cqp query language
''' '''
self.query = query
self.cqp_query(self.corpus_name, result_subcorpus_name, query) self.cqp_query(self.corpus_name, result_subcorpus_name, query)
self.result_subcorpus = (self.corpus_name self.result_subcorpus = (self.corpus_name
+ ':' + ':'
@ -198,7 +199,8 @@ class CQiWrapper(CQiClient):
self.results = {'matches': all_matches, self.results = {'matches': all_matches,
'cpos_lookup': all_cpos_infos, 'cpos_lookup': all_cpos_infos,
'text_lookup': text_lookup, 'text_lookup': text_lookup,
'total_nr_matches': self.total_nr_matches} 'total_nr_matches': self.total_nr_matches,
'query': self.query}
return self.results return self.results
def get_cpos_infos(self, all_cpos): def get_cpos_infos(self, all_cpos):

View File

@ -107,25 +107,31 @@
</form> </form>
<div id="export-query-results-modal" class="modal modal-fixed-footer no-autoinit"> <div id="export-query-results-modal" class="modal modal-fixed-footer no-autoinit">
<!-- <form id="download-query-results-form" method="post"> --> <div class="modal-content">
<div class="modal-content"> {{ query_download_form.hidden_tag() }}
{{ query_download_form.hidden_tag() }} <h4>Download current query Results</h4>
<h4>Download current query Results</h4> <p>The results of the current query can be downlaoded as several files like csv or json. Those files can be used in other software like excel. Also it is easy to publish your results as raw data like this!</p>
<p>The results of the current query can be downlaoded as several files like csv or json. Those files can be used in other software like excel. Also it is easy to publish your results as raw data like this!</p> <table>
<div class="input-field"> <tr>
<i class="material-icons prefix">insert_drive_file</i> <td>JSON</td>
{{ query_download_form.file_type() }} <td><a class="btn waves-effect waves-light" id="download-results">Download</a></td>
{{ query_download_form.file_type.label }} </tr>
{% for error in query_download_form.file_type.errors %} <tr>
<span class="helper-text red-text">{{ error }}</span> <td>CSV</td>
{% endfor %} <td><a class="btn waves-effect waves-light disabled">Download</a></td>
</div> </tr>
</div> <tr>
<div class="modal-footer"> <td>EXCEL</td>
<!-- <button class="btn waves-effect waves-light" id="download-form-submit" type="submit">Download</button> --> <td><a class="btn waves-effect waves-light disabled">Download</a></td>
<a class="btn waves-effect waves-light" id="downloadAnchorElem">Download</a> </tr>
</div> <tr>
<!-- </form> --> <td>HTML</td>
<td><a class="btn waves-effect waves-light disabled">Download</a></td>
</tr>
</table>
</div>
<div class="modal-footer">
</div>
</div> </div>
<div class="row"> <div class="row">
@ -240,8 +246,8 @@
var queryFormElement = document.getElementById("query-form"); var queryFormElement = document.getElementById("query-form");
var queryResultsElement = document.getElementById("query-results"); var queryResultsElement = document.getElementById("query-results");
var queryResultsMetadataElement = document.getElementById("query-results-metadata") var queryResultsMetadataElement = document.getElementById("query-results-metadata");
var exportQueryResults = document.getElementById("export-query-results") var exportQueryResults = document.getElementById("export-query-results");
exportQueryResults.onclick = function() { exportQueryResults.onclick = function() {
exportModal.open(); exportModal.open();
}; };
@ -387,21 +393,42 @@
}], }],
valueNames: ["text-titles", "left-context", "hit", "right-context"]}; valueNames: ["text-titles", "left-context", "hit", "right-context"]};
var userList = new List('result-list', options); var userList = new List('result-list', options);
var inspectBtns = document.getElementsByClassName("inspect"); var inspectBtns = document.getElementsByClassName("inspect");
for(var i = 0; i < inspectBtns.length; i++) { for(var i = 0; i < inspectBtns.length; i++) {
var inspectBtn = inspectBtns[i]; var inspectBtn = inspectBtns[i];
var dataIndex = inspectBtn.parentNode.parentNode.getAttribute("data-index"); var dataIndex = inspectBtn.parentNode.parentNode.getAttribute("data-index");
inspectBtn.onclick = function() { inspectBtn.onclick = function() {
exportModal.open(); contextModal.open();
nopaque.socket.emit("inspect_match", {"cpos": matches[dataIndex]["hit"]}); nopaque.socket.emit("inspect_match", {"cpos": matches[dataIndex]["hit"]});
}; };
} }
// download results as JSON
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(message, undefined, 2)); // Function to download data to a file
console.log(dataStr); function download(downloadElem, data, filename, type) {
var downloadAnchorElem = document.getElementById("downloadAnchorElem"); var file = new Blob([data], {type: type});
downloadAnchorElem.setAttribute("href", dataStr); if (window.navigator.msSaveOrOpenBlob) // IE10+
downloadAnchorElem.setAttribute("download", "results.json"); window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var url = URL.createObjectURL(file);
downloadElem.href = url;
downloadElem.download = filename;
}
}
// create json filename for download
var today = new Date();
var currentDate = today.getUTCFullYear() + '-' + (today.getUTCMonth() +1) + '-' + today.getUTCDate();
var currentTime = today.getUTCHours() + ":" + today.getUTCMinutes() + ":" + today.getUTCSeconds();
var safeFilename = message['query'].replace(/[^a-z0-9_-]/gi, "_");
var resultFilename = "UTC-" + currentDate + "_" + currentTime + "_" + safeFilename + ".json";
// get a where download is served
var downloadResults = document.getElementById("download-results");
// stringify JSON object for json download
var dataStr = JSON.stringify(message, undefined, 2);
downloadResults.onclick = download(downloadResults,
dataStr,
resultFilename,
"text/json");
}); });