在Flask中保留信息

huangapple go评论100阅读模式
英文:

Information retaining in Flask

问题

我正在尝试同时显示两个不同 div 块的内容。但是,当我点击 "Show Text" 后,另一个 div 块的内容就会消失。(参见图像以供参考)

我希望它可以这样运行:如果我点击 "Analyze" 或 "Show Text" 中的任何一个,内容应分别保留。

后端代码:

  1. @app.route('/predict', methods=['POST', 'GET'])
  2. def predict():
  3. email = request.form.get('email', '')
  4. if request.method == 'POST':
  5. # ... 其他代码 ...
  6. return render_template('testing.html', prediction=my_prediction, percentage=percentage, result=count_of_words, email=email, emo_data=emo_data)
  7. return render_template('testing.html', email=email)
  8. @app.route('/highlight_text', methods=['POST'])
  9. def highlight_text():
  10. email = request.form.get('email', '')
  11. if not email:
  12. return render_template('testing.html', error_message='Please enter an email.', email=email)
  13. # ... 其他代码 ...
  14. return render_template('testing.html', highlighted_text=highlighted_text, email=email, prediction=prediction, percentage=percentage, result=result, emo_data=emo_data)

前端代码:

  1. <div class="right-element">
  2. {% if prediction is defined %}
  3. <div class="result box">
  4. <h5>Prediction {% if prediction == 1 %}<span style="color: red;">Spam</span>{% elif prediction == 0 %}
  5. <span style="color: green;">Not Spam</span>{% endif %}</h5>
  6. <h5>Spam Probability {{ percentage }} %</h5>
  7. <h5>Word Count {{ result }}</h5>
  8. <br>
  9. <h5>Emotional Analysis:</h5>
  10. <table class="table">
  11. <thead>
  12. <tr>
  13. <th>Emotion Label</th>
  14. <th>Score</th>
  15. </tr>
  16. </thead>
  17. <tbody>
  18. {% for label, score in emo_data %}
  19. <tr>
  20. <td>{{ label }}</td>
  21. <td>{{ score }}%</td>
  22. </tr>
  23. {% endfor %}
  24. </tbody>
  25. </table>
  26. </div>
  27. {% endif %}
  28. </div>
  29. <div class="highlighted-text">
  30. <h3>Highlighted text for Emotions</h3>
  31. <p>{{ highlighted_text | safe }}</p>
  32. <form action="{{ url_for('highlight_text') }}" method="post" id="highlight-form">
  33. <input type="hidden" name="email" value="{{ email }}">
  34. <button type="submit" id="highlight-btn" name="highlight-btn">Show Text</button>
  35. </form>
  36. </div>

Images:
在Flask中保留信息
在Flask中保留信息

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to display both the contents of two different div blocks. But after I click on &quot;Show Text&quot; the other div block content disappears. (See images for reference)
  4. I want to make it function such that if I click on either &#39;Analyze&#39; or &#39;Show Text&#39;, the contain must be retained for either of the two respectively.
  5. Backend code:
  6. @app.route(&#39;/predict&#39;, methods=[&#39;POST&#39;, &#39;GET&#39;])
  7. def predict():
  8. email = request.form.get(&#39;email&#39;, &#39;&#39;)
  9. if request.method == &#39;POST&#39;:
  10. if not email:
  11. return render_template(&#39;testing.html&#39;, error_message=&#39;Please enter an email.&#39;, email=email)
  12. else:
  13. count_of_words = len(email.split())
  14. # Perform Emotional Analysis
  15. result = emotion(email) # Emotional Analysis Labels
  16. sorted_labels = sort_emo(result) # Sorting in Descending order; extracts top 5 labels
  17. # Clean the text
  18. clean_text = clean_html(email)
  19. processed_text = clean_string(clean_text)
  20. # Perform Spam Detection prediction
  21. string_vectorized = vectorizer.transform([processed_text])
  22. my_prediction = model.predict(string_vectorized)
  23. probability = model.predict_proba(string_vectorized)[0][1]
  24. percentage = round(probability * 100, 2)
  25. # Capitalize emotion labels
  26. capitalized_labels = []
  27. scores = []
  28. # For extracting top 5 labels
  29. for res in sorted_labels:
  30. label = res[&#39;label&#39;]
  31. capitalized_label = label.capitalize()
  32. score = round(res[&#39;score&#39;] * 100, 2)
  33. capitalized_labels.append(capitalized_label)
  34. scores.append(score)
  35. # Zipping the lists together
  36. emo_data = zip(capitalized_labels, scores)
  37. return render_template(&#39;testing.html&#39;, prediction=my_prediction, percentage=percentage, result=count_of_words, email=email, emo_data=emo_data)
  38. # For GET request or if no form submission has occurred
  39. return render_template(&#39;testing.html&#39;, email=email)
  40. @app.route(&#39;/highlight_text&#39;, methods=[&#39;POST&#39;])
  41. def highlight_text():
  42. email = request.form.get(&#39;email&#39;, &#39;&#39;)
  43. if not email:
  44. return render_template(&#39;testing.html&#39;, error_message=&#39;Please enter an email.&#39;, email=email)
  45. # Perform necessary processing to generate highlighted text
  46. processed_text = clean_string(clean_html(email))
  47. emotion_lists = process_text_emotions(processed_text)
  48. reduced_emotions = create_reduced_emotions(emotion_lists)
  49. highlighted_text = generate_html_with_highlights(email, reduced_emotions)
  50. # Fetch the relevant variables for rendering the template
  51. prediction = request.form.get(&#39;prediction&#39;)
  52. percentage = request.form.get(&#39;percentage&#39;)
  53. result = request.form.get(&#39;result&#39;)
  54. emo_data = request.form.getlist(&#39;emo_data&#39;)
  55. return render_template(&#39;testing.html&#39;, highlighted_text=highlighted_text, email=email, prediction=prediction, percentage=percentage, result=result, emo_data=emo_data)
  56. Frontend:
  57. &lt;div class=&quot;right-element&quot;&gt;
  58. {% if prediction is defined %}
  59. &lt;div class=&quot;result box&quot;&gt;
  60. &lt;h5&gt;Prediction {% if prediction == 1 %}&lt;span style=&quot;color: red;&quot;&gt;Spam&lt;/span&gt;{% elif prediction == 0 %}
  61. &lt;span style=&quot;color: green;&quot;&gt;Not Spam&lt;/span&gt;{% endif %}&lt;/h5&gt;
  62. &lt;h5&gt;Spam Probability {{ percentage }} %&lt;/h5&gt;
  63. &lt;h5&gt;Word Count {{ result }}&lt;/h5&gt;
  64. &lt;br&gt;
  65. &lt;h5&gt;Emotional Analysis:&lt;/h5&gt;
  66. &lt;table class=&quot;table&quot;&gt;
  67. &lt;thead&gt;
  68. &lt;tr&gt;
  69. &lt;th&gt;Emotion Label&lt;/th&gt;
  70. &lt;th&gt;Score&lt;/th&gt;
  71. &lt;/tr&gt;
  72. &lt;/thead&gt;
  73. &lt;tbody&gt;
  74. {% for label, score in emo_data %}
  75. &lt;tr&gt;
  76. &lt;td&gt;{{ label }}&lt;/td&gt;
  77. &lt;td&gt;{{ score }}%&lt;/td&gt;
  78. &lt;/tr&gt;
  79. {% endfor %}
  80. &lt;/tbody&gt;
  81. &lt;/table&gt;
  82. &lt;/div&gt;
  83. {% endif %}
  84. &lt;/div&gt;
  85. &lt;div class=&quot;highlighted-text&quot;&gt;
  86. &lt;h3&gt;Highlighted text for Emotions&lt;/h3&gt;
  87. &lt;p&gt;{{ highlighted_text | safe }}&lt;/p&gt;
  88. &lt;form action=&quot;{{ url_for(&#39;highlight_text&#39;) }}&quot; method=&quot;post&quot; id=&quot;highlight-form&quot;&gt;
  89. &lt;input type=&quot;hidden&quot; name=&quot;email&quot; value=&quot;{{ email }}&quot;&gt;
  90. &lt;button type=&quot;submit&quot; id=&quot;highlight-btn&quot; name=&quot;highlight-btn&quot;&gt;Show Text&lt;/button&gt;
  91. &lt;/form&gt;
  92. &lt;/div&gt;
  93. Images:
  94. [![When Analyze is clicked][1]][1]
  95. [![When Show Text is clicked][2]][2]
  96. [1]: https://i.stack.imgur.com/XYGDB.png
  97. [2]: https://i.stack.imgur.com/Cw46d.png
  98. </details>
  99. # 答案1
  100. **得分**: 1
  101. 1. ```Show Text``` 提交您的表单。
  102. 2. 该表单中唯一的字段是一个用于电子邮件的隐藏字段。表单不包含```percentage, result```字段,这意味着以下行将返回```None```。
  103. ```python
  104. prediction = request.form.get('prediction')
  105. percentage = request.form.get('percentage')
  1. 要实现您的要求,您需要将percentage, result也放在表单中,或者保持您的设计不变,通过Javascript和Ajax调用提交您的表单。如果您进行Ajax调用,这意味着您的HTML页面不会被重新加载。然后,您可以简单地将通过Javascript和Ajax调用完成的表单调用的结果附加到您想要的位置。在Stackoverflow上进行快速搜索将向您展示如何通过Javascript和Ajax调用提交表单。
英文:
  1. Show Text submits your form.

  2. The only field in that form is a hidden field for email. The form doesn't contain the fields for percentage, result which means that the following lines will return None

  1. prediction = request.form.get(&#39;prediction&#39;)
  2. percentage = request.form.get(&#39;percentage&#39;)
  1. To achieve your requirement, you need to place percentage, result in the form too or keep your design as is, and submit your form via Javascript and Ajax call. If you do an Ajax call, it means your html page won't be reloaded. You can then simply append the result of the form call (the one you did via Javascript and Ajax) to the places you want. A quick search on Stackoverflow will show you how to submit a form via Javascript and Ajax call

huangapple
  • 本文由 发表于 2023年7月12日 20:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定