Spaces:
Sleeping

doluong6 commited on
Commit
fc906d1
·
verified ·
1 Parent(s): 2d403ac

Update routes/cap_nhat_hskh.py

Browse files
Files changed (1) hide show
  1. routes/cap_nhat_hskh.py +16 -8
routes/cap_nhat_hskh.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import APIRouter, Request, HTTPException
2
- from huggingface_hub import upload_file
3
  import pandas as pd
4
  import openpyxl
5
  import os
@@ -10,41 +10,49 @@ router = APIRouter()
10
  @router.post("/cap_nhat_hskh")
11
  async def cap_nhat_hskh(request: Request):
12
  try:
 
13
  body = await request.json()
14
- columns = body["columns"]
15
- rows = body["rows"]
16
 
 
17
  df = pd.DataFrame(rows, columns=columns)
18
 
 
 
19
  file_path = hf_hub_download(
20
  repo_id="doluong6/Quatest1",
21
  filename="QLHS_LUU_2025.xlsx",
22
  repo_type="dataset",
23
- token=os.environ["HF_TOKEN"],
24
  cache_dir="/tmp"
25
  )
26
 
 
27
  wb = openpyxl.load_workbook(file_path)
28
  if "HSKH" in wb.sheetnames:
29
- std = wb["HSKH"]
30
- wb.remove(std)
31
  ws = wb.create_sheet("HSKH")
32
 
 
33
  for i, row in enumerate([columns] + rows):
34
  for j, val in enumerate(row):
35
- ws.cell(row=i+1, column=j+1, value=val)
36
 
 
37
  temp_path = os.path.join(tempfile.gettempdir(), "QLHS_LUU_2025.xlsx")
38
  wb.save(temp_path)
39
 
 
40
  upload_file(
41
  path_or_fileobj=temp_path,
42
  path_in_repo="QLHS_LUU_2025.xlsx",
43
  repo_id="doluong6/Quatest1",
44
  repo_type="dataset",
45
- token=os.environ["HF_TOKEN"]
46
  )
47
 
48
  return {"message": "✅ Cập nhật sheet HSKH thành công!"}
 
49
  except Exception as e:
50
  raise HTTPException(status_code=500, detail=f"Lỗi cập nhật HSKH: {str(e)}")
 
1
  from fastapi import APIRouter, Request, HTTPException
2
+ from huggingface_hub import hf_hub_download, upload_file
3
  import pandas as pd
4
  import openpyxl
5
  import os
 
10
  @router.post("/cap_nhat_hskh")
11
  async def cap_nhat_hskh(request: Request):
12
  try:
13
+ # Nhận dữ liệu JSON từ frontend
14
  body = await request.json()
15
+ columns = body.get("columns", [])
16
+ rows = body.get("rows", [])
17
 
18
+ # Chuyển thành DataFrame
19
  df = pd.DataFrame(rows, columns=columns)
20
 
21
+ # Tải file gốc từ Hugging Face dataset
22
+ HF_TOKEN = os.environ["HF_TOKEN"]
23
  file_path = hf_hub_download(
24
  repo_id="doluong6/Quatest1",
25
  filename="QLHS_LUU_2025.xlsx",
26
  repo_type="dataset",
27
+ token=HF_TOKEN,
28
  cache_dir="/tmp"
29
  )
30
 
31
+ # Mở workbook và thay thế sheet "HSKH"
32
  wb = openpyxl.load_workbook(file_path)
33
  if "HSKH" in wb.sheetnames:
34
+ wb.remove(wb["HSKH"])
 
35
  ws = wb.create_sheet("HSKH")
36
 
37
+ # Ghi lại dữ liệu mới
38
  for i, row in enumerate([columns] + rows):
39
  for j, val in enumerate(row):
40
+ ws.cell(row=i + 1, column=j + 1, value=val)
41
 
42
+ # Lưu tạm và upload lại
43
  temp_path = os.path.join(tempfile.gettempdir(), "QLHS_LUU_2025.xlsx")
44
  wb.save(temp_path)
45
 
46
+ # Upload lên Hugging Face dataset
47
  upload_file(
48
  path_or_fileobj=temp_path,
49
  path_in_repo="QLHS_LUU_2025.xlsx",
50
  repo_id="doluong6/Quatest1",
51
  repo_type="dataset",
52
+ token=HF_TOKEN
53
  )
54
 
55
  return {"message": "✅ Cập nhật sheet HSKH thành công!"}
56
+
57
  except Exception as e:
58
  raise HTTPException(status_code=500, detail=f"Lỗi cập nhật HSKH: {str(e)}")