Skip to content

Instantly share code, notes, and snippets.

@larshb
Last active September 16, 2019 18:02
Show Gist options
  • Save larshb/0997e5ca01fd9d13861c010a897c4b0a to your computer and use it in GitHub Desktop.
Save larshb/0997e5ca01fd9d13861c010a897c4b0a to your computer and use it in GitHub Desktop.
Minimal example of a REST API in Python

Depending on your operating system, environment and python version all that should be needed is the following:

pip install -r requirements.txt
python server.py

Tested, working in Windows Subsystem for Linux with Python 3.5.

When running the server test the following addresses in your browser:

flask_jsonpify is not necessary.

flask
flask_restful
flask_jsonpify
from flask import Flask, request
from flask_restful import Resource, Api
from flask_jsonpify import jsonify
app = Flask(__name__)
api = Api(app)
PORT = 5000
class MyRes(Resource):
def get(self):
return jsonify({'My key:': 'my value'})
class Multiply(Resource):
def get(self, a, b):
return jsonify(float(a) * float(b))
api.add_resource(MyRes, '/myres')
api.add_resource(Multiply, '/mul/<a>/<b>')
if __name__ == '__main__':
app.run(port=str(PORT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment