-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskapi.py
32 lines (23 loc) · 831 Bytes
/
flaskapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Using flask to make an api
# import necessary libraries and functions
from flask import Flask, jsonify, request
# creating a Flask app
app = Flask(__name__)
# on the terminal type: curl http://127.0.0.1:5000/
# returns hello world when we use GET.
# returns the data that we send when we use POST.
@app.route('/', methods = ['GET', 'POST'])
def home():
if(request.method == 'GET'):
data = "hello world"
return jsonify({'data': data})
# A simple function to calculate the square of a number
# the number to be squared is sent in the URL when we use GET
# on the terminal type: curl http://127.0.0.1:5000 / home / 10
# this returns 100 (square of 10)
@app.route('/home/<int:num>', methods = ['GET'])
def disp(num):
return jsonify({'data': num**2})
# driver function
if __name__ == '__main__':
app.run(debug = True)