Spaces:
Sleeping
Sleeping
Youngger9765 commited on
Commit ·
f66e0a4
1
Parent(s): 6dccf0d
更新 app.py 檔案,簡化 HTML 預覽 API 的日誌輸出
Browse files- 移除多餘的日誌輸出,減少不必要的控制台訊息,提升程式碼可讀性
- 確保在返回生成的 HTML 時,仍然保留必要的調試資訊
此次更新旨在簡化程式碼,提升效能與可維護性。
app.py
CHANGED
|
@@ -104,37 +104,17 @@ async def preview_html(
|
|
| 104 |
):
|
| 105 |
"""API endpoint to preview HTML with variables"""
|
| 106 |
try:
|
| 107 |
-
# Log 1: 接收到的原始資料
|
| 108 |
-
print("=== API Preview Called ===")
|
| 109 |
-
print(f"Template length: {len(template_content)} characters")
|
| 110 |
-
print(f"Template preview: {template_content[:200]}...")
|
| 111 |
-
print(f"Variables received: {variables}")
|
| 112 |
-
|
| 113 |
-
# Log 2: 解析 JSON
|
| 114 |
variables_dict = json.loads(variables)
|
| 115 |
-
print(f"\nParsed variables: {json.dumps(variables_dict, indent=2, ensure_ascii=False)}")
|
| 116 |
-
|
| 117 |
-
# Log 3: 獲取額外資料
|
| 118 |
awards_data = get_sheets_data()
|
| 119 |
-
print(f"\nAwards data count: {len(awards_data)}")
|
| 120 |
|
| 121 |
-
# Log 4: 添加額外變數
|
| 122 |
variables_dict['awards_data'] = awards_data
|
| 123 |
variables_dict['current_year'] = datetime.now().year
|
| 124 |
-
print(f"\nAdded current_year: {variables_dict['current_year']}")
|
| 125 |
-
|
| 126 |
-
# Log 5: 所有變數的 keys
|
| 127 |
-
print(f"\nAll variable keys: {list(variables_dict.keys())}")
|
| 128 |
|
| 129 |
-
# Log 6: 渲染模板
|
| 130 |
from jinja2 import Template
|
| 131 |
template = Template(template_content)
|
| 132 |
generated_html = template.render(**variables_dict)
|
| 133 |
-
print(f"\nGenerated HTML length: {len(generated_html)} characters")
|
| 134 |
-
print(f"Generated HTML preview: {generated_html[:300]}...")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
return_data = {
|
| 138 |
"html": generated_html,
|
| 139 |
"debug": {
|
| 140 |
"variables_count": len(variables_dict),
|
|
@@ -142,15 +122,8 @@ async def preview_html(
|
|
| 142 |
"template_length": len(template_content)
|
| 143 |
}
|
| 144 |
}
|
| 145 |
-
print(f"\nReturning success with HTML length: {len(generated_html)}")
|
| 146 |
-
|
| 147 |
-
return return_data
|
| 148 |
except Exception as e:
|
| 149 |
-
print(f"\n!!! ERROR in preview_html: {str(e)}")
|
| 150 |
-
print(f"Error type: {type(e).__name__}")
|
| 151 |
import traceback
|
| 152 |
-
print(f"Traceback: {traceback.format_exc()}")
|
| 153 |
-
|
| 154 |
return {
|
| 155 |
"error": str(e),
|
| 156 |
"html": "",
|
|
|
|
| 104 |
):
|
| 105 |
"""API endpoint to preview HTML with variables"""
|
| 106 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
variables_dict = json.loads(variables)
|
|
|
|
|
|
|
|
|
|
| 108 |
awards_data = get_sheets_data()
|
|
|
|
| 109 |
|
|
|
|
| 110 |
variables_dict['awards_data'] = awards_data
|
| 111 |
variables_dict['current_year'] = datetime.now().year
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
| 113 |
from jinja2 import Template
|
| 114 |
template = Template(template_content)
|
| 115 |
generated_html = template.render(**variables_dict)
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
return {
|
|
|
|
| 118 |
"html": generated_html,
|
| 119 |
"debug": {
|
| 120 |
"variables_count": len(variables_dict),
|
|
|
|
| 122 |
"template_length": len(template_content)
|
| 123 |
}
|
| 124 |
}
|
|
|
|
|
|
|
|
|
|
| 125 |
except Exception as e:
|
|
|
|
|
|
|
| 126 |
import traceback
|
|
|
|
|
|
|
| 127 |
return {
|
| 128 |
"error": str(e),
|
| 129 |
"html": "",
|