Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,24 @@ import altair as alt
|
|
| 3 |
import pandas as pd
|
| 4 |
from fpdf import FPDF
|
| 5 |
from datetime import datetime
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# ---------- Inject Custom CSS for
|
| 9 |
st.markdown(
|
| 10 |
"""
|
| 11 |
<style>
|
| 12 |
-
/*
|
| 13 |
-
|
| 14 |
-
background
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
linear-gradient(-45deg, transparent 75%, #fff 75%);
|
| 20 |
-
background-size: 56.57px 56.57px;
|
| 21 |
-
background-position: 0 0, 0 0, 28.28px 28.28px, 28.28px 28.28px;
|
| 22 |
}
|
| 23 |
-
/* Glassy
|
| 24 |
.glassy-container {
|
| 25 |
background: rgba(0, 0, 0, 0.5);
|
|
|
|
|
|
|
| 26 |
backdrop-filter: blur(12px);
|
| 27 |
border: 1px solid transparent;
|
| 28 |
border-radius: 10px;
|
|
@@ -35,7 +33,7 @@ st.markdown(
|
|
| 35 |
.glassy-container:hover {
|
| 36 |
transform: scale(1.02);
|
| 37 |
}
|
| 38 |
-
/* Headings */
|
| 39 |
h1, h2, h3, p, label {
|
| 40 |
color: #fff;
|
| 41 |
text-shadow: 0 0 3px rgba(0,0,0,0.7);
|
|
@@ -81,7 +79,7 @@ st.markdown(
|
|
| 81 |
transform: translateY(-2px);
|
| 82 |
box-shadow: 0 6px 16px rgba(0,0,0,0.8);
|
| 83 |
}
|
| 84 |
-
/* Footer */
|
| 85 |
footer {
|
| 86 |
width: 100%;
|
| 87 |
background: #000;
|
|
@@ -90,7 +88,7 @@ st.markdown(
|
|
| 90 |
text-align: center;
|
| 91 |
border-top: 2px solid #fff;
|
| 92 |
}
|
| 93 |
-
/* Altair
|
| 94 |
.streamlit-expanderHeader {
|
| 95 |
font-weight: bold;
|
| 96 |
}
|
|
@@ -99,7 +97,8 @@ st.markdown(
|
|
| 99 |
unsafe_allow_html=True
|
| 100 |
)
|
| 101 |
|
| 102 |
-
# ----------
|
|
|
|
| 103 |
st.title("Mini Orientation Inventory")
|
| 104 |
st.markdown(
|
| 105 |
"#### Discover your self-actualizing potential with this Mini Orientation Inventory. "
|
|
@@ -165,38 +164,44 @@ recommendations = {
|
|
| 165 |
|
| 166 |
# ---------- Create the Assessment Form ----------
|
| 167 |
with st.form(key="assessment_form"):
|
|
|
|
| 168 |
name = st.text_input("Enter your full name")
|
| 169 |
st.markdown("##### Rate the following statements on a scale from 1 (Not at all) to 5 (Very Much):")
|
| 170 |
user_responses = {}
|
| 171 |
for q in questions:
|
| 172 |
user_responses[q["trait"]] = st.slider(q["question"], 1, 5, 3, key=q["trait"])
|
| 173 |
submitted = st.form_submit_button("Submit Assessment")
|
|
|
|
|
|
|
| 174 |
|
| 175 |
# ---------- Process and Display Results ----------
|
| 176 |
if submitted and name:
|
| 177 |
st.success(f"Thank you, {name}! Your results are ready.")
|
| 178 |
|
| 179 |
-
#
|
|
|
|
| 180 |
st.markdown(
|
| 181 |
f"""
|
| 182 |
-
<
|
| 183 |
-
|
| 184 |
-
<
|
| 185 |
-
|
| 186 |
-
</ul>
|
| 187 |
-
</div>
|
| 188 |
""",
|
| 189 |
unsafe_allow_html=True
|
| 190 |
)
|
|
|
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
|
|
|
|
| 194 |
for trait, score in user_responses.items():
|
| 195 |
rec_html += f"<li><strong>{trait}:</strong> {recommendations[trait][score]}</li>"
|
| 196 |
-
rec_html += "</ul>
|
| 197 |
st.markdown(rec_html, unsafe_allow_html=True)
|
|
|
|
| 198 |
|
| 199 |
-
#
|
|
|
|
| 200 |
df = pd.DataFrame({
|
| 201 |
"Trait": list(user_responses.keys()),
|
| 202 |
"Score": list(user_responses.values())
|
|
@@ -207,17 +212,10 @@ if submitted and name:
|
|
| 207 |
color=alt.Color("Score:Q", scale=alt.Scale(scheme="category10"))
|
| 208 |
).properties(width=700, height=400)
|
| 209 |
st.altair_chart(chart, use_container_width=True)
|
|
|
|
| 210 |
|
| 211 |
-
#
|
| 212 |
-
|
| 213 |
-
try:
|
| 214 |
-
chart.save(chart_path, scale_factor=2.0)
|
| 215 |
-
except Exception as e:
|
| 216 |
-
st.error("Error saving chart image: " + str(e) + ". Please install the 'vl-convert-python' package (pip install vl-convert-python) to enable PNG export.")
|
| 217 |
-
chart_path = None
|
| 218 |
-
|
| 219 |
-
# ---------- PDF Report Generation Function using FPDF ----------
|
| 220 |
-
def generate_pdf_report(name, responses, recs, generated_on, chart_path=None):
|
| 221 |
pdf = FPDF()
|
| 222 |
pdf.add_page()
|
| 223 |
# Header with Black background, White text
|
|
@@ -242,23 +240,16 @@ if submitted and name:
|
|
| 242 |
pdf.set_font("Arial", "", 12)
|
| 243 |
for trait, score in responses.items():
|
| 244 |
pdf.multi_cell(0, 10, f"{trait}: {recs[trait][score]}")
|
| 245 |
-
|
| 246 |
-
# Include Chart Image if available
|
| 247 |
-
if chart_path and os.path.exists(chart_path):
|
| 248 |
-
pdf.set_font("Arial", "B", 12)
|
| 249 |
-
pdf.cell(0, 10, "Score Visualization:", ln=True)
|
| 250 |
-
pdf.image(chart_path, x=10, y=pdf.get_y(), w=pdf.epw)
|
| 251 |
-
pdf.ln(5)
|
| 252 |
-
# Footer
|
| 253 |
pdf.set_y(-20)
|
| 254 |
pdf.set_font("Arial", "I", 8)
|
| 255 |
pdf.cell(0, 10, "Developed by Kabura Kuria - Contact: +254 707 865 934", 0, 0, "C")
|
| 256 |
pdf.ln(5)
|
| 257 |
-
pdf.cell(0, 10, "© 2025
|
| 258 |
return pdf.output(dest="S").encode("latin1")
|
| 259 |
|
| 260 |
generated_on = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 261 |
-
pdf_bytes = generate_pdf_report(name, user_responses, recommendations, generated_on
|
| 262 |
|
| 263 |
st.download_button(
|
| 264 |
label="Download PDF Report",
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from fpdf import FPDF
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
+
# ---------- Inject Custom CSS for a Faded, Existential Background and Glassy Widgets with Architectural Patterns ----------
|
| 8 |
st.markdown(
|
| 9 |
"""
|
| 10 |
<style>
|
| 11 |
+
/* Faded existential background inspired by Dr. Suss architectural style */
|
| 12 |
+
body {
|
| 13 |
+
background: linear-gradient(rgba(26,26,26,0.8), rgba(26,26,26,0.8)),
|
| 14 |
+
url('https://www.toptal.com/designers/subtlepatterns/patterns/dark_wall.png');
|
| 15 |
+
background-size: cover;
|
| 16 |
+
background-attachment: fixed;
|
| 17 |
+
color: #fff;
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
/* Glassy Container with a subtle architectural pattern overlay */
|
| 20 |
.glassy-container {
|
| 21 |
background: rgba(0, 0, 0, 0.5);
|
| 22 |
+
background-image: url('https://www.toptal.com/designers/subtlepatterns/patterns/cubes.png');
|
| 23 |
+
background-blend-mode: overlay;
|
| 24 |
backdrop-filter: blur(12px);
|
| 25 |
border: 1px solid transparent;
|
| 26 |
border-radius: 10px;
|
|
|
|
| 33 |
.glassy-container:hover {
|
| 34 |
transform: scale(1.02);
|
| 35 |
}
|
| 36 |
+
/* Headings and text styling */
|
| 37 |
h1, h2, h3, p, label {
|
| 38 |
color: #fff;
|
| 39 |
text-shadow: 0 0 3px rgba(0,0,0,0.7);
|
|
|
|
| 79 |
transform: translateY(-2px);
|
| 80 |
box-shadow: 0 6px 16px rgba(0,0,0,0.8);
|
| 81 |
}
|
| 82 |
+
/* Footer styling */
|
| 83 |
footer {
|
| 84 |
width: 100%;
|
| 85 |
background: #000;
|
|
|
|
| 88 |
text-align: center;
|
| 89 |
border-top: 2px solid #fff;
|
| 90 |
}
|
| 91 |
+
/* Ensure Altair charts are adjusted */
|
| 92 |
.streamlit-expanderHeader {
|
| 93 |
font-weight: bold;
|
| 94 |
}
|
|
|
|
| 97 |
unsafe_allow_html=True
|
| 98 |
)
|
| 99 |
|
| 100 |
+
# ---------- Wrap Title, Description, and Form in a Glassy Widget ----------
|
| 101 |
+
st.markdown("<div class='glassy-container'>", unsafe_allow_html=True)
|
| 102 |
st.title("Mini Orientation Inventory")
|
| 103 |
st.markdown(
|
| 104 |
"#### Discover your self-actualizing potential with this Mini Orientation Inventory. "
|
|
|
|
| 164 |
|
| 165 |
# ---------- Create the Assessment Form ----------
|
| 166 |
with st.form(key="assessment_form"):
|
| 167 |
+
st.markdown("<div class='glassy-container'>", unsafe_allow_html=True)
|
| 168 |
name = st.text_input("Enter your full name")
|
| 169 |
st.markdown("##### Rate the following statements on a scale from 1 (Not at all) to 5 (Very Much):")
|
| 170 |
user_responses = {}
|
| 171 |
for q in questions:
|
| 172 |
user_responses[q["trait"]] = st.slider(q["question"], 1, 5, 3, key=q["trait"])
|
| 173 |
submitted = st.form_submit_button("Submit Assessment")
|
| 174 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 175 |
+
st.markdown("</div>", unsafe_allow_html=True) # Close the initial glassy container
|
| 176 |
|
| 177 |
# ---------- Process and Display Results ----------
|
| 178 |
if submitted and name:
|
| 179 |
st.success(f"Thank you, {name}! Your results are ready.")
|
| 180 |
|
| 181 |
+
# Orientation Profile section
|
| 182 |
+
st.markdown("<div class='glassy-container'>", unsafe_allow_html=True)
|
| 183 |
st.markdown(
|
| 184 |
f"""
|
| 185 |
+
<h2 style="text-align:center;">{name}'s Orientation Profile</h2>
|
| 186 |
+
<ul>
|
| 187 |
+
{"".join([f"<li><strong>{trait}:</strong> {score}/5</li>" for trait, score in user_responses.items()])}
|
| 188 |
+
</ul>
|
|
|
|
|
|
|
| 189 |
""",
|
| 190 |
unsafe_allow_html=True
|
| 191 |
)
|
| 192 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 193 |
|
| 194 |
+
# Therapeutic Recommendations section
|
| 195 |
+
st.markdown("<div class='glassy-container'>", unsafe_allow_html=True)
|
| 196 |
+
rec_html = "<h3 style='text-align:center;'>Therapeutic Recommendations</h3><ul>"
|
| 197 |
for trait, score in user_responses.items():
|
| 198 |
rec_html += f"<li><strong>{trait}:</strong> {recommendations[trait][score]}</li>"
|
| 199 |
+
rec_html += "</ul>"
|
| 200 |
st.markdown(rec_html, unsafe_allow_html=True)
|
| 201 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 202 |
|
| 203 |
+
# Multicolored Bar Chart of Scores in a full-width glassy container
|
| 204 |
+
st.markdown("<div class='glassy-container'>", unsafe_allow_html=True)
|
| 205 |
df = pd.DataFrame({
|
| 206 |
"Trait": list(user_responses.keys()),
|
| 207 |
"Score": list(user_responses.values())
|
|
|
|
| 212 |
color=alt.Color("Score:Q", scale=alt.Scale(scheme="category10"))
|
| 213 |
).properties(width=700, height=400)
|
| 214 |
st.altair_chart(chart, use_container_width=True)
|
| 215 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 216 |
|
| 217 |
+
# ---------- PDF Report Generation Function using FPDF (without chart image) ----------
|
| 218 |
+
def generate_pdf_report(name, responses, recs, generated_on):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
pdf = FPDF()
|
| 220 |
pdf.add_page()
|
| 221 |
# Header with Black background, White text
|
|
|
|
| 240 |
pdf.set_font("Arial", "", 12)
|
| 241 |
for trait, score in responses.items():
|
| 242 |
pdf.multi_cell(0, 10, f"{trait}: {recs[trait][score]}")
|
| 243 |
+
# Footer (removed the phrase "LEARN THE CRAFT OF PSYCHOLOGY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
pdf.set_y(-20)
|
| 245 |
pdf.set_font("Arial", "I", 8)
|
| 246 |
pdf.cell(0, 10, "Developed by Kabura Kuria - Contact: +254 707 865 934", 0, 0, "C")
|
| 247 |
pdf.ln(5)
|
| 248 |
+
pdf.cell(0, 10, "© 2025", 0, 0, "C")
|
| 249 |
return pdf.output(dest="S").encode("latin1")
|
| 250 |
|
| 251 |
generated_on = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 252 |
+
pdf_bytes = generate_pdf_report(name, user_responses, recommendations, generated_on)
|
| 253 |
|
| 254 |
st.download_button(
|
| 255 |
label="Download PDF Report",
|