liveBook · Manning

View function

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A view function is a Flask request-handler function that processes a web request and returns a response or rendered page.

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

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains routing, template rendering, query parsing, type conversion, model invocation, and result display, while deferring robust bin calculation.
Examples
Uses home.html and show-prediction.html handlers for a property-price prediction form with categorical and continuous fields.
What it assumes
Assumes basic Python, HTML, JavaScript, Flask, and saved-model usage while demonstrating the request-to-prediction flow.
What you can do after
Apply it via Flask to render pages, collect request inputs, call a loaded Keras model, and display predictions.
How it connects
Connects Flask routes with HTML templates, JavaScript form submission, query strings, and Keras model inference.

Also explained in

Compare how 2 books explain view function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.