Step 1: Set Up Your Project
- Create a project directory:bashCopy code
mkdir health_metrics_tracker cd health_metrics_tracker
- Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install Flask:
pip install Flask
Step 2: Create the Flask Application
- Create the main application file (
app.py
):
from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) # In-memory storage for health metrics health_data = [] @app.route('/') def index(): return render_template('index.html', health_data=health_data) @app.route('/add', methods=['POST']) def add_health_metric(): weight = request.form['weight'] blood_pressure = request.form['blood_pressure'] glucose = request.form['glucose'] health_data.append({ 'weight': weight, 'blood_pressure': blood_pressure, 'glucose': glucose }) return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True)
- Create the templates directory:
mkdir templates
- Create the HTML template (
templates/index.html
):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Health Metrics Tracker</title> </head> <body> <h1>Health Metrics Tracker</h1> <form action="/add" method="post"> <label for="weight">Weight (kg):</label> <input type="text" name="weight" required> <br> <label for="blood_pressure">Blood Pressure (mmHg):</label> <input type="text" name="blood_pressure" required> <br> <label for="glucose">Glucose Level (mg/dL):</label> <input type="text" name="glucose" required> <br> <button type="submit">Add Metric</button> </form> <h2>Recorded Metrics</h2> <ul> {% for data in health_data %} <li>Weight: {{ data.weight }} kg, Blood Pressure: {{ data.blood_pressure }} mmHg, Glucose: {{ data.glucose }} mg/dL</li> {% endfor %} </ul> </body> </html>
Step 3: Run Your Application
- In your terminal, run the application:
python app.py
- Open your web browser and go to
http://127.0.0.1:5000
.
Step 4: Test Your Application
- You can enter different health metrics, and they will be displayed on the page after submission.
Leave a Reply