0

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.

Christopher
  • 427
  • 1
  • 8
  • 18
  • You need to run your app in debug mode. By default, flask won't show what error occurred for security reasons (not that the webserver shipped with flask should be used in production environments anyway). Do this with either `app.debug = True` or `app.run(debug=True)` or at the command line `FLASK_ENV=development flask run`. – Dunes Oct 20 '20 at 10:28
  • 1
    The likely problem is that flask will not coerce the returned value to a string (like `print()` does). That is, `print` automatically turns `1` into `"1"`. Flask won't do that for you. You probably want to change your return statement to `return str(predict_power_output(int(text)))`. – Dunes Oct 20 '20 at 10:40
  • @Dunes. Thank you for your advice. I am currently doing the following at the command line 'set FLASK_APP=app.py' , then 'flask run' ... whe I substitute 'flask run' with 'FLASK_ENV=development flask run' I get an error at the command line - 'FLASK_ENV' is not recognized as an internal or external command, operable program or batch file. How should I change the set step to be compatible with your advice? – Christopher Oct 20 '20 at 10:41
  • That means you're on windows. I was assuming you were using a UNIX like OS. In which case enter `set FLASK_ENV=development` before you do `flask run`. That will work for cmd.exe. – Dunes Oct 20 '20 at 10:44
  • @Dunes, this advice you gave 'You probably want to change your return statement to return str(predict_power_output(int(text)))' worked perfectly. Thank you so much! – Christopher Oct 20 '20 at 10:45

0 Answers0