You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask页面点击按钮后Div内容丢失问题求助

Flask页面区块内容丢失问题

我在Flask项目开发中尝试展示两个不同div区块的内容,但点击「Show Text」按钮后,另一区块的分析结果内容会消失。需要实现的功能是:点击「Analyze」或「Show Text」按钮时,各自对应的内容需分别保留。

后端代码

@app.route('/predict', methods=['POST', 'GET'])
def predict():
    email = request.form.get('email', '')
    
    if request.method == 'POST':
        if not email:
            return render_template('testing.html', error_message='Please enter an email.', email=email)
        else:
            count_of_words = len(email.split())

            # 执行情感分析
            result = emotion(email)  # 情感分析标签
            sorted_labels = sort_emo(result)  # 降序排序,提取前5个标签

            # 清洗文本
            clean_text = clean_html(email)
            processed_text = clean_string(clean_text)

            # 执行垃圾邮件检测预测
            string_vectorized = vectorizer.transform([processed_text])
            my_prediction = model.predict(string_vectorized)
            probability = model.predict_proba(string_vectorized)[0][1]
            percentage = round(probability * 100, 2)

            # 情感标签首字母大写处理
            capitalized_labels = []
            scores = []

            # 提取前5个标签数据
            for res in sorted_labels:
                label = res['label']
                capitalized_label = label.capitalize()
                score = round(res['score'] * 100, 2)
                capitalized_labels.append(capitalized_label)
                scores.append(score)

            # 合并标签与分数列表
            emo_data = zip(capitalized_labels, scores)
            
            return render_template('testing.html', prediction=my_prediction, percentage=percentage, result=count_of_words, email=email, emo_data=emo_data)

    # GET请求或未提交表单时的默认渲染
    return render_template('testing.html', email=email)

@app.route('/highlight_text', methods=['POST'])
def highlight_text():
    email = request.form.get('email', '')

    if not email:
        return render_template('testing.html', error_message='Please enter an email.', email=email)

    # 生成高亮文本的预处理流程
    processed_text = clean_string(clean_html(email))
    emotion_lists = process_text_emotions(processed_text)
    reduced_emotions = create_reduced_emotions(emotion_lists)
    highlighted_text = generate_html_with_highlights(email, reduced_emotions)

    # 获取渲染模板所需的相关变量
    prediction = request.form.get('prediction')
    percentage = request.form.get('percentage')
    result = request.form.get('result')
    emo_data = request.form.getlist('emo_data')

    return render_template('testing.html', highlighted_text=highlighted_text, email=email, prediction=prediction, percentage=percentage, result=result, emo_data=emo_data)

前端代码

<div class="right-element">
    {% if prediction is defined %}
    <div class="result box">
        <h5>Prediction {% if prediction == 1 %}<span style="color: red;">Spam</span>{% elif prediction == 0 %}
            <span style="color: green;">Not Spam</span>{% endif %}</h5>
        <h5>Spam Probability {{ percentage }} %</h5>
        <h5>Word Count {{ result }}</h5>
        <br>
        <h5>Emotional Analysis:</h5>
        <table class="table">
            <thead>
                <tr>
                    <th>Emotion Label</th>
                    <th>Score</th>
                </tr>
            </thead>
            <tbody>
                {% for label, score in emo_data %}
                <tr>
                    <td>{{ label }}</td>
                    <td>{{ score }}%</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% endif %}
</div>

<div class="highlighted-text">
    <h3>Highlighted text for Emotions</h3>
    <p>{{ highlighted_text | safe }}</p>

    <form action="{{ url_for('highlight_text') }}" method="post" id="highlight-form">
        <input type="hidden" name="email" value="{{ email }}">
        <button type="submit" id="highlight-btn" name="highlight-btn">Show Text</button>
    </form>
</div>

现象截图

点击Analyze时的页面
点击Show Text时的页面


内容的提问来源于stack exchange,提问作者abdz_128

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 10:12:28