Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from io import StringIO
|
| 4 |
from streamlit_lottie import st_lottie
|
| 5 |
import requests
|
| 6 |
-
import
|
| 7 |
-
import datetime
|
| 8 |
|
| 9 |
# Function to load Lottie animation
|
| 10 |
def load_lottie_url(url):
|
|
@@ -103,29 +103,44 @@ for index, q in enumerate(questions):
|
|
| 103 |
user_answer = st.radio(f"Choose the correct answer for Question {index + 1}:", q["options"], key=f"q{index}")
|
| 104 |
user_answers.append((q["question"], user_answer))
|
| 105 |
if user_answer == q["answer"]:
|
|
|
|
| 106 |
score += 1
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
# Save Results
|
| 109 |
-
def
|
| 110 |
-
# Get current
|
| 111 |
-
|
| 112 |
-
filename = f"{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# Create DataFrame
|
| 115 |
data = {"Name": [name], "Score": [score], "Total Questions": [total_questions]}
|
| 116 |
df = pd.DataFrame(data)
|
| 117 |
|
| 118 |
-
# Save to
|
| 119 |
-
df.to_csv(
|
| 120 |
-
return
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
if st.button("
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# Footer
|
| 131 |
st.markdown("""
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
from io import StringIO
|
| 5 |
from streamlit_lottie import st_lottie
|
| 6 |
import requests
|
| 7 |
+
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
# Function to load Lottie animation
|
| 10 |
def load_lottie_url(url):
|
|
|
|
| 103 |
user_answer = st.radio(f"Choose the correct answer for Question {index + 1}:", q["options"], key=f"q{index}")
|
| 104 |
user_answers.append((q["question"], user_answer))
|
| 105 |
if user_answer == q["answer"]:
|
| 106 |
+
st.success("\u2705 Correct!")
|
| 107 |
score += 1
|
| 108 |
+
else:
|
| 109 |
+
st.error(f"\u274C Incorrect! The correct answer is: {q['answer']}")
|
| 110 |
|
| 111 |
+
# Save Results In-Specific Path (Desktop or C drive)
|
| 112 |
+
def save_results_to_desktop(name, score, total_questions):
|
| 113 |
+
# Get the current date and time for unique file names
|
| 114 |
+
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 115 |
+
filename = f"quiz_results_{current_time}.csv"
|
| 116 |
+
|
| 117 |
+
# Path to the Desktop folder (change to your username in Windows)
|
| 118 |
+
# Example for Windows:
|
| 119 |
+
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop", filename)
|
| 120 |
|
| 121 |
# Create DataFrame
|
| 122 |
data = {"Name": [name], "Score": [score], "Total Questions": [total_questions]}
|
| 123 |
df = pd.DataFrame(data)
|
| 124 |
|
| 125 |
+
# Save to CSV
|
| 126 |
+
df.to_csv(desktop_path, index=False)
|
| 127 |
+
return desktop_path
|
| 128 |
|
| 129 |
+
# Display Final Score
|
| 130 |
+
if st.button("Show Final Score"):
|
| 131 |
+
st.write(f"**Your final score is: {score}/{len(questions)}** \U0001F389")
|
| 132 |
+
if score == len(questions):
|
| 133 |
+
st.balloons()
|
| 134 |
+
st_lottie(animation_success, height=200, key="success")
|
| 135 |
+
st.success("Excellent! You're a respiratory system expert!")
|
| 136 |
+
elif score >= len(questions) // 2:
|
| 137 |
+
st.info("Good job! You have a solid understanding.")
|
| 138 |
+
else:
|
| 139 |
+
st.warning("Keep learning! You can do better next time.")
|
| 140 |
+
|
| 141 |
+
# Save and provide a download button for the result
|
| 142 |
+
result_file_path = save_results_to_desktop(name, score, len(questions))
|
| 143 |
+
st.write(f"Your result has been saved to: {result_file_path}")
|
| 144 |
|
| 145 |
# Footer
|
| 146 |
st.markdown("""
|