Overview
This project is a Flask-based API that provides interesting mathematical properties of a given number. It determines whether a number is prime, perfect, or an Armstrong number, and also provides its digit sum and a fun fact.
Zappa makes it super easy to build and deploy serverless, event-driven python applications(including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Find more info about Zappa here.
Note that you would need an AWS CLI user setup with necessary permissions to lambda and api gateway.
Features
Determines if a number is prime, perfect, or an Armstrong number.
Computes the sum of digits.
Returns a fun fact about the number.
Supports CORS for cross-origin access.
Returns responses in JSON format.
Setup Necessary Tools and Dependencies.
First of all, Ensure you have python installed. You can go through the python documentation to assist you.
Install Flask and Zappa.
pip install flask flask-cors zappa
Setup your API on app.py in your local environment
from flask import Flask, request, jsonify
from flask_cors import CORS
import requests
import math
app = Flask(__name__)
CORS(app)
def is_prime(num):
if num <= 1 or num % 1 != 0:
return False
num = int(num)
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
def is_armstrong(num):
if num % 1 != 0: # Only whole numbers can be Armstrong numbers
return False
num = abs(int(num))
digits = [int(digit) for digit in str(num)]
power = len(digits)
return sum(digit ** power for digit in digits) == num
def get_fun_fact(num):
url = f"http://numbersapi.com/{num}/math"
response = requests.get(url)
return response.text if response.status_code == 200 else "No fun fact found"
@app.route('/api/classify-number', methods=['GET'])
def classify_number():
number = request.args.get('number')
try:
number = float(number)
except (ValueError, TypeError):
return jsonify({"number": number, "error": "true"}), 400
armstrong = is_armstrong(number)
prime = is_prime(number)
even = number % 2 == 0
digit_sum = sum(map(int, str(abs(int(number)))))
fun_fact = get_fun_fact(int(number))
properties = []
if armstrong:
properties.append("armstrong")
properties.append("even" if even else "odd")
return jsonify({
"number": number,
"is_prime": prime,
"is_perfect": False, # No perfect number check required
"properties": properties,
"digit_sum": digit_sum,
"fun_fact": fun_fact
})
if __name__ == "__main__":
app.run()
Test your API Locally
http://127.0.0.1:5000/api/classify-number?number=-100.5
Deploy API with Zappa on AWS Lambda using API Gateway
Initialize Zappa
zappa init
Follow the prompts to configure deployment settings.
Deploy your Flask app to AWS Lambda using the code below:
zappa deploy dev
This would take a while. After deployment, a link will be shared to access your API.
If you make update to your function, you can always update lambda using the code below
zappa update dev
Zappa will automatically set up a regularly occurring execution of your application to keep the Lambda function warm. This helps optimize speed and produces fast response time.
You can confirm the creation of the AWS Lambda function and API Gateway on your AWS Console and also make changes there.
Top comments (0)