Making A Flask API
Requirements:
Python (https://www.python.org/)
Note: Add to Path (Check this box)
Flask
Run this command in command prompt with administrator privileges
(python -m pip install --user flask)
There is example for REST API using Flask:
from flask import Flask, jsonify #Importing essential Library
app = Flask(__name__) #here we given the name to the App
#In course variable we are passing the data as a dictionary
course=[
{ 'name' :"Python Programming Certification " ,
'course_id':"0",
' Description' : "Python programming certification helps you learn",
'price':"355"},
{ 'name' :"Python Programming Certification " ,
'course_id':"1",
' Description' : "Python programming certification helps you learn",
'price':"355"},
{ 'name' :"Python Programming Certification " ,
'course_id':"2",
' Description' : "Python programming certification helps you learn",
'price':"355"},
{ 'name' :"Python Programming Certification " ,
'course_id':"3",
' Description' : "Python programming certification helps you learn",
'price':"355"}]
#we are calling the app using name “app” and assigned a url by using route function this the default url
@app.route('/')
# here we declared a default function calling index and returning a string which will be displayed over
the default page.
def index():
return "Welcome to This API Homepage."
#Again we are assigning it an extended URL for variable course we created earlier and getting it back
using GET function.
@app.route("/course",methods=['GET'])
#Defining a get function to call it using course ID
def get():
return jsonify ({'Course':course})
#@app.route("/course/<int:course_id>",methods=['GET'])
#This is main body to start the app as we imported the module from flask library we are assigning
__main__ to __name__.
if __name__ =="__main__":
app.run(debug=True)