Simple Flask Server

%%python --bg

from flask import Flask, request, jsonify
from flask_cors import CORS

# initialize a flask application (app)
app = Flask(__name__)
CORS(app, supports_credentials=True, origins="*")  # Allow all origins (*)

users = ["advik", "aashray", "akhil", "srijan"]

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        # Get the user data from the form
        user_data = request.form.get("user")
        if user_data:
            # Add the user to the list
            users.append(user_data)
            return """
            <p>User added successfully!</p>
            <br>
            <a href="/users"><button>View Users</button></a>"""
    
    # Display the form to add a user
    return """
        <form method="POST" action="/">
            <label for="user">Add a user:</label>
            <input type="text" id="user" name="user" required>
            <input type="submit" value="Submit">
        </form>
        <br>
        <a href="/users"><button>View Users</button></a>
    """
@app.route("/users", methods=["GET"])
def show_users():
    # Return the list of users in JSON format
    return jsonify(users)

if __name__ == "__main__":
    # starts flask server on default port, http://127.0.0.1:5001
    from werkzeug.serving import run_simple
    run_simple("localhost", 5001, app)

Tester Function

import requests

# Tester program to check any function that returns something
def tester(result, expected_result):
    try:
        # Validate the functionality
        if result == expected_result:
            print("Users match! Everything is secure!")
        else:
            print("Users did not match!! Check immediately!")
    except Exception as e:
        # Handle exceptions gracefully
        print(f"Test failed: An error occurred - {str(e)}")

def get_users():
    req = requests.get("http://localhost:5001/users")
    userlst = req.text
    charlist = ["'", '"', "[", "]", "\n"]
    for i in charlist:
        userlst = userlst.replace(i, "")
    userlst = userlst.strip().split(",")
    req.close()
    return userlst

tester(get_users(), ["advik", "aashray", "akhil", "srijan"])
Users match! Everything is secure!