Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,238 members, 7,957,567 topics. Date: Tuesday, 24 September 2024 at 02:48 PM |
Nairaland Forum / Science/Technology / Programming / HOWTO: Create A Simple API With Python And Flask (1683 Views)
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)
(1) (Reply)
HOWTO: Create A Simple API With Python And Flask by pystar: 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: 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 ) |
(1) (Reply)
Please Criticize It, Condemn It / How Can I Be Very Good As A Computer Science Student? / Beginning Coders Get Dirty
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8 |