0% found this document useful (0 votes)
29 views

Making A Flask API

This document provides instructions for creating a REST API using Flask in Python. It explains how to install Flask using pip and import the necessary libraries. It then gives an example of creating a Flask app with routes to return JSON data from a list of courses. The routes include a homepage, a route to retrieve all courses, and a route to retrieve a single course by ID. The app runs a local development server to serve the API endpoints.

Uploaded by

Sameer khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Making A Flask API

This document provides instructions for creating a REST API using Flask in Python. It explains how to install Flask using pip and import the necessary libraries. It then gives an example of creating a Flask app with routes to return JSON data from a list of courses. The routes include a homepage, a route to retrieve all courses, and a route to retrieve a single course by ID. The app runs a local development server to serve the API endpoints.

Uploaded by

Sameer khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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)

You might also like