from flask import Flask, render_template, request, redirect, url_for
from collections import Counter
from cppy.cp_util import *
import os

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # 获取上传的文件
        file = request.files['file']
        
        # 保存临时文件并读取内容
        filename = os.path.join('/temp', file.filename)
        file.save(filename)

        # 计算词频
        words = extract_file_words(filename)
        word_counts = Counter(words)

        # 删除临时文件
        os.remove(filename)

        return render_template('result.html', word_counts=word_counts.most_common())

    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)