-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (38 loc) · 1.53 KB
/
Copy pathapp.py
File metadata and controls
45 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from flask import Flask, render_template, request
from analysis import load_data, analyze_sleep, create_plot, get_recommendations, get_occupations
import matplotlib
matplotlib.use('Agg') # Set non-interactive backend
app = Flask(__name__)
# Load data once when app starts
df = load_data()
@app.route('/')
def index():
analysis_results = analyze_sleep(df)
plot_data = create_plot(df)
return render_template('index.html',
analysis=analysis_results,
plot=plot_data,
occupations=get_occupations(df)
)
@app.route('/analyze', methods=['POST'])
def analyze():
try:
age = int(request.form['age'])
occupation = str(request.form.get('occupation', '').strip().title())
sleep_duration = float(request.form['sleep_duration'])
sleep_quality = float(request.form['sleep_quality'])
recommendations = get_recommendations(
age, occupation, sleep_duration, sleep_quality
)
return render_template('index.html',
analysis=analyze_sleep(df),
plot=create_plot(df),
recommendations=recommendations,
user_data=request.form)
except Exception as e:
return render_template('index.html',
analysis=analyze_sleep(df),
plot=create_plot(df),
error=str(e))
if __name__ == '__main__':
app.run(debug=True)