File size: 4,968 Bytes
eadf58c
 
 
 
 
 
 
a94c22f
eadf58c
49e2a28
 
 
 
 
 
 
 
 
 
 
 
 
 
085019b
eadf58c
 
 
 
085019b
49e2a28
eadf58c
 
 
49e2a28
ab4b2be
eadf58c
ab4b2be
 
 
db43bab
 
ab4b2be
 
 
 
 
 
 
db43bab
ab4b2be
eadf58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e2a28
 
 
 
 
 
 
 
085019b
ec30f42
085019b
49e2a28
 
 
 
 
 
 
 
eadf58c
49e2a28
 
 
 
 
 
eadf58c
ec30f42
eadf58c
ec30f42
 
49e2a28
eadf58c
 
ec30f42
 
 
 
49e2a28
eadf58c
ec30f42
49e2a28
eadf58c
ec30f42
085019b
 
49e2a28
ec30f42
 
ab4b2be
ec30f42
db43bab
eadf58c
ec30f42
 
 
eadf58c
ec30f42
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
import streamlit as st
import time
import pytz
from datetime import datetime
import matplotlib.pyplot as plt

# Set page configuration
st.set_page_config(page_title="MK316 Customized Timer", layout="centered")

# Initialize session state for countdown
if "countdown_started" not in st.session_state:
    st.session_state.countdown_started = False
if "start_time" not in st.session_state:
    st.session_state.start_time = 0
if "remaining_time" not in st.session_state:
    st.session_state.remaining_time = 0
if "time_up" not in st.session_state:
    st.session_state.time_up = False

# Title
st.title("🐧 MK316 Quiet Timer ⏳ ")

# Function to display the current time (as a live digital clock)
def display_current_time(current_time_placeholder):
    seoul_tz = pytz.timezone('Asia/Seoul')  # Set timezone to Seoul
    current_time = datetime.now(seoul_tz).strftime("%H:%M:%S")  # Convert to Seoul time
    
    # Style the clock (increase font size and set color)
    current_time_placeholder.markdown(
        f"<h1 style='text-align: center; font-size: 80px; color: #5785A4;'>{current_time}</h1>",  # Large font
        unsafe_allow_html=True
    )

# Function to update the progress circle
def update_progress_circle(remaining_time, total_time, time_up=False):
    fig, ax = plt.subplots(figsize=(2, 2))  # Smaller figure size to fit layout
    
    if time_up:
        # Show "Time's Up!" in the center of the circle
        ax.pie([1], colors=['#6d8c9c'], startangle=90, counterclock=False, wedgeprops=dict(width=0.15))
        ax.text(0, 0, "Time's Up!", fontsize=12, va='center', ha='center', color="gray")  # "Time's Up!" message inside the circle
    else:
        # Calculate the proportion of remaining time
        fraction_completed = remaining_time / total_time if total_time > 0 else 0
        ax.pie([fraction_completed, 1 - fraction_completed], colors=['#5785A4', '#D5DEDD'], startangle=90, counterclock=False, wedgeprops=dict(width=0.3))
        
        # Format and add remaining time as text in the center of the circle
        minutes, seconds = divmod(remaining_time, 60)
        ax.text(0, 0, f"{int(minutes):02d}:{int(seconds):02d}", fontsize=12, va='center', ha='center')  # Remaining time
    
    ax.set_aspect('equal')
    return fig

# Function to start the countdown timer
def start_countdown():
    if not st.session_state.countdown_started:
        st.session_state.remaining_time = st.session_state.start_time
        st.session_state.countdown_started = True
        st.session_state.time_up = False

# Function to reset the countdown timer
def reset_countdown():
    st.session_state.start_time = 0
    st.session_state.remaining_time = 0
    st.session_state.countdown_started = False
    st.session_state.time_up = False

# Input field for countdown time in seconds
st.session_state.start_time = st.number_input("Set Countdown Time (in seconds)", min_value=0, max_value=3600, value=10)

# Two columns: one for current time and buttons, another for circular progress
col1, col2 = st.columns([2, 1])

# Left column: Current time, input, buttons
with col1:
    # Placeholder to display the current time
    current_time_placeholder = st.empty()
    
    # Buttons to Start and Reset the countdown
    start_button, reset_button = st.columns([1, 1])
    with start_button:
        if st.button("Start"):
            start_countdown()
    with reset_button:
        if st.button("Reset"):
            reset_countdown()

    # Placeholder for displaying countdown text message
    countdown_placeholder = st.empty()

# Right column: Circular progress chart
with col2:
    progress_placeholder = st.empty()

# Timer countdown loop (only runs when countdown has started)
if st.session_state.countdown_started and not st.session_state.time_up:
    while st.session_state.remaining_time > 0:
        # Display the circular progress chart with time in the center
        fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time)
        progress_placeholder.pyplot(fig)

        # Display countdown time
        minutes, seconds = divmod(st.session_state.remaining_time, 60)
        countdown_placeholder.write(f"**Remaining Time:** {int(minutes):02d}:{int(seconds):02d}")
        
        # Update the remaining time
        st.session_state.remaining_time -= 1
        
        # Sleep for a second
        time.sleep(1)

        # Update current time in the placeholder
        display_current_time(current_time_placeholder)
        
    # When the countdown finishes
    st.session_state.time_up = True
    fig = update_progress_circle(0, st.session_state.start_time, time_up=True)  # Add "Time's Up!" message
    progress_placeholder.pyplot(fig)
    # countdown_placeholder.write("⏰ **Time's Up!**")

    # Play the sound using Streamlit's audio player
    audio_file = open("timesup.mp3", "rb")
    st.audio(audio_file.read(), format="audio/mp3")

    st.session_state.countdown_started = False