MVC-Like

Learning Python Flask MVC Framework

2018-01-10. Category & Tags: Flask, MVC-Like

Run A Demo #

a simple getting-start #

Let’s take the example below (Serving Prophet Model with Flask — Predicting Future (very simple) 2019.07).
Download the saved model and put to the same folder as app.py which is:

from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin

import pickle
with open('forecast_model.pckl', 'rb') as fin:
    m2 = pickle.load(fin)

app = Flask(__name__)
CORS(app)
@app.route("/katana-ml/api/v1.0/forecast/ironsteel", methods=['POST'])
def predict():
    horizon = int(request.json['horizon'])

    future2 = m2.make_future_dataframe(periods=horizon)
    forecast2 = m2.predict(future2)

    data = forecast2[['ds', 'yhat', 'yhat_lower', 'yhat_upper']][-horizon:]

    ret = data.to_json(orient='records', date_format='iso')

    return ret
# running REST interface, port=3000 for direct test
if __name__ == "__main__":
    app.run(debug=False, host='0.0.0.0', port=3000)

Then run python app.py to serve this model.

...