Skip to content

Instantly share code, notes, and snippets.

@mickaellegal
Last active January 3, 2016 22:19
Show Gist options
  • Save mickaellegal/8527751 to your computer and use it in GitHub Desktop.
Save mickaellegal/8527751 to your computer and use it in GitHub Desktop.
Python: Blog flask app
# We first import all the libraries needed
from flask import Flask, request, render_template, jsonify
from yhat import Yhat
import os
#We create the application object of class Flask
app = Flask(__name__)
# We set our credential to access our model on yhat from the Flask app
yh = Yhat(os.environ.get("YHAT_USERNAME"), os.environ.get("YHAT_APIKEY"))
# The route decorator below links the hello function to the url "/"
@app.route("/", methods=["GET", "POST"])
def hello():
# If nothing is posted on the form just return home.html
if request.method=="GET":
return render_template("home.html")
# Otherwise go fetch the data and format it properly
else:
data = request.form.get('entry')
data = {
"content": [data]
}
# Call the model on yhat
results = yh.predict("NYTimesClassifier", 1, data)
# Return the results
return jsonify({"results": results})
# Run the app
if __name__ == '__main__':
app.run(port=int(os.environ.get('PORT', 5000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment