Skip to content

Commit c8624a0

Browse files
author
SM_SAYEED
committed
Auto import added
1 parent b32c61a commit c8624a0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

app.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,45 @@
1616
ALLOWED_RESULTS_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif', 'pdf', 'docx'}
1717
ALLOWED_MUSIC_EXTENSIONS = {'mp3', 'wav', 'm4a', 'ogg', 'mp4'}
1818

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+
1958
# ========== FLASK APP ==========
2059
app = Flask(__name__)
2160
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@@ -444,6 +483,8 @@ def delete_dataset_file(property_name, tab, filename):
444483
for rule in app.url_map.iter_rules():
445484
print(rule.endpoint, rule)
446485

486+
auto_import_uploads()
487+
447488
# ========== MAIN ==========
448489
if __name__ == '__main__':
449490
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

0 commit comments

Comments
 (0)