首页 > 解决方案 > 无法使用 JQuery 将值传入或传出 Python

问题描述

我正在尝试将值从网站传递到 Python,并获得返回以显示。输入位置如下:

<form>                                                                      <!-- create inputs -->
    <input type="text" id="WS" style="font-size:10pt; height:25px" required><br>    <!-- Wind speed input box -->
<br>
</form>

然后用户单击一个按钮(在本例中为 Lin):

<button id="Lin" style="height:30px; width:10%; background-color:#5188e0; border-color: black; color: white; font-weight: bold" title="Linear regression attempts to model the relationship between two variables by fitting a linear equation to observed data">Linear Regression</button>

这应该将数据传递给以下脚本:

$("#Lin").click(
    function(e) {
        $.get('/api/Lin/' + $('#WS').val(), function(data) {
            $('#Power_Est').val(data.value);
        });
    });

输出框为:

<form>
    <label>Estimated power output (KW/h):</label>                           <!-- Power label -->
    <input class="form-control" id="Power_Est" type="text" style="font-size:10pt; height:25px" placeholder="Power Estimate" readonly><br>   <!-- Power Estimate box -->
    <br>
</form>

我拥有的 Python 脚本是:

import flask as fl
import numpy as np
import joblib

app = fl.Flask(__name__)                                        # Create a new web app.

@app.route("/")                                                 # Add root route.
def home():                                                     # Home page
    return app.send_static_file("Front_Page.html")              # Return the index.html file

@app.route("/api/Lin/<float:speed>", methods = ["GET"])         # If the Linear Regression button is chosen
def Lin_Reg(speed):                                             # Call the linear regression function
    lin_model_load = joblib.load("Models/lin_reg.pkl")          # Reimport the linear regression model
    power_est = np.round(lin_model_load.predict(speed)[0], 3)   # Use the linear regression model to estimate the power for the user inputted speed
    return power_est                                            # Return the power estimate

每当我使用烧瓶和http://127.0.0.1:5000/运行上述程序时,我都会收到以下错误消息:

127.0.0.1 - - [30/Dec/2020 21:07:19] “GET /api/Lin/20 HTTP/1.1”404 -

有关如何纠正此问题的任何建议?

编辑 1

使用以下:

def Lin_Reg(speed):                                             # Call the linear regression function
    print(speed)
    speed = speed.reshape(1, -1)
    lin_model_load = joblib.load("Models/lin_reg.pkl")          # Reimport the linear regression model
    power_est = lin_model_load.predict(speed) # Use the linear regression model to estimate the power for the user inputted speed
    return power_est                                            # Return the power estimate

print(Lin_Reg(20.0))

错误是:

-------------------------------------------------- ------------------------- AttributeError Traceback (most recent call last) in 6 return power_est # 返回功率估计 7 ----> 8 print (Lin_Reg(20.0))

in Lin_Reg(speed) 1 def Lin_Reg(speed): # 调用线性回归函数 2 print(speed) ----> 3 speed = speed.reshape(1, -1) 4 lin_model_load = joblib.load("Models/ lin_reg.pkl") # 重新导入线性回归模型 5 power_est = lin_model_load.predict(speed) # 使用线性回归模型估计用户输入速度的功率

AttributeError:“float”对象没有“reshape”属性

标签: javascriptpythonhtmljqueryflask

解决方案


确保发送浮点数而不是整数。

$.get('/api/Lin/' + parseFloat($('#WS').val()), function(data) {
  $('#Power_Est').val(data.value);
});

也调用predict二维数组。

power_est = np.round(lin_model_load.predict([[speed]])[0], 3)

推荐阅读