63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
|
from flask import Flask, request, jsonify, render_template
|
||
|
import subprocess
|
||
|
import sys
|
||
|
from datetime import datetime
|
||
|
import os
|
||
|
import json
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
@app.route('/')
|
||
|
def home():
|
||
|
return render_template('index.html')
|
||
|
|
||
|
@app.route('/calculate', methods=['POST'])
|
||
|
def calculate():
|
||
|
date_str = request.form.get('date')
|
||
|
time_str = request.form.get('time', '00:00:00') # 默认为00:00:00
|
||
|
|
||
|
try:
|
||
|
# 验证日期和时间格式
|
||
|
datetime.strptime(date_str, '%Y-%m-%d') # 先单独验证日期
|
||
|
datetime.strptime(time_str, '%H:%M:%S') # 再验证时间
|
||
|
|
||
|
# 组合日期和时间
|
||
|
datetime_str = f"{date_str} {time_str}"
|
||
|
|
||
|
api_path = os.path.join(os.path.dirname(__file__), 'API.py')
|
||
|
result = subprocess.run(
|
||
|
[sys.executable, api_path, datetime_str],
|
||
|
capture_output=True,
|
||
|
text=True, # 确保输出是文本
|
||
|
check=True
|
||
|
)
|
||
|
|
||
|
# 打印完整输出用于调试
|
||
|
print("=== API.py 原始输出 ===")
|
||
|
print("stdout:", result.stdout)
|
||
|
print("stderr:", result.stderr)
|
||
|
|
||
|
try:
|
||
|
json_output = json.loads(result.stdout)
|
||
|
return jsonify({'success': True, 'result': json_output})
|
||
|
except json.JSONDecodeError as e:
|
||
|
print("JSON 解析失败:", e)
|
||
|
print("原始输出:", result.stdout)
|
||
|
return jsonify({
|
||
|
'success': False,
|
||
|
'error': f'JSON解析失败: {str(e)}',
|
||
|
'raw_output': result.stdout # 返回原始输出用于调试
|
||
|
})
|
||
|
|
||
|
except ValueError:
|
||
|
return jsonify({'success': False, 'error': '无效的日期格式'})
|
||
|
except subprocess.CalledProcessError as e:
|
||
|
return jsonify({
|
||
|
'success': False,
|
||
|
'error': f'计算失败: {e.stderr}',
|
||
|
'stdout': e.stdout
|
||
|
})
|
||
|
except Exception as e:
|
||
|
return jsonify({'success': False, 'error': str(e)})
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(debug=True,host='0.0.0.0',port=5000)
|