mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-09 17:57:06 +08:00
feat(datasource): commit any code for excel
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, File, UploadFile
|
||||
from fastapi import APIRouter, File, UploadFile, HTTPException
|
||||
from ..crud.datasource import get_datasource_list, check_status, create_ds, update_ds, delete_ds, getTables, getFields, \
|
||||
execSql, update_table_and_fields, getTablesByDs, chooseTables, preview
|
||||
from common.core.deps import SessionDep
|
||||
@@ -6,6 +6,9 @@ from ..models.datasource import CoreDatasource, CreateDatasource, EditObj, CoreT
|
||||
from ..crud.table import get_tables_by_ds_id
|
||||
from ..crud.field import get_fields_by_table_id
|
||||
from typing import List
|
||||
import uuid
|
||||
import pandas as pd
|
||||
import hashlib
|
||||
|
||||
router = APIRouter(tags=["datasource"], prefix="/datasource")
|
||||
|
||||
@@ -80,7 +83,27 @@ async def edit_local(session: SessionDep, id: int, data: EditObj):
|
||||
return preview(session, id, data)
|
||||
|
||||
|
||||
@router.post("/uploadExcel/")
|
||||
@router.post("/uploadExcel")
|
||||
async def upload_excel(session: SessionDep, file: UploadFile = File(...)):
|
||||
ALLOWED_EXTENSIONS = {"xlsx", "xls", "csv"}
|
||||
if not file.filename.lower().endswith(tuple(ALLOWED_EXTENSIONS)):
|
||||
raise HTTPException(400, "Only support .xlsx/.xls/.csv")
|
||||
|
||||
contents = await file.read()
|
||||
df_sheets = pd.read_excel(contents, sheet_name=None)
|
||||
# build columns and data to insert db
|
||||
# todo
|
||||
pass
|
||||
sheets = []
|
||||
for sheet_name, df in df_sheets.items():
|
||||
print("--------------------")
|
||||
print(f"Sheet: {sheet_name}, {hashlib.sha256(uuid.uuid4().bytes).hexdigest()[:10]}")
|
||||
sheets.append({"name": f"{sheet_name}",
|
||||
"uniqueName": f"{sheet_name}_{hashlib.sha256(uuid.uuid4().bytes).hexdigest()[:10]}"})
|
||||
column_len = len(df.dtypes)
|
||||
for i in range(column_len):
|
||||
print(f"{df.columns[i]} , {df.dtypes[i]}")
|
||||
|
||||
for row in df.values:
|
||||
print(row)
|
||||
|
||||
return {"filename": file.filename, "sheets": sheets}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Author: Junjun
|
||||
# Date: 2025/5/19
|
||||
@@ -29,7 +29,9 @@ dependencies = [
|
||||
"pymysql (>=1.1.1,<2.0.0)",
|
||||
"cryptography (>=44.0.3,<45.0.0)",
|
||||
"llama_index>=0.12.35",
|
||||
"pymssql (>=2.3.4,<3.0.0)"
|
||||
"pymssql (>=2.3.4,<3.0.0)",
|
||||
"pandas (>=2.2.3,<3.0.0)",
|
||||
"openpyxl (>=3.1.5,<4.0.0)"
|
||||
]
|
||||
[[tool.uv.index]]
|
||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
||||
|
||||
Reference in New Issue
Block a user