javascript - 无法使用 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”属性
解决方案
确保发送浮点数而不是整数。
$.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)
推荐阅读
- java - 我应该使用线程安全的 Map 吗?
- r - 计算R中的像素(计数)光栅文件的数量
- java - Ensuring the HTTPS connection was properly set up
- javascript - react-create-app “npm run build” 不转译 JSX
- html - React Player Controls - HTML5 视频事件处理程序无法识别视频对象
- r - 使用 lapply 为 do.call 创建参数列表
- powershell - Powershell 将选项卡式文本文件内容转换为格式良好的 HTML 表
- javascript - npm 运行构建时出错
- scala - 使用 Scala 创建 Couchbase 存储桶?
- string - 将字符串转换为代码