Repository navigation
Expand file tree
/
Copy pathapp.py.back.py
More file actions
195 lines (155 loc) · 6.34 KB
/
Copy pathapp.py.back.py
File metadata and controls
195 lines (155 loc) · 6.34 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from flask import Flask, render_template, request, redirect, url_for, session
import pandas as pd
import sqlite3
import json
import os
# =========================
# SETUP
# =========================
app = Flask(__name__)
app.secret_key = "supersecret"
DB_PATH = os.path.join(os.path.dirname(__file__), "beyond.db")
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
# =========================
# LOAD DATA
# =========================
def load_sector_data():
base = os.path.dirname(__file__)
path = os.path.join(base, "data", "sector_jobs.json")
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
sector_data = load_sector_data()
# Load jobs.csv
try:
jobs = pd.read_csv(os.path.join(os.path.dirname(__file__), "data", "jobs.csv"))
except FileNotFoundError:
jobs = pd.DataFrame(columns=["title", "description"]) # fallback so the app won't crash
# =========================
# PERSONALITY TEST QUESTIONS
# =========================
PERSONALITY_QUESTIONS = [
{"id": "openness", "text": "I enjoy exploring new ideas and concepts."},
{"id": "conscientiousness", "text": "I like to plan and stay organized."},
{"id": "extraversion", "text": "I gain energy from being around other people."},
{"id": "agreeableness", "text": "I prefer harmony over conflict."},
{"id": "neuroticism", "text": "I often feel stressed in challenging situations."},
]
LIFESTYLE_QUESTIONS = [
{"id": "social_pref", "text": "I prefer working alone rather than in a team."},
{"id": "stem_interest", "text": "I enjoy solving analytical or technical problems (STEM)."},
{"id": "noise_comfort", "text": "I am comfortable working in noisy or busy environments."},
{"id": "no_work_choice", "text": "If I didn’t need to work, I would still do creative or learning activities."},
{"id": "hands_on", "text": "I like jobs that let me build or fix things physically."}
]
# =========================
# ROUTES
# =========================
@app.route("/")
def home():
return render_template("home.html")
# ---------- AUTH ----------
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form.get("username").strip()
password = request.form.get("password").strip()
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT username, password FROM users WHERE username = ?", (username,))
user = cursor.fetchone()
conn.close()
if user and user["password"] == password:
session["user"] = username
return redirect(url_for("test"))
else:
error = "Invalid username or password."
return render_template("login.html", error=error)
return render_template("login.html")
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
username = request.form.get("username").strip()
password = request.form.get("password").strip()
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
conn.commit()
session["user"] = username
return redirect(url_for("test"))
except sqlite3.IntegrityError:
error = "Username already exists."
return render_template("register.html", error=error)
finally:
conn.close()
return render_template("register.html")
@app.route("/logout")
def logout():
session.pop("user", None)
return redirect(url_for("home"))
# ---------- PERSONALITY TEST ----------
@app.route("/test", methods=["GET", "POST"])
def test():
if request.method == "POST":
answers = {k: int(v) for k, v in request.form.items()}
session["answers"] = answers
return redirect(url_for("results"))
return render_template("test.html", personality=PERSONALITY_QUESTIONS, lifestyle=LIFESTYLE_QUESTIONS)
@app.route("/results")
def results():
answers = session.get("answers", {})
if not answers:
return redirect(url_for("test"))
# --- Calculate personality and preferences ---
ocean_score = (answers["openness"] + answers["conscientiousness"] +
answers["extraversion"] + answers["agreeableness"] +
(6 - answers["neuroticism"])) / 5
preferences = {
"Technology": (answers["stem_interest"] + answers["hands_on"]) / 2,
"Education": (answers["openness"] + answers["no_work_choice"]) / 2,
"Healthcare": (answers["agreeableness"] + (6 - answers["noise_comfort"])) / 2,
"Business": (answers["extraversion"] + answers["conscientiousness"]) / 2,
"Creative": (answers["openness"] + answers["no_work_choice"]) / 2,
"Agriculture": answers["hands_on"]
}
ranked = sorted(preferences.items(), key=lambda x: x[1], reverse=True)
# --- Match jobs ---
top_sector = ranked[0][0].lower()
matches = jobs[jobs["description"].str.contains(top_sector, case=False, na=False)].head(5)
# --- Save results ---
if "user" in session:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"INSERT INTO results (username, result_json) VALUES (?, ?)",
(session["user"], json.dumps(ranked))
)
conn.commit()
conn.close()
return render_template("results.html", ranked=ranked, matches=matches.to_dict(orient="records"))
@app.route("/my_results")
def my_results():
if "user" not in session:
return redirect(url_for("login"))
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT result_json, timestamp FROM results WHERE username = ? ORDER BY timestamp DESC",
(session["user"],)
)
rows = cursor.fetchall()
conn.close()
user_results = [{"data": json.loads(row["result_json"]), "time": row["timestamp"]} for row in rows]
return render_template("my_results.html", user_results=user_results)
@app.route("/sectors")
def show_all_sectors():
import json, os
json_path = os.path.join("data", "sector_jobs.json")
with open(json_path, "r", encoding="utf-8") as f:
sectors = json.load(f)
return render_template("sectors.html", sectors=sectors)
if __name__ == "__main__":
app.run(debug=True)