from flask import Flask, request, jsonify
app = Flask(__name__)
# Initialize an empty dictionary to store user data
user_data = {}
@app.route('/api/saveUserData', methods=['POST'])
def save_user_data():
data = request.json
# Ensure the "username" key is in the data
if 'username' not in data:
return jsonify({"error": "Username is required."}), 400
username = data['username']
# Store user data in the dictionary
user_data[username] = data
return jsonify({"message": "Data saved successfully"})
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask, request, jsonify
from flask_restful import Api, Resource # used for REST API building
import requests # used for testing
import random
calcalc_api = __name__ ('calcalc_api', __name__,
url_prefix='/api/calcalc')
api = Api(calcalc_api)
app = Flask(__name__)
def calculateCalories(user_data):
age = user_data['age']
weight = user_data['weight']
height = user_data['height']
activity = user_data['activity']
gender = user_data['gender']
# Define activity level multipliers
activity_levels = {
'sedentary': 1.2,
'lightly_active': 1.375,
'moderately_active': 1.55,
'very_active': 1.725
}
# Example calorie calculation (Harris-Benedict equation)
if gender == 'male':
calorie_maintenance = (88.362 + 13.397 * weight + 4.799 * height - 5.677 * age) * activity_levels[activity]
else:
calorie_maintenance = (447.593 + 9.247 * weight + 3.098 * height - 4.330 * age) * activity_levels[activity]
return calorie_maintenance
- It imports the library for the api at the top
- Creates an empyt dictionairy to store the user data.
- It requires a username and if you don’t give one it says username is required and if it is there then it assigns it to the user
- It stores all the users data in the dictionary
-
if statement ensures the flask server runs the script
- Creates the API
- It defines all the users variables as data
- It calculates calorie maintnence to input it into the data base.
- It does the calculations and stores it as the variable “calorie maintnence”