Recently FastAPI I could see more posts/recommendations compared to flask API in a performance context. A basic example of implementation with flask vs Fast API. The format /syntax, request differences you can spot by comparing them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
from flask import Flask, request | |
app = flask.Flask(__name__) | |
app.config["DEBUG"] = True | |
#http://localhost:4321/api1?searchparam=param1 | |
@app.route('/api1',methods=['GET']) | |
def api1(): | |
paramname = request.args.get("searchparam") | |
return str(paramname) | |
#http://localhost:4321/api2?searchparam1=ABC&searchparam2=100 | |
@app.route('/api2',methods=['GET']) | |
def api2(): | |
paramname1 = request.args.get("searchparam1") | |
paramname2 = request.args.get("searchparam2") | |
return str(paramname1 + paramname2) | |
if __name__ == "__main__": | |
app.run(host='localhost',port=4321,threaded=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Step 1 - Packages to Install | |
#pip install fastapi | |
#pip install uvicorn | |
#Option - Run from Terminal | |
#uvicorn Example1:app --reload | |
#Run from Spyder / Example Code | |
from fastapi import FastAPI | |
import uvicorn | |
from fastapi import Form, Request | |
app = FastAPI(debug=True) | |
#http://localhost:8002/api1/raj | |
@app.get('/api1/{searchparam1}') | |
def api1(searchparam1: str): | |
paramname = searchparam1 | |
print(paramname) | |
return str(paramname) | |
#http://localhost:8002/api2/?searchparam1=ABC&searchparam2=100 | |
@app.get('/api2/') | |
def api2(searchparam1: str,searchparam2: int): | |
param1 = searchparam1 | |
param2 = str(searchparam2) | |
print(str(param1 +','+ param2)) | |
return str(param1 +','+ param2) | |
if __name__ == "__main__": | |
uvicorn.run(app,host="localhost",port=8002) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import base64 | |
from flask import Flask, request | |
from PIL import Image | |
import io | |
app = flask.Flask(__name__) | |
app.config["DEBUG"] = True | |
#http://localhost:4321/api1?searchparam=param1 | |
@app.route('/api1',methods=['GET']) | |
def Api1(): | |
paramname = request.args.get("searchparam") | |
return str(paramname) | |
#http://localhost:4321/api2?searchparam1=ABC&searchparam2=100 | |
@app.route('/api2',methods=['GET']) | |
def Api2(): | |
paramname1 = request.args.get("searchparam1") | |
paramname2 = request.args.get("searchparam2") | |
return str(paramname1 + paramname2) | |
@app.route('/ApplyEncodedTransformation',methods=['POST']) | |
def ApplyEncodedTransformation(): | |
try: | |
paramname1 = request.values.get("searchparam1") | |
print(paramname1) | |
encodedimage = request.values.get("searchparam2") | |
print(encodedimage) | |
im = Image.open(io.BytesIO(base64.b64decode(encodedimage))) | |
input_filename = r'image1.png' | |
im.save(input_filename, 'PNG') | |
with open(input_filename, "rb") as imagedata: | |
encoded_string = base64.b64encode(imagedata.read()) | |
return encoded_string | |
except: | |
return 'Error' | |
@app.route('/ApplySimpleImageTransfer',methods=['POST']) | |
def ApplySimpleImageTransfer(): | |
try: | |
file = request.files['image'] | |
img = Image.open(file.stream) | |
result_file_name = 'Inputimage.jpg' | |
img.save(result_file_name) | |
encoded_string = "" | |
with open(result_file_name, "rb") as imagedata: | |
encoded_string = base64.b64encode(imagedata.read()) | |
return encoded_string | |
except: | |
return 'Error' | |
if __name__ == "__main__": | |
app.run(host='localhost',port=4321,threaded=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import cv2 | |
import base64 | |
def passanimagetest(): | |
url = 'http://localhost:4321/ApplySimpleImageTransfer' | |
#Pick Random image for test | |
my_img = {'searchparam1':'sendimage','image': open(r'1.jpg','rb')} | |
#Send the request | |
response = requests.post(url,files=my_img,timeout=20) | |
if response.status_code == 200: | |
print(response.content) | |
with open(r"result.png", "wb") as fh: | |
fh.write(base64.decodebytes(response.content)) | |
def passanimagetransformation(): | |
url = 'http://localhost:4321/ApplyEncodedTransformation' | |
encoded_string = "" | |
with open(r'1.jpg', "rb") as imagedata: | |
encoded_string = base64.b64encode(imagedata.read()) | |
#print(encoded_string) | |
data = {'searchparam1': 'Client1','searchparam2': encoded_string} | |
response = requests.post(url, data) | |
if response.status_code == 200: | |
print(response.content) | |
with open(r"result.png", "wb") as fh: | |
fh.write(base64.decodebytes(response.content)) | |
#passanimagetest() | |
passanimagetransformation() |
CI / CD
Makefile
Happy Learning!!!
No comments:
Post a Comment