What that error message is saying is that it's trying to execute the statement
app_rv = app(environ, start_response)
...which calls the function that is the value of the variable app
with the given parameters, and then assigns the result to app_rv
. The error message is TypeError: 'InputForm' object is not callable
, and there is only one call in the line, which means that the variable app
must be an InputForm
object. This is strange, because it should (as you would expect) be a Flask app, not a form.
Now let's take a look at your code, copied and pasted from Stack Overflow:
from wtforms import Form, FloatField
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
class InputForm(Form):
P_level_upper = FloatField(label='Upper pressure level (mb)',
default=650.0)
P_level_lower = FloatField(label='Upper pressure level (mb)',
default=850.0)
Z_upper = FloatField(label='Upper boundary height', default=3.5)
Z_upper = FloatField(label='Lower boundary height', default=1.5)
T_upper = FloatField(label='Upper boundary temperature', default=0)
T_lower = FloatField(label='Lower boundary temperature', default=25)
TD_upper = FloatField(label='Upper boundary dewpoint', default=0)
TD_lower = FloatField(label='Lower boundary dewpoint', default=10)
CAPE = FloatField(label='CAPE', default=1000)
def index():
form = InputForm(request.form)
def MWPI(P_level_upper, P_level_lower, Z_upper, Z_lower, T_upper,
T_lower, TD_upper, TD_lower, CAPE):
gamma = (T_lower - T_upper)/(Z_upper - Z_lower)
DD_upper = T_upper - TD_upper
DD_lower = T_lower - TD_lower
DDD = DD_lower - DD_upper
MWPI = CAPE/100 + gamma + DDD
WGP = (0.4553 * MWPI) + 28.769
return MWPI, WGP
if request.method == 'POST':
result = MWPI(form.P_level_upper.data, form.P_level_lower.data,
form.Z_upper.data, form.Z_lower.data, form.T_upper.data,
form.T_lower.data, form.TD_upper.data, form.TD_lower.data,
form.CAPE.data)
else:
result = None
return render_template('MWPI.html', form=form, result=result)
if __name__ == "__main__":
app.run()
There's something very strange there. The app.route
decorator is applied to the thing that immediately follows it. The is normally the function that handles the URL that you specify in the decorator. However, you've put it just before your class InputForm
instead. This will confuse Flask, as it's trying to work out which function to send a request to, and it's getting something that isn't a function -- hence the error.
What you need to do is move the @app.route
down so that it applies to your view function instead of to the InputForm
class, like this:
from wtforms import Form, FloatField
from flask import Flask, render_template, request
app = Flask(__name__)
class InputForm(Form):
P_level_upper = FloatField(label='Upper pressure level (mb)',
default=650.0)
P_level_lower = FloatField(label='Upper pressure level (mb)',
default=850.0)
Z_upper = FloatField(label='Upper boundary height', default=3.5)
Z_upper = FloatField(label='Lower boundary height', default=1.5)
T_upper = FloatField(label='Upper boundary temperature', default=0)
T_lower = FloatField(label='Lower boundary temperature', default=25)
TD_upper = FloatField(label='Upper boundary dewpoint', default=0)
TD_lower = FloatField(label='Lower boundary dewpoint', default=10)
CAPE = FloatField(label='CAPE', default=1000)
@app.route('/', methods=['GET', 'POST'])
def index():
form = InputForm(request.form)
def MWPI(P_level_upper, P_level_lower, Z_upper, Z_lower, T_upper,
T_lower, TD_upper, TD_lower, CAPE):
gamma = (T_lower - T_upper)/(Z_upper - Z_lower)
DD_upper = T_upper - TD_upper
DD_lower = T_lower - TD_lower
DDD = DD_lower - DD_upper
MWPI = CAPE/100 + gamma + DDD
WGP = (0.4553 * MWPI) + 28.769
return MWPI, WGP
if request.method == 'POST':
result = MWPI(form.P_level_upper.data, form.P_level_lower.data,
form.Z_upper.data, form.Z_lower.data, form.T_upper.data,
form.T_lower.data, form.TD_upper.data, form.TD_lower.data,
form.CAPE.data)
else:
result = None
return render_template('MWPI.html', form=form, result=result)
if __name__ == "__main__":
app.run()