使用 Flask 在网络中开发统计分析

问题描述 投票:0回答:1

在此向大家问好!

我的项目如下:让我们假设我们有一些产品商店,顾客要选择任何产品并写评论,我们每天可能有数百条评论,商店经理决定分析这些评论,确定它们的极性和返回无论是积极的还是消极的,并以图形方式显示这些结果,以保持他的业务活跃和有效,项目由两部分组成:

第 1 部分:我们必须构建网页,以显示用户撰写评论的可能性

第2部分:使用python和flask我们应该能够连接表单,检索这些注释并使用变压器确定它们的极性

使用以下代码成功实现了第一部分和第二部分: 第一个是html部分

            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>Sell Weapons</title>
            </head>
            <body>
            <form action="{{ url_for('predict')}}"method="post">
                <label for="comment1">evaluate first product</label><br>
                <input type="text" id="comment1" name="comment1" required="required"><br>
                  <label for="comment2">evaluate second product</label><br>
                <input type="text" id="comment2" name="comment2" required="required"><br>
                  <label for="comment3">evaluate third product</label><br>
                <input type="text" id="comment3" name="comment3" required="required"><br>
                 <input type="submit" value="Submit">
            </form>
            
             <h4 style="color:Violet;">
               {{ prediction_text }} </h4>
            
            
            </body>
            </html>

结果是: filling form

基于此表单,我创建了以下 app.py 文件

            import numpy as np
            from flask import Flask,request,jsonify,render_template
            import pickle
            from transformers import pipeline
            from collections import Counter
            import matplotlib.pyplot as plt
            sentiment_pipeline = pipeline("sentiment-analysis")
            app =Flask(__name__)
            @app.route('/')
            def home():
                return render_template("Commenting.html")
            
            @app.route('/predict',methods=['POST'])
            # @app.route('/')
            def predict():
                text =  [x for x in request.form.values()]
                print(text)
                status = []
                for sentence in text:
                    print(sentence)
                    status.append(sentiment_pipeline(sentence)[0]['label'])
                print(status)
                # data = {'Task': 'Hours per Day', 'Work': 22, 'Eat': 4, 'Commute': 6, 'Watching TV': 5, 'Sleeping': 15}
                data = Counter(status)
                print(data)
                data ={"Positive":list(data.values())[0],"Negative":list(data.values())[1]}
                positive_text =list(data .keys())[0]
                positive_frequency =list(data .values())[0]
                negative_text = list(data.keys())[1]
                negative_frequency = list(data.values())[1]
                total =positive_frequency+negative_frequency
                text_sho =(f'{positive_text}  takes {positive_frequency/(positive_frequency+negative_frequency)*100 } % '
                           f'and {negative_text} takes {negative_frequency/(positive_frequency+negative_frequency)*100} %')
            
                # sentiment_pipeline = pipeline("sentiment-analysis")
                # status = []
                # for sentence in text:
                #     status.append(sentiment_pipeline(sentence)[0]['label'])
                #     # print(sentiment_pipeline(sentence)[0])
                # emotion_counter = Counter(status)
                # plt.pie([float(v) for v in emotion_counter.values()], labels=[k for k in emotion_counter],
                #         autopct="%2.3f%%")
                # plt.savefig("templates/sentiment_distribution.png")
                # plt.show()
                # values =[float(v) for v in emotion_counter.values()]
                # labels = [k for k in emotion_counter]
                #
            
                # text=list(text)
                return render_template('Commenting.html',prediction_text=text_sho)
                # sentiment_pipeline = pipeline("sentiment-analysis")
                # result =sentiment_pipeline(text)[0]
                # if result['label']=='POSITIVE':
                #     return render_template('Commenting.html',prediction_text=f'emotion of comment is positive')
                # else:
                #     return render_template('Commenting.html', prediction_text=f'emotion of comment is not positive')
            if __name__ =="__main__":
                app.run()

当我们运行它时,得到以下结果:

result

所以问题是:我如何生成图表?我使用几个 Chart.js 文件,如下所示: 图表

但是如何将其应用到我的项目中呢?请帮助我

python flask jinja2
1个回答
0
投票

您的问题标题与您提出的问题不太相关。此外,除了通过 Flask 提供生成的内容之外,Flask 与图表关系不大。

向用户显示图表可以通过以下方式完成:

  • 生成图像(使用 matplotlib、seaborn 等)并将该图像嵌入到 html 中。简单、静态。
  • 使用多个 js 库,包括您提到的那个 (chart.js)。更难、更好的用户体验、交互性等。

根据您的选择,您必须调整您的应用程序以提供静态内容。一种方法可能是创建图像并将该图像保存在静态文件夹中,然后使用 Flask 的

send_from_directory
函数将其显示在 html 中。

Js 库都不同,因此您需要阅读它们的文档并将它们与 Flask 集成。

© www.soinside.com 2019 - 2024. All rights reserved.