I am trying to pass values from a website to Python, and get a return passed back to be displayed. The input location is as follows:
<form> <!-- create inputs -->
<input type="text" id="WS" style="font-size:10pt; height:25px" required><br> <!-- Wind speed input box -->
<br>
</form>
The user then clicks a button (in this case 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>
This should pass the data to the following script:
$("#Lin").click(
function(e) {
$.get('/api/Lin/' + $('#WS').val(), function(data) {
$('#Power_Est').val(data.value);
});
});
The output box is:
<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>
The Python script I have is:
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
Whenever I run the above, using flask, and http://127.0.0.1:5000/ I get the following error message:
127.0.0.1 - - [30/Dec/2020 21:07:19] "GET /api/Lin/20 HTTP/1.1" 404 -
Any suggestions on how to correct this?
Edit 1
Using the below:
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))
The error is:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 6 return power_est # Return the power estimate 7 ----> 8 print(Lin_Reg(20.0))
in Lin_Reg(speed) 1 def Lin_Reg(speed): # Call the linear regression function 2 print(speed) ----> 3 speed = speed.reshape(1, -1) 4 lin_model_load = joblib.load("Models/lin_reg.pkl") # Reimport the linear regression model 5 power_est = lin_model_load.predict(speed) # Use the linear regression model to estimate the power for the user inputted speed
AttributeError: 'float' object has no attribute 'reshape'
$('WS')is an invalid id selector. Id selectors start with a#predictmethod expects an array (A vector) instead of a float, but I could be wrong. Please confirm this with the documentation.