Skip to content

Instantly share code, notes, and snippets.

@kristiewirth
Created February 4, 2019 19:29
Show Gist options
  • Save kristiewirth/4e6f18bf4fffc6b81b86e4ff0c75673d to your computer and use it in GitHub Desktop.
Save kristiewirth/4e6f18bf4fffc6b81b86e4ff0c75673d to your computer and use it in GitHub Desktop.
from flask import Flask, jsonify, request
import pandas as pd
# Intialize app
app = Flask(__name__)
# Need the liveness & readiness routes in order to pass Kubernetes checks
@app.route('/liveness')
def liveness():
return "alive"
@app.route('/readiness')
def readiness():
return "ready"
@app.route("/classify", methods=['POST'])
def classify():
# Set an API key here for added security
if request.headers.get('x-api-key') != 'SETSOMEKEYHERE':
return 403
# Load json into pandas dataframe
try:
df = pd.read_json(request.get_data())
except ValueError:
return jsonify({"error": "No JSON payload found."}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9999)
# View page in browser http://0.0.0.0:9999/
# Send JSON to test - change to port 4000 for testing within Docker container
# curl --header "Content-Type: application/json" --request POST --data @data/json_from_df.json http://localhost:9999/classify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment