繁体   English   中英

如何使用 Python Flask 将数据发送到网页

[英]How to send data to webpage using Python Flask

语境

我创建了这个 python flask 应用程序。 它读取 csv 文件,从该文件计算“数据”。

from flask import Flask, render_template, request
import pandas
app = Flask(__name__)

@app.route('/')


def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])

def index_post():
    if request.method == 'POST':
        req = request.form['fileToUpload']
        df = pandas.read_csv(req)
        base_pay_MEAN = df['BasePay'].mean()
        overtime_MAX = df['OvertimePay'].max()
        highest_paid_Person_NAME = (df[df['TotalPayBenefits'] == max(df['TotalPayBenefits'])]).iloc[0]['EmployeeName']
        highest_paid_Person_SALARY = (df[df['TotalPayBenefits'] == max(df['TotalPayBenefits'])]).iloc[0]['TotalPayBenefits']
        num_Unique_Jobs = df['JobTitle'].nunique()
        most_common_jobs = df.groupby('JobTitle').count().sort_values(by='Id', ascending=False)['Id'].head(3)        
        return render_template('index.html')
    else:
        render_template('index.html')

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

这是 HTML 文件的一部分

            <p>Please upload your CSV file. The values containing addresses should be in a column named <em>address</em> or <em>Address</em></p>
            <form method='POST'>
                <input type="file" name="fileToUpload" id="fileToUpload">
                <button id="download">Submit</button>
            </form>
            <div class="results">
                <h3 class="base-pay"></h3>
                <p class="base-pay-mean"> </p>
                <p class="base-pay-high"></p>
                <p class="base-pay-low"></p>

                <h3 class="over-time"></h3>
                <p class="over-time-max"></p>

                <h3 class="highest-paid"></h3>
                <p class="highest-paid-person"></p>
                <p class="highest-paid-job"></p>
                <p class="highest-paid-salary"></p>


                <h3 class="lowest-paid"></h3>
                <p class="lowest-paid-person"></p>
                <p class="lowest-paid-job"></p>
                <p class="lowest-paid-salary"></p>
            </div>


这是网页的图片: 在此处输入图像描述

问题

我已经计算了需要发送回网页的值,例如 base_pay_MEAN、overtime_MAX 等。我已经创建了一个<div> ,其中class = "results"我需要将此数据发送到该 div 中的相应标签。 但是如何将其发送回网页? 我的目标是当用户上传 CSV 文件并单击提交时,页面应显示必要的值,例如: 在此处输入图像描述

但我什至确定从哪里开始。

附言。 对不起,长帖子

最简单的方法是制作一个字典并通过return jsonify(response_dict)返回它(您需要from flask import jasonify )。 在我的情况下,响应字典是response_dict = {"message": reponse_text} ,因为它在这里完成https://medium.com/zenofai/creating-chatbot-using-python-flask-d6947d8ef805

@app.route('/send_message', methods=['POST'])
def send_message():
    message = request.form['message']
    project_id = os.getenv('DIALOGFLOW_PROJECT_ID')
    fulfillment_text = detect_intent_texts(project_id, "unique", message, 'en')
    response_text = { "message":  fulfillment_text }
    return jsonify(response_text)

您可以将要显示的值作为关键字 arguments 传递给render_template()

例如,要显示“基本工资平均值”,您可以从视图中将适当的变量发送到模板(Python 部分):

base_pay_MEAN = df['BasePay'].mean()
return render_template('index.html', base_pay_MEAN=base_pay_MEAN)

然后在模板中渲染该变量(HTML 部分):

<div class="results">
    <h3 class="base-pay">Basepay:</h3>
    <p class="base-pay-mean">Mean: {{ base_pay_MEAN }}</p>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM