|
16 | 16 | ALLOWED_RESULTS_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif', 'pdf', 'docx'} |
17 | 17 | ALLOWED_MUSIC_EXTENSIONS = {'mp3', 'wav', 'm4a', 'ogg', 'mp4'} |
18 | 18 |
|
| 19 | +# Automation of import to sqlite3 database |
| 20 | +def auto_import_uploads(): |
| 21 | + if not os.path.exists(UPLOAD_FOLDER): |
| 22 | + return |
| 23 | + |
| 24 | + for root, dirs, files in os.walk(UPLOAD_FOLDER): |
| 25 | + for filename in files: |
| 26 | + ext = filename.rsplit('.', 1)[1].lower() |
| 27 | + if ext not in ['csv', 'npy']: |
| 28 | + continue |
| 29 | + |
| 30 | + filepath = os.path.join(root, filename) |
| 31 | + table_name = filename.replace('.', '_').replace('-', '_').replace('/', '_').replace('\\', '_') |
| 32 | + |
| 33 | + try: |
| 34 | + if ext == 'csv': |
| 35 | + df = pd.read_csv(filepath) |
| 36 | + elif ext == 'npy': |
| 37 | + arr = np.load(filepath, allow_pickle=True) |
| 38 | + if isinstance(arr, np.ndarray): |
| 39 | + if arr.ndim == 2: |
| 40 | + df = pd.DataFrame(arr) |
| 41 | + elif arr.ndim == 1 and hasattr(arr[0], 'dtype') and arr[0].dtype.names: |
| 42 | + df = pd.DataFrame(arr) |
| 43 | + else: |
| 44 | + df = pd.DataFrame(arr) |
| 45 | + else: |
| 46 | + continue # skip unsupported NPY format |
| 47 | + else: |
| 48 | + continue |
| 49 | + |
| 50 | + with sqlite3.connect(DB_NAME) as conn: |
| 51 | + df.to_sql(table_name, conn, if_exists='replace', index=False) |
| 52 | + print(f"Imported: {filename} → table '{table_name}'") |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + print(f"Failed to import {filename}: {e}") |
| 56 | + |
| 57 | + |
19 | 58 | # ========== FLASK APP ========== |
20 | 59 | app = Flask(__name__) |
21 | 60 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
@@ -444,6 +483,8 @@ def delete_dataset_file(property_name, tab, filename): |
444 | 483 | for rule in app.url_map.iter_rules(): |
445 | 484 | print(rule.endpoint, rule) |
446 | 485 |
|
| 486 | +auto_import_uploads() |
| 487 | + |
447 | 488 | # ========== MAIN ========== |
448 | 489 | if __name__ == '__main__': |
449 | 490 | app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) |
0 commit comments