This repository was archived by the owner on Feb 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
373 lines (305 loc) · 12.7 KB
/
Copy pathapp.py
File metadata and controls
373 lines (305 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from flask import Flask, render_template, request, jsonify
import sqlite3
import os
import csv
import re
from werkzeug.utils import secure_filename
app = Flask(__name__)
DB_FILE = 'sustainable_energy.db'
SOURCE_DATA_DIR = 'source_data'
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB limit
def get_db_connection():
"""Get database connection with proper configuration"""
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
# Set busy timeout and WAL mode to reduce locking issues
conn.execute('PRAGMA busy_timeout = 30000')
conn.execute('PRAGMA journal_mode = WAL')
return conn
def validate_table_name(table_name):
"""Validate table name to prevent SQL injection"""
if not table_name:
return False
# Only allow alphanumeric and underscores
return bool(re.match(r'^[a-zA-Z0-9_]+$', table_name))
def sanitize_column_name(header):
"""Sanitize CSV header to valid SQL column name"""
# Clean headers - remove newline characters
header = header.replace('\n', ' ').strip()
# Replace spaces and special characters with underscores
sql_col = header.replace(' ', '_').replace('(', '').replace(')', '').replace('%', 'pct').replace('$', 'USD').replace('/', '_per_')
sql_col = sql_col.replace(',', '').replace('-', '_')
# Remove any remaining special characters
sql_col = ''.join(c if c.isalnum() or c == '_' else '_' for c in sql_col)
return sql_col
def detect_column_type(values):
"""Detect SQL column type from sample values"""
if not values:
return 'TEXT'
# Check if all non-empty values are integers
all_int = True
has_int = False
for val in values:
if val and val.strip():
has_int = True
try:
int(val)
except (ValueError, TypeError):
all_int = False
break
if all_int and has_int:
return 'INTEGER'
# Check if all non-empty values are numeric
all_numeric = True
has_numeric = False
for val in values:
if val and val.strip():
has_numeric = True
try:
float(val)
except (ValueError, TypeError):
all_numeric = False
break
if all_numeric and has_numeric:
return 'REAL'
return 'TEXT'
def execute_query(query):
"""Execute SQL query and return results"""
try:
conn = get_db_connection()
cursor = conn.cursor()
# Only allow SELECT queries for safety
query_upper = query.strip().upper()
if not query_upper.startswith('SELECT'):
conn.close()
return None, None, "Only SELECT queries are allowed for safety."
cursor.execute(query)
rows = cursor.fetchall()
# Get column names
columns = [description[0] for description in cursor.description] if cursor.description else []
# Convert rows to list of dictionaries
results = [dict(row) for row in rows]
conn.close()
return results, columns, None
except Exception as e:
return None, None, str(e)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/tables', methods=['GET'])
def get_tables():
"""Get list of all tables in the database"""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
tables = [row[0] for row in cursor.fetchall()]
conn.close()
return jsonify({'tables': tables})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/upload-csv', methods=['POST'])
def upload_csv():
"""Upload and store CSV file"""
try:
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
# Check file extension
if not file.filename.lower().endswith('.csv'):
return jsonify({'error': 'Only CSV files are allowed'}), 400
# Check file size
file.seek(0, os.SEEK_END)
file_size = file.tell()
file.seek(0)
if file_size > MAX_FILE_SIZE:
return jsonify({'error': f'File size exceeds {MAX_FILE_SIZE / 1024 / 1024}MB limit'}), 400
# Ensure source_data directory exists
os.makedirs(SOURCE_DATA_DIR, exist_ok=True)
# Check for duplicate
filename = secure_filename(file.filename)
filepath = os.path.join(SOURCE_DATA_DIR, filename)
if os.path.exists(filepath):
return jsonify({'error': 'File with this name already exists'}), 400
# Save file
file.save(filepath)
return jsonify({
'success': True,
'filename': filename,
'message': 'File uploaded successfully'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/preview-csv', methods=['POST'])
def preview_csv():
"""Preview CSV file before import"""
try:
data = request.get_json()
filename = data.get('filename', '')
if not filename:
return jsonify({'error': 'No filename provided'}), 400
filepath = os.path.join(SOURCE_DATA_DIR, secure_filename(filename))
if not os.path.exists(filepath):
return jsonify({'error': 'File not found'}), 404
headers = []
sample_rows = []
column_types = []
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
headers = next(reader)
headers = [h.replace('\n', ' ').strip() for h in headers]
# Read first 20 rows for preview
for i, row in enumerate(reader):
if i >= 20:
break
sample_rows.append(row)
# Detect column types from sample data
if sample_rows:
for col_idx in range(len(headers)):
column_values = [row[col_idx] if col_idx < len(row) else '' for row in sample_rows]
col_type = detect_column_type(column_values)
column_types.append(col_type)
else:
column_types = ['TEXT'] * len(headers)
return jsonify({
'headers': headers,
'sample_rows': sample_rows[:20],
'column_types': column_types,
'row_count': len(sample_rows)
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/create-table', methods=['POST'])
def create_table():
"""Create table from CSV file"""
try:
data = request.get_json()
filename = data.get('filename', '')
if not filename:
return jsonify({'error': 'No filename provided'}), 400
# Derive table name from filename
table_name = os.path.splitext(secure_filename(filename))[0]
table_name = re.sub(r'[^a-zA-Z0-9_]', '_', table_name)
if not validate_table_name(table_name):
return jsonify({'error': 'Invalid table name generated from filename'}), 400
filepath = os.path.join(SOURCE_DATA_DIR, secure_filename(filename))
if not os.path.exists(filepath):
return jsonify({'error': 'File not found'}), 404
# Check if table already exists
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,))
if cursor.fetchone():
conn.close()
return jsonify({'error': f'Table "{table_name}" already exists'}), 400
# Read CSV and create table
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
headers = next(reader)
headers = [h.replace('\n', ' ').strip() for h in headers]
# Sanitize column names
sql_columns = [sanitize_column_name(h) for h in headers]
# Read all rows to detect types properly
all_rows = list(reader)
# Detect column types
column_types = []
for col_idx in range(len(headers)):
column_values = [row[col_idx] if col_idx < len(row) else '' for row in all_rows]
col_type = detect_column_type(column_values)
column_types.append(col_type)
# Create table statement
create_table_sql = f"CREATE TABLE {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT"
for col, col_type in zip(sql_columns, column_types):
create_table_sql += f",\n {col} {col_type}"
create_table_sql += "\n)"
cursor.execute(create_table_sql)
# Prepare insert statement
placeholders = ','.join(['?' for _ in headers])
insert_sql = f"INSERT INTO {table_name} ({','.join(sql_columns)}) VALUES ({placeholders})"
# Insert data
rows_inserted = 0
for row in all_rows:
processed_row = []
for i, value in enumerate(row):
if i >= len(column_types):
break
if value == '' or value is None:
processed_row.append(None)
elif column_types[i] == 'INTEGER':
try:
processed_row.append(int(value))
except (ValueError, TypeError):
processed_row.append(None)
elif column_types[i] == 'REAL':
try:
processed_row.append(float(value))
except (ValueError, TypeError):
processed_row.append(None)
else:
processed_row.append(value)
# Pad row if needed
while len(processed_row) < len(sql_columns):
processed_row.append(None)
cursor.execute(insert_sql, processed_row[:len(sql_columns)])
rows_inserted += 1
conn.commit()
conn.close()
return jsonify({
'success': True,
'table_name': table_name,
'rows_inserted': rows_inserted,
'message': f'Table "{table_name}" created successfully with {rows_inserted} rows'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/table/<table_name>', methods=['DELETE'])
def delete_table(table_name):
"""Delete table from database"""
try:
if not validate_table_name(table_name):
return jsonify({'error': 'Invalid table name'}), 400
# Prevent deletion of system tables
if table_name.startswith('sqlite_'):
return jsonify({'error': 'Cannot delete system tables'}), 400
conn = get_db_connection()
cursor = conn.cursor()
# Check if table exists
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,))
if not cursor.fetchone():
conn.close()
return jsonify({'error': f'Table "{table_name}" does not exist'}), 404
# Drop table
cursor.execute(f"DROP TABLE {table_name}")
conn.commit()
conn.close()
return jsonify({
'success': True,
'message': f'Table "{table_name}" deleted successfully'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/query', methods=['POST'])
def query():
data = request.get_json()
sql_query = data.get('query', '')
if not sql_query:
return jsonify({'error': 'No query provided'}), 400
results, columns, error = execute_query(sql_query)
if error:
return jsonify({'error': error}), 400
return jsonify({
'columns': columns,
'results': results,
'row_count': len(results) if results else 0
})
if __name__ == '__main__':
# Check if database exists
if not os.path.exists(DB_FILE):
print(f"Error: Database file '{DB_FILE}' not found!")
print("Please run 'python3 create_database.py' first.")
else:
print(f"Starting server...")
print(f"Open http://localhost:8080 in your browser")
app.run(debug=True, host='127.0.0.1', port=8080)