Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,408 members, 7,822,890 topics. Date: Thursday, 09 May 2024 at 06:47 PM

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 (1654 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 grin)

(1) (Reply)

Data Analysis Packages: Which Should I Learn?! / Become Proficient In Python Programming / Zillionphpscript Is A Thief

(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. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.