HOWTO: Create A Simple API With Python And Flask - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › HOWTO: Create A Simple API With Python And Flask (1770 Views)
1 Reply
| HOWTO: Create A Simple API With Python And Flask by pystar(op): 4:51am On Aug 29, 2014 |
This is a simple flask app: Pardon the lack of indentation in my code. Installing Flask-Restful in your flask app makes building API's extremely simple. $ PIP install flask-restful ######Simple flask app with API to expose the app########## from flask import Flask from flask.ext.restful import Resource, API app = Flask(__name__) api = Api(app) @app.route("/" ) @app.route("/index" ) @app.route("/home" ) def index(): return "Returning data" ###This exposes your app as an API### class Nairaland(Resource): def get(self): return {"data" : "Returning data"} app.add_resource(Nairaland, "/api/v1.0/" ) if __name__ == "__main__": app.run() |
| Re: HOWTO: Create A Simple API With Python And Flask by pystar(op): 4:56am On Aug 29, 2014 |
You can copy the above code into a file and name it api.py, kindly indent it to enable it run. Ensure you have installed the flask-restful extension into your flask installation (hopefully you should be using virtualenv). Run the app with $ python api.py and visit http://localhost:5000/api/v1.0 to see the api in action. To view the API returned data programmatically $ curl -i http://localhost:5000/api/v1.0 In my next post, I will show how to post data with the API. (Off to work ) |
Build A Bare Bones Reddit Clone With Python And Flask (hands On Tutorial Thread) • Pls Help With Python Problem • Dll Load Failure With Py2exe (python And Qt) • 2 • 3 • 4
A Java App Developer Should Please Help • Websites To Learn How To Code For Free • Which Brand Model Of Printer Is Best For Your Use? Please See This.
)