Update app.py
Browse files
app.py
CHANGED
|
@@ -724,30 +724,40 @@ async def quan_ly_hoa_chat():
|
|
| 724 |
@app.post("/cap_nhat_hoa_chat")
|
| 725 |
async def cap_nhat_hoa_chat(request: Request):
|
| 726 |
try:
|
|
|
|
| 727 |
body = await request.json()
|
| 728 |
columns = body["columns"]
|
| 729 |
data = body["data"]
|
| 730 |
|
| 731 |
-
# Tạo
|
| 732 |
df = pd.DataFrame(data, columns=columns)
|
| 733 |
|
| 734 |
-
#
|
| 735 |
for col in df.columns:
|
| 736 |
if "ngày" in col.lower():
|
| 737 |
-
|
|
|
|
|
|
|
|
|
|
| 738 |
|
| 739 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
file_path = "/tmp/Quan_ly_hoa_chat.xlsx"
|
| 741 |
wb = openpyxl.Workbook()
|
| 742 |
ws = wb.active
|
| 743 |
ws.title = "HOA CHAT SU DUNG"
|
| 744 |
|
|
|
|
| 745 |
for r in dataframe_to_rows(df, index=False, header=True):
|
| 746 |
ws.append(r)
|
| 747 |
|
|
|
|
| 748 |
wb.save(file_path)
|
| 749 |
|
| 750 |
-
#
|
| 751 |
upload_file(
|
| 752 |
path_or_fileobj=file_path,
|
| 753 |
path_in_repo="Quan_ly_hoa_chat.xlsx",
|
|
@@ -756,6 +766,8 @@ async def cap_nhat_hoa_chat(request: Request):
|
|
| 756 |
token=os.environ["HF_TOKEN"]
|
| 757 |
)
|
| 758 |
|
| 759 |
-
return {"message": "Đã
|
|
|
|
| 760 |
except Exception as e:
|
| 761 |
-
|
|
|
|
|
|
| 724 |
@app.post("/cap_nhat_hoa_chat")
|
| 725 |
async def cap_nhat_hoa_chat(request: Request):
|
| 726 |
try:
|
| 727 |
+
# Đọc dữ liệu từ request
|
| 728 |
body = await request.json()
|
| 729 |
columns = body["columns"]
|
| 730 |
data = body["data"]
|
| 731 |
|
| 732 |
+
# Tạo DataFrame mới
|
| 733 |
df = pd.DataFrame(data, columns=columns)
|
| 734 |
|
| 735 |
+
# Chuyển các cột ngày về đúng định dạng
|
| 736 |
for col in df.columns:
|
| 737 |
if "ngày" in col.lower():
|
| 738 |
+
try:
|
| 739 |
+
df[col] = pd.to_datetime(df[col], errors="coerce").dt.strftime('%Y-%m-%d')
|
| 740 |
+
except Exception as e:
|
| 741 |
+
print(f"Lỗi xử lý ngày ở cột {col}: {e}")
|
| 742 |
|
| 743 |
+
# Loại bỏ đuôi .0 trong cột STT
|
| 744 |
+
if "STT" in df.columns:
|
| 745 |
+
df["STT"] = df["STT"].astype(str).str.replace(r"\.0$", "", regex=True)
|
| 746 |
+
|
| 747 |
+
# Ghi đè dữ liệu vào file Excel mới
|
| 748 |
file_path = "/tmp/Quan_ly_hoa_chat.xlsx"
|
| 749 |
wb = openpyxl.Workbook()
|
| 750 |
ws = wb.active
|
| 751 |
ws.title = "HOA CHAT SU DUNG"
|
| 752 |
|
| 753 |
+
# Ghi dữ liệu từ DataFrame vào sheet
|
| 754 |
for r in dataframe_to_rows(df, index=False, header=True):
|
| 755 |
ws.append(r)
|
| 756 |
|
| 757 |
+
# Lưu file tạm
|
| 758 |
wb.save(file_path)
|
| 759 |
|
| 760 |
+
# Ghi đè lên dataset Hugging Face
|
| 761 |
upload_file(
|
| 762 |
path_or_fileobj=file_path,
|
| 763 |
path_in_repo="Quan_ly_hoa_chat.xlsx",
|
|
|
|
| 766 |
token=os.environ["HF_TOKEN"]
|
| 767 |
)
|
| 768 |
|
| 769 |
+
return {"message": "Đã lưu dữ liệu mới thành công!"}
|
| 770 |
+
|
| 771 |
except Exception as e:
|
| 772 |
+
print("❌ Lỗi khi cập nhật hóa chất:\n", traceback.format_exc())
|
| 773 |
+
raise HTTPException(status_code=500, detail=f"Lỗi khi lưu: {str(e)}")
|