【问题标题】:How i can get data in Python Script using Flask [duplicate]我如何使用 Flask 在 Python 脚本中获取数据 [重复]
【发布时间】:2018-02-01 19:27:22
【问题描述】:

我编写了一个 html 代码,它将从用户那里获得 3 个输入。 我附上了html代码sn-p如下; 您可以尝试运行此代码。这段代码基本上接受来自用户的 3 个值,它们是 team1、team2 和 match_id,点击预测按钮后,我希望这些值进入我编写机器学习算法的 python 脚本中。

<!DOCTYPE html>
<html>

<head>
    <title>Criclytics</title>
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="bootstrap.min.js"></script>
        <script src="jquery.min.js"></script>
    <style type="text/css">
        @import url(http://fonts.googleapis.com/css?family=Exo:100,200,400);
        @import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:700,400,300);

        body{
            margin: 0;
            padding: 0;
            background: #fff;

            color: black;
            font-family: Arial;
            font-size: 25px;


            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;

        }

        .bcg-img{
            width: 100vw;
            height: 100vh;
            z-index: -1;
            position: fixed;
                background-image: url("bg-blurred.jpg");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }

        table, td, th {    
               text-align: center;
        }

        table {
            border-collapse: collapse;
            width: 50%;
        }

        th, td {
            padding: 15px;
        }
        .button1 {width: 250px;}
    </style>
</head>
<body>
    <div class="bcg-img"></div>
    <div class="jumbotron" align="center" style="opacity:0.60">
        <h1 align="center"><b>Criclytics</b></h1>
        Predicting chances of winning
    </div>
    <form onsubmit="http://localhost:5000/"> 
         <div class="col-xs-3">
             <label for="ex2">Team 1</label>
             <input class="form-control" id="team1" id="team1" type="text" placeholder="Enter team 1">
        </div>
             <div class="col-xs-3">
             <label for="ex2">Team 2</label>
             <input class="form-control" id="team2" id="team2" type="text" placeholder="Enter team 2">
        </div>
        <div class="col-xs-3">
             <label for="ex2">Match ID:</label>
             <input class="form-control" id="matchid" id="matchid" type="number" placeholder="Enter match ID ">
        </div>
        <br>
        <input type="submit" value="Predict" class="btn btn-info btn-lg" style="width: 250px"/>
        <!--
        <div width="cover" padding="30%"><!--put your graph here</div>-->
    </form>
</body>
</html> 

我正在使用flask在localhost:5000上创建服务器,我已经编写了代码,如下;

from flask import Flask, render_template
from flask import request
app = Flask(__name__)
print("hello")
@app.route('/')
def getrender():
    return render_template('/cric.html')

@app.route('/',methods=['GET','POST'])
def getinfo():
    if  request.method == 'GET':
        a=request.args.get('team1')
        b=request.args.get('team2')
        c=request.args.get('matchid')
        print(a,b,c)
    return a,b,c

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

html 文件在 localhost:5000 上完美运行,但我不知道如何访问这些用户输入值并将其用作我的机器学习算法的输入。 我只想帮助如何访问这些 team1、team2 和 match_id 并将它们放入变量中,以便我可以在我的程序中使用它们。

【问题讨论】:

  • print(a, b, c) 时控制台会打印什么值
  • 看看查询字符串en.wikipedia.org/wiki/Query_string
  • 程序运行时控制台上没有打印任何内容
  • 在 localhost:5000 上输入值后重新运行程序时,我在屏幕上看到的只是“使用 stat 重新启动”
  • 为什么getinfo和getrender使用同一个url?

标签: python html flask


【解决方案1】:

您的表单有问题, 您的所有输入都没有 name 属性,而是有 2 个 id 属性。 所以把一个id属性改成name

here is the reason

定义和用法 name 属性指定元素的名称。 name 属性用于引用 JavaScript 中的元素,或者 提交表单后引用表单数据。

注意:只有具有 name 属性的表单元素才会在提交表单时传递其值。

你的表格应该是这样的:

<form method="POST" action="{{url_for('getinfo')}}">
         <div class="col-xs-3">
             <label for="ex2">Team 1</label>
             <input class="form-control" name="team1" id="team1" type="text" placeholder="Enter team 1">
        </div>
             <div class="col-xs-3">
             <label for="ex2">Team 2</label>
             <input class="form-control" name="team2" id="team2" type="text" placeholder="Enter team 2">
        </div>
        <div class="col-xs-3">
             <label for="ex2">Match ID:</label>
             <input class="form-control" id="matchid" name="matchid" type="number" placeholder="Enter match ID ">
        </div>
        <br>
        <input type="submit" value="Predict" class="btn btn-info btn-lg" style="width: 250px"/>
        <!--
        <div width="cover" padding="30%"><!--put your graph here</div>-->
    </form>

在您的视图函数中,您应该有 2 个不同的 URL:一个用于主页,另一个用于表单提交

@app.route('/')
def getrender():
    return render_template('/cric.html')

@app.route('/predict', methods=['GET', 'POST'])
def getinfo():
    if request.method=='POST':
        a=request.form.get('team1')
        b=request.form.get('team2')
        c=request.form.get('matchid')
        print(a,b,c)
    else :
        return 'get Im in '
    return 'OK'

【讨论】:

    【解决方案2】:

    我的建议:

    为html页面分配另一个路由

    @app.route('/cric')
    def getrender():
        return render_template('/cric.html')
    

    在函数getinfo中将方法检查从GET更新为POST,并使用request.form获取参数

    @app.route('/',methods=['GET','POST'])
    def getinfo():
        print request.method  # print the request.method for debug purpose
        if  request.method == 'POST':
            a=request.form.get('team1')
            b=request.form.get('team2')
            c=request.form.get('matchid')
            print(a,b,c)
    
        return render_template('/cric.html')
    

    并更新表单标题并在html中分配三个带有名称的输入:

    <form action="http://localhost:5000/" method="post">
    ...
    <input class="form-control" id="team1" name="team1" type="text" placeholder="Enter team 1">
    ...
    

    然后你可以访问'/cric'查看html页面,然后提交表单,它会调用一个post请求到'/'并打印参数。

    【讨论】:

    • 一切正常,除了它不打印值 a,b,c 我正在使用 Spyder 运行这个程序。
    • 答案已更新@MayurMahajan
    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 2015-10-23
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2016-03-29
    • 2017-09-09
    相关资源
    最近更新 更多