DEV Community

Precious Edmund
Precious Edmund

Posted on

HNG-StageN-1:Number Facts API: A Fun & Interactive Flask Web Service

Overview

The Number Facts API is a Flask-based web service that classifies numbers and provides interesting mathematical properties along with a fun fact. This API supports several mathematical functions such as determining whether a number is prime, perfect, or Armstrong, classifying numbers as odd or even, computing the sum of its digits, and retrieving fun facts using the Numbers API.

This project is part of my HNG Internship DevOps Bootcamp, and as I mentioned in my earlier post, I’ll be sharing the next stage with you here on this platform. But first, here’s the Stage 1 task in my HNG internship journey!


Key Features

  • Classifies Numbers: Determines if a number is prime, perfect, Armstrong, odd, or even.
  • Sum of Digits: Computes the sum of a number’s digits.
  • Fun Facts: Fetches a fun fact about the number using Numbers API.
  • API Response: Returns information in JSON format.
  • Handles Errors Gracefully: If an invalid input is provided, the API will return a meaningful error message.

API Endpoint

GET /api/classify-number?number=<integer>

Example Request:

curl "http://127.0.0.1:5000/api/classify-number?number=371"
Enter fullscreen mode Exit fullscreen mode

Success Response (200 OK):

{
    "number": 371,
    "is_prime": false,
    "is_perfect": false,
    "properties": ["armstrong", "odd"],
    "digit_sum": 11,
    "fun_fact": "371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371"
}
Enter fullscreen mode Exit fullscreen mode

Error Response (400 Bad Request):

{
    "number": "abc",
    "error": true
}
Enter fullscreen mode Exit fullscreen mode

Installation & Running the API

Prerequisites

Before running the API, ensure you have the following installed:

  • Python 3
  • Flask: Python web framework.
  • Requests Library: For making HTTP requests to fetch fun facts about the numbers.

Setup

  1. Clone the Repository:

    git clone https://github.com/P3dmund/HNG-Stage-1.git
    cd HNG-Stage-1
    
  2. Install Dependencies using requirements.txt:

    pip install -r requirements.txt
    
  3. Run the API:

    python app.py
    

    The API will be accessible at http://127.0.0.1:5000.

    You can now make requests to:

    http://127.0.0.1:5000/api/classify-number?number=371
    

Deployment on Render

Prerequisites

  • GitHub Account: You need a GitHub account to push your project.
  • Render Account: Render is a cloud platform that supports Python-based deployments.

Steps to Deploy the API on Render:

  1. Push the Code to GitHub:

    Push your code to a GitHub repository (if not done already).

  2. Create a New Web Service on Render:

    • Log in to your Render account.
    • Navigate to the "Dashboard" and click on New Web Service.
    • Link your GitHub repository to Render.
  3. Configure Start Command:

    Set the Start Command as:

    python app.py
    
  4. Deploy:

    • Click Deploy and wait for the deployment to finish.
    • After deployment, Render will provide a public URL for your API.

Gunicorn Challenges and Solutions

While deploying Flask applications on Render, it’s common to encounter Gunicorn-related issues, such as:

Problem:

After deployment, the API might fail with an error like:

ModuleNotFoundError: No module named 'flask'
Enter fullscreen mode Exit fullscreen mode

This error happens when Gunicorn can't locate the required dependencies, or the application isn't set up properly for a WSGI server.

Solution: Using Gunicorn Properly

  1. Install Gunicorn:

    pip install gunicorn
    
  2. Update Your app.py:

    Ensure your Flask app has the proper WSGI entry point:

    from flask import Flask, jsonify, request
    app = Flask(__name__)
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  3. Configure Gunicorn in Render:

    • In Render, modify the Start Command to use Gunicorn instead of python app.py:
      gunicorn app:app
    
  4. Redeploy the Application

After applying these fixes, your Number Facts API should work correctly on Render! 🎉


Testing the API

To test the API locally or after deployment, you can use:

  • Browser: Visit http://127.0.0.1:5000/api/classify-number?number=371 (for local testing).
  • Postman: Send a GET request to the URL and check the response.
  • cURL:

    curl "http://127.0.0.1:5000/api/classify-number?number=371"
    

Conclusion

This Number Facts API is a fun and interactive way to explore numbers and their properties through a Flask-based web service. It provides classification, digit sum computation, and fun facts about numbers.

This project marks Stage 1 of my journey in the HNG Internship DevOps Bootcamp. As I mentioned in my previous post, I’ll be sharing the next stage here as well! Stay tuned for more updates as I progress through the internship. 🚀

If you found this useful or are also participating in HNG, let’s connect! Drop a comment and share your experience. 👇

Top comments (0)