71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
from flask import Flask, render_template, jsonify, send_file, request
|
||
from pathlib import Path
|
||
import sys
|
||
import os
|
||
|
||
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
||
|
||
from src.ppt_generator import PPTGenerator
|
||
from loguru import logger
|
||
|
||
app = Flask(__name__)
|
||
app.config['JSON_AS_ASCII'] = False
|
||
|
||
generator = PPTGenerator()
|
||
|
||
@app.route('/')
|
||
def index():
|
||
projects = generator.list_projects()
|
||
return render_template('index.html', projects=projects)
|
||
|
||
@app.route('/api/projects')
|
||
def list_projects():
|
||
projects = generator.list_projects()
|
||
return jsonify({'success': True, 'projects': projects})
|
||
|
||
@app.route('/api/generate/<project_name>', methods=['POST'])
|
||
def generate_ppt(project_name):
|
||
try:
|
||
output_path = generator.generate_project(project_name)
|
||
if output_path:
|
||
filename = os.path.basename(output_path)
|
||
return jsonify({
|
||
'success': True,
|
||
'message': 'PPT生成成功',
|
||
'filename': filename,
|
||
'download_url': f'/download/{filename}'
|
||
})
|
||
else:
|
||
return jsonify({'success': False, 'message': 'PPT生成失败,请查看日志'})
|
||
except Exception as e:
|
||
logger.exception(f"生成PPT时发生错误: {e}")
|
||
return jsonify({'success': False, 'message': str(e)})
|
||
|
||
@app.route('/download/<filename>')
|
||
def download_file(filename):
|
||
output_dir = Path(__file__).parent / "output"
|
||
file_path = output_dir / filename
|
||
if file_path.exists():
|
||
return send_file(str(file_path), as_attachment=True)
|
||
return jsonify({'success': False, 'message': '文件不存在'})
|
||
|
||
@app.route('/api/files')
|
||
def list_files():
|
||
output_dir = Path(__file__).parent / "output"
|
||
files = []
|
||
if output_dir.exists():
|
||
for f in sorted(output_dir.glob("*.pptx"), reverse=True):
|
||
files.append({
|
||
'name': f.name,
|
||
'size': round(f.stat().st_size / 1024 / 1024, 2),
|
||
'modified': f.stat().st_mtime
|
||
})
|
||
return jsonify({'success': True, 'files': files})
|
||
|
||
if __name__ == '__main__':
|
||
print("\n" + "="*60)
|
||
print("PPT管理系统 Web 界面启动中...")
|
||
print("请在浏览器中打开: http://localhost:5000")
|
||
print("="*60 + "\n")
|
||
app.run(debug=True, host='0.0.0.0', port=5000)
|