binaychandra commited on
Commit
fcc6ca2
·
1 Parent(s): 5ad571f

fix download

Browse files
Files changed (2) hide show
  1. app.py +14 -1
  2. templates/data.html +11 -20
app.py CHANGED
@@ -7,7 +7,8 @@ from flask import ( Flask, jsonify, render_template, request,
7
  send_file,
8
  Response,
9
  render_template_string,
10
- redirect)
 
11
  import pandas as pd
12
  import plotly.graph_objects as go
13
  from sqlalchemy import create_engine
@@ -21,6 +22,7 @@ from utilities.plotting import (get_validation_json,
21
  get_lst_topdepartment,
22
  get_wfrankwise_count,
23
  get_topfive_badgetitle)
 
24
  import sqlite3
25
  import time
26
  import json
@@ -244,6 +246,17 @@ def hometest():
244
  request_info['tbl_selected'] = request.form.getlist('tbl_selected')
245
  return redirect(url_for('data'))
246
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
  # if __name__ == '__main__':
249
  # app.run(debug=True)
 
7
  send_file,
8
  Response,
9
  render_template_string,
10
+ redirect,
11
+ send_file)
12
  import pandas as pd
13
  import plotly.graph_objects as go
14
  from sqlalchemy import create_engine
 
22
  get_lst_topdepartment,
23
  get_wfrankwise_count,
24
  get_topfive_badgetitle)
25
+ import os
26
  import sqlite3
27
  import time
28
  import json
 
246
  request_info['tbl_selected'] = request.form.getlist('tbl_selected')
247
  return redirect(url_for('data'))
248
 
249
+ @app.route('/download/<table_name>', methods=['GET'])
250
+ def download_csv(table_name):
251
+ # Assuming you have a mapping or logic to get the CSV file path from the table name
252
+ csv_file_path = os.path.join('referencefiles', f"{table_name}.csv")
253
+
254
+ if os.path.exists(csv_file_path):
255
+ return send_file(csv_file_path, as_attachment=True)
256
+ else:
257
+ return "File not found", 404
258
+
259
+
260
 
261
  # if __name__ == '__main__':
262
  # app.run(debug=True)
templates/data.html CHANGED
@@ -93,28 +93,19 @@
93
 
94
  // Function to handle the table data download
95
  function downloadTableData() {
96
- const selectedTable = $('#table-dropdown').val();
97
- if (selectedTable) {
98
- const fileName = `referencefiles/${selectedTable}.csv`;
99
- fetch(fileName)
100
- .then(response => response.blob())
101
- .then(blob => {
102
- const url = window.URL.createObjectURL(blob);
103
- const a = document.createElement('a');
104
- a.style.display = 'none';
105
- a.href = url;
106
- a.download = `${selectedTable}.csv`;
107
- document.body.appendChild(a);
108
- a.click();
109
- window.URL.revokeObjectURL(url);
110
- })
111
- .catch(error => console.error('Error downloading the file:', error));
112
- } else {
113
- alert('Please select a table to download.');
114
- }
115
  }
116
 
117
- // ... rest of the existing code ...
118
  </script>
119
  <script>
120
  var req_tables = {{ req_tables | safe }};
 
93
 
94
  // Function to handle the table data download
95
  function downloadTableData() {
96
+ // Get the selected table name from the dropdown
97
+ const selectedTableIndex = $('#table-dropdown').val();
98
+
99
+ // Map the selected index back to the table name
100
+ const tableName = $("#table-dropdown option:selected").text().toLowerCase(); // Assuming the table name is stored in lowercase as CSV filename
101
+
102
+ // Build the URL for the download endpoint
103
+ const downloadUrl = `/download/${tableName}`;
104
+
105
+ // Redirect the browser to the download URL to trigger the download
106
+ window.location.href = downloadUrl;
 
 
 
 
 
 
 
 
107
  }
108
 
 
109
  </script>
110
  <script>
111
  var req_tables = {{ req_tables | safe }};