I am beginner of FLASK.
This is my html page my-form.html which I have put in the template folder
<!DOCTYPE html>
<head>
<title>Calculate Energy Output From Wind Turbine</title>
</head>
<body>
<h1>
Predict Energy Output From Wind Speed
</h1>
<form method="POST">
Enter mph wind speed to calculate energy output<br>
<input name="text" type="number">
<input type="submit">
</form>
</body>
</html>
I have no problem with the Python/Flask code when I do simple return operation in code below. I found the basics of retrieving the form data at Send data from a textbox into Flask? but now I would really appreciate broadening my knowledge of how to now do normal Python formulas on the data and return the result to web page without returning error.
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
processed_text = text.upper()
return processed_text
I would now like to do some math on the entered wind speed and call a function on the returned result, predict_power_output(int(text)). Below is the code I am trying. I am trying to call a regression formula on the entered text.
import sklearn.linear_model as lin
import pandas as pd
# Load a dataset.
powerproduction = pd.read_csv('powerproduction.csv')
def f(speed, p):
return p[0] + speed * p[1]
def predict_power_output(speed):
return f(speed, p)
speed = powerproduction["speed"].to_numpy()
y = powerproduction["power"].to_numpy()
speed = speed.reshape(-1, 1)
model = lin.LinearRegression()
model.fit(speed, y)
r = model.score(speed, y)
p = [model.intercept_, model.coef_[0]]
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
return predict_power_output(int(text))
When I try and do a Python calculation operation before returning the value I get this error
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Any advice appreciated. Thank you.