Update app.py
Browse files
app.py
CHANGED
@@ -5,55 +5,42 @@ from datetime import datetime
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
|
7 |
# Set page configuration
|
8 |
-
st.set_page_config(page_title="MK316 Timer
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def display_current_time():
|
12 |
seoul_tz = pytz.timezone('Asia/Seoul') # Set timezone to Seoul
|
13 |
current_time = datetime.now(seoul_tz).strftime("%H:%M:%S") # Convert to Seoul time
|
14 |
|
15 |
# Style the clock (increase font size and set color)
|
16 |
st.markdown(
|
17 |
-
f"<h1 style='text-align: center; font-size:
|
18 |
unsafe_allow_html=True
|
19 |
)
|
20 |
|
21 |
-
# Function to update the progress circle
|
22 |
-
def update_progress_circle(remaining_time, total_time
|
23 |
fig, ax = plt.subplots(figsize=(2, 2)) # Smaller figure size to fit layout
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
ax.text(0, 0, "Time's Up!", fontsize=10, va='center', ha='center') # Smaller font size for "Time's Up!"
|
29 |
-
else:
|
30 |
-
# Calculate the proportion of remaining time
|
31 |
-
fraction_completed = remaining_time / total_time if total_time > 0 else 0
|
32 |
-
ax.pie([fraction_completed, 1 - fraction_completed], colors=['#6d8c9c', '#D5DEDD'], startangle=90, counterclock=False, wedgeprops=dict(width=0.3))
|
33 |
-
|
34 |
-
# Format and add remaining time as text in the center of the circle
|
35 |
-
minutes, seconds = divmod(remaining_time, 60)
|
36 |
-
ax.text(0, 0, f"{int(minutes):02d}:{int(seconds):02d}", fontsize=14, va='center', ha='center') # Adjusted font size for remaining time
|
37 |
-
|
38 |
ax.set_aspect('equal')
|
39 |
return fig
|
40 |
|
41 |
-
# Initialize session state for countdown
|
42 |
-
if "countdown_started" not in st.session_state:
|
43 |
-
st.session_state.countdown_started = False
|
44 |
-
if "start_time" not in st.session_state:
|
45 |
-
st.session_state.start_time = 0
|
46 |
-
if "remaining_time" not in st.session_state:
|
47 |
-
st.session_state.remaining_time = 0
|
48 |
-
if "time_up" not in st.session_state:
|
49 |
-
st.session_state.time_up = False
|
50 |
-
|
51 |
-
# Display current time
|
52 |
-
display_current_time()
|
53 |
-
|
54 |
-
# Input field for countdown time in seconds
|
55 |
-
st.session_state.start_time = st.number_input("Set Countdown Time (in seconds)", min_value=0, max_value=3600, value=10)
|
56 |
-
|
57 |
# Function to start the countdown timer
|
58 |
def start_countdown():
|
59 |
if not st.session_state.countdown_started:
|
@@ -68,36 +55,57 @@ def reset_countdown():
|
|
68 |
st.session_state.countdown_started = False
|
69 |
st.session_state.time_up = False
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
# Placeholder for displaying
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Timer countdown logic (runs when countdown has started)
|
85 |
if st.session_state.countdown_started and not st.session_state.time_up:
|
86 |
if st.session_state.remaining_time > 0:
|
87 |
# Update the circular progress chart with time in the center
|
88 |
-
fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time
|
89 |
progress_placeholder.pyplot(fig)
|
90 |
|
91 |
-
#
|
92 |
st.session_state.remaining_time -= 1
|
|
|
|
|
93 |
time.sleep(1)
|
|
|
|
|
|
|
94 |
else:
|
95 |
# When the countdown finishes, display "Time's Up!" inside the circle
|
96 |
st.session_state.time_up = True
|
97 |
-
fig = update_progress_circle(
|
98 |
progress_placeholder.pyplot(fig)
|
|
|
99 |
|
100 |
-
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
time.sleep(0.1)
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
|
7 |
# Set page configuration
|
8 |
+
st.set_page_config(page_title="MK316 Quiet Timer", layout="centered")
|
9 |
|
10 |
+
# Initialize session state for countdown
|
11 |
+
if "countdown_started" not in st.session_state:
|
12 |
+
st.session_state.countdown_started = False
|
13 |
+
if "start_time" not in st.session_state:
|
14 |
+
st.session_state.start_time = 0
|
15 |
+
if "remaining_time" not in st.session_state:
|
16 |
+
st.session_state.remaining_time = 0
|
17 |
+
if "time_up" not in st.session_state:
|
18 |
+
st.session_state.time_up = False
|
19 |
+
|
20 |
+
# Title
|
21 |
+
st.title("🐧 MK316 Quiet Timer ⏳ ")
|
22 |
+
|
23 |
+
# Function to display the current time (as a live digital clock)
|
24 |
def display_current_time():
|
25 |
seoul_tz = pytz.timezone('Asia/Seoul') # Set timezone to Seoul
|
26 |
current_time = datetime.now(seoul_tz).strftime("%H:%M:%S") # Convert to Seoul time
|
27 |
|
28 |
# Style the clock (increase font size and set color)
|
29 |
st.markdown(
|
30 |
+
f"<h1 style='text-align: center; font-size: 80px; color: #5785A4;'>{current_time}</h1>", # Large font
|
31 |
unsafe_allow_html=True
|
32 |
)
|
33 |
|
34 |
+
# Function to update the progress circle
|
35 |
+
def update_progress_circle(remaining_time, total_time):
|
36 |
fig, ax = plt.subplots(figsize=(2, 2)) # Smaller figure size to fit layout
|
37 |
+
fraction_completed = remaining_time / total_time if total_time > 0 else 0
|
38 |
+
ax.pie([fraction_completed, 1 - fraction_completed], colors=['#5785A4', '#D5DEDD'], startangle=90, counterclock=False, wedgeprops=dict(width=0.3))
|
39 |
+
minutes, seconds = divmod(remaining_time, 60)
|
40 |
+
ax.text(0, 0, f"{int(minutes):02d}:{int(seconds):02d}", fontsize=14, va='center', ha='center') # Remaining time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
ax.set_aspect('equal')
|
42 |
return fig
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Function to start the countdown timer
|
45 |
def start_countdown():
|
46 |
if not st.session_state.countdown_started:
|
|
|
55 |
st.session_state.countdown_started = False
|
56 |
st.session_state.time_up = False
|
57 |
|
58 |
+
# Input field for countdown time in seconds
|
59 |
+
st.session_state.start_time = st.number_input("Set Countdown Time (in seconds)", min_value=0, max_value=3600, value=10)
|
60 |
+
|
61 |
+
# Two columns: one for current time and buttons, another for circular progress
|
62 |
+
col1, col2 = st.columns([2, 1])
|
63 |
+
|
64 |
+
# Left column: Current time, input, buttons
|
65 |
+
with col1:
|
66 |
+
# Display current time
|
67 |
+
display_current_time()
|
68 |
|
69 |
+
# Buttons to Start and Reset the countdown
|
70 |
+
start_button, reset_button = st.columns([1, 1])
|
71 |
+
with start_button:
|
72 |
+
if st.button("Start"):
|
73 |
+
start_countdown()
|
74 |
+
with reset_button:
|
75 |
+
if st.button("Reset"):
|
76 |
+
reset_countdown()
|
77 |
|
78 |
+
# Placeholder for displaying countdown text message
|
79 |
+
countdown_placeholder = st.empty()
|
80 |
+
|
81 |
+
# Right column: Circular progress chart
|
82 |
+
with col2:
|
83 |
+
progress_placeholder = st.empty()
|
84 |
|
85 |
# Timer countdown logic (runs when countdown has started)
|
86 |
if st.session_state.countdown_started and not st.session_state.time_up:
|
87 |
if st.session_state.remaining_time > 0:
|
88 |
# Update the circular progress chart with time in the center
|
89 |
+
fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time)
|
90 |
progress_placeholder.pyplot(fig)
|
91 |
|
92 |
+
# Update the remaining time
|
93 |
st.session_state.remaining_time -= 1
|
94 |
+
|
95 |
+
# Sleep for a second
|
96 |
time.sleep(1)
|
97 |
+
|
98 |
+
# Rerun the app to update every second
|
99 |
+
st.experimental_rerun()
|
100 |
else:
|
101 |
# When the countdown finishes, display "Time's Up!" inside the circle
|
102 |
st.session_state.time_up = True
|
103 |
+
fig = update_progress_circle(0, st.session_state.start_time)
|
104 |
progress_placeholder.pyplot(fig)
|
105 |
+
countdown_placeholder.write("⏰ **Time's Up!**")
|
106 |
|
107 |
+
# Play the sound using Streamlit's audio player
|
108 |
+
audio_file = open("timesup.mp3", "rb")
|
109 |
+
st.audio(audio_file.read(), format="audio/mp3")
|
110 |
|
111 |
+
st.session_state.countdown_started = False
|
|