A view function is a Flask request handler that connects a URL to an action. It can render an HTML page directly or process submitted values before returning a prediction page. section 10.1.2
A prediction workflow uses view functions to:
- Map the root URL to a home function that supplies a title and renders home.html. section 10.1.2
- Map /show-prediction/ to a function that reads query-string values from the web form. section 10.1.2 section 10.1.3
- Convert categorical and continuous inputs, prepare model input, invoke the loaded Keras model, and pass the result to a template. section 10.1.2
Property-price prediction
After a user selects a location and enters property details, JavaScript navigates to a URL such as /show-prediction/?location=KLCC, Kuala Lumpur&rooms=1&size=500. The prediction view function collects these arguments, supplies the model’s result to show-prediction.html, and displays the prediction. section 10.1.3 section 10.1.2
Listing 10.2 home.html view function
@app.route('/') #1
def home():
title_text = "web deployment of Keras model"
title = {'titlename':title_text}
return render_template('home.html',title=title) #2
See the minimal route-to-template pattern for a view function. section 10.1.2
Listing 10.3 show-prediction.html view function
@app.route('/show-prediction/') #1
def show_prediction():
scoring_dict = {}
for col in config['scoring_columns_cat']:
print("value for "+col+" is: "+str(request.args.get(col)))
scoring_dict[col] = str(request.args.get(col)) #2
for col in config['scoring_columns_cont']:
scoring_dict[col] = float(request.args.get(col)) #3
scoring_dict['size_type_bin'] = \
str(request.args.get('size_type'))+' 1' #4
input_dict = \
{name: tf.convert_to_tensor([value]) for name, \
value in scoring_dict.items()} #5
predictions = loaded_model.predict(input_dict) #6
prob = tf.nn.sigmoid(predictions[0])
if prob < 0.5: #7
predict_string = "Prediction is: property has a price less than
median"
else:
predict_string = "Prediction is: property has a price more than
median"
prediction = {'prediction_key':predict_string}
return(render_template('show-prediction.html',\
prediction=prediction)) #8
See the fuller handler pattern for reading request values and invoking a prediction model. section 10.1.2

