amkj84 commited on
Commit
c620f02
·
verified ·
1 Parent(s): dfbea41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -17
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 os
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 Locally
109
- def save_results_locally(name, score, total_questions):
110
- # Get current timestamp for unique filenames
111
- timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
112
- filename = f"{name}_{timestamp}_quiz_results.csv"
 
 
 
 
113
 
114
  # Create DataFrame
115
  data = {"Name": [name], "Score": [score], "Total Questions": [total_questions]}
116
  df = pd.DataFrame(data)
117
 
118
- # Save to file
119
- df.to_csv(filename, index=False)
120
- return filename
121
 
122
- # Button to Save Results Locally
123
- if st.button("Finish Quiz and Save Results"):
124
- # Save results in a file
125
- results_filename = save_results_locally(name, score, len(questions))
126
-
127
- st.success(f"Your results have been saved! The results are stored in the file: `{results_filename}`")
128
- st.info("The file is stored on the server where this app is running.")
 
 
 
 
 
 
 
 
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("""