Upload 2 files
Browse files
plugins/file_handlers/csv_handler.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""CSV File Handler Plugin"""
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
class CSVHandler:
|
| 8 |
+
"""Handle CSV files with auto-detection."""
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.supported_extensions = ['.csv', '.tsv', '.txt']
|
| 11 |
+
|
| 12 |
+
def can_handle(self, filename: str) -> bool:
|
| 13 |
+
return Path(filename).suffix.lower() in self.supported_extensions
|
| 14 |
+
|
| 15 |
+
def load(self, file_path: str) -> Dict[str, Any]:
|
| 16 |
+
delimiter = ','
|
| 17 |
+
try:
|
| 18 |
+
for d in [',', '\t', ';', '|']:
|
| 19 |
+
try:
|
| 20 |
+
df = pd.read_csv(file_path, delimiter=d, nrows=5)
|
| 21 |
+
if len(df.columns) > 1:
|
| 22 |
+
delimiter = d
|
| 23 |
+
df = pd.read_csv(file_path, delimiter=delimiter)
|
| 24 |
+
break
|
| 25 |
+
except: continue
|
| 26 |
+
metadata = {"total_rows": len(df), "delimiter": delimiter}
|
| 27 |
+
return {"success": True, "data": df, "metadata": metadata, "file_type": "csv"}
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return {"success": False, "error": f"Failed to load CSV: {str(e)}", "file_type": "csv"}
|
| 30 |
+
|
| 31 |
+
def preview(self, data: Dict[str, Any], max_rows: int = 5) -> str:
|
| 32 |
+
if not data.get("success"):
|
| 33 |
+
return f"<p class='text-red-500'>Error: {data.get('error')}</p>"
|
| 34 |
+
html = f"<h4>📄 CSV File</h4><p><strong>Rows:</strong> {data['metadata']['total_rows']:,}</p>"
|
| 35 |
+
df = data["data"]
|
| 36 |
+
html += df.head(max_rows).to_html(index=False, classes="preview-table border w-full text-xs")
|
| 37 |
+
return html
|
plugins/file_handlers/excel_handler.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Excel File Handler Plugin"""
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
class ExcelHandler:
|
| 8 |
+
"""Handle Excel files with multi-sheet support."""
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.supported_extensions = ['.xlsx', '.xls', '.xlsm']
|
| 11 |
+
|
| 12 |
+
def can_handle(self, filename: str) -> bool:
|
| 13 |
+
return Path(filename).suffix.lower() in self.supported_extensions
|
| 14 |
+
|
| 15 |
+
def load(self, file_path: str) -> Dict[str, Any]:
|
| 16 |
+
try:
|
| 17 |
+
excel_file = pd.ExcelFile(file_path)
|
| 18 |
+
sheets = {}
|
| 19 |
+
for sheet_name in excel_file.sheet_names:
|
| 20 |
+
sheets[sheet_name] = pd.read_excel(file_path, sheet_name=sheet_name)
|
| 21 |
+
combined_df = pd.concat(sheets.values(), ignore_index=True)
|
| 22 |
+
metadata = {"sheet_count": len(sheets), "total_rows": len(combined_df)}
|
| 23 |
+
return {"success": True, "sheets": sheets, "combined": combined_df, "metadata": metadata, "file_type": "excel"}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"success": False, "error": f"Failed to load Excel: {str(e)}", "file_type": "excel"}
|
| 26 |
+
|
| 27 |
+
def preview(self, data: Dict[str, Any], max_rows: int = 5) -> str:
|
| 28 |
+
if not data.get("success"):
|
| 29 |
+
return f"<p class='text-red-500'>Error: {data.get('error')}</p>"
|
| 30 |
+
html = f"<h4>📊 Excel File</h4><p><strong>Sheets:</strong> {data['metadata']['sheet_count']}</p>"
|
| 31 |
+
html += f"<p><strong>Total Rows:</strong> {data['metadata']['total_rows']:,}</p>"
|
| 32 |
+
df = data["combined"]
|
| 33 |
+
html += "<h5>Combined Data Preview:</h5>"
|
| 34 |
+
html += df.head(max_rows).to_html(index=False, classes="preview-table border w-full text-xs")
|
| 35 |
+
return html
|