mytimer / app.py
MK-316's picture
Create app.py
eadf58c verified
raw
history blame
4.14 kB
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="Countdown Timer", layout="centered")
# Function to display the current time (Seoul Time)
def display_current_time():
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)
st.markdown(
f"<h1 style='text-align: center; font-size: 60px; color: #5785A4;'>{current_time}</h1>", # Big font size for the clock
unsafe_allow_html=True
)
# Function to update the progress circle with time inside or display "Time's Up!"
def update_progress_circle(remaining_time, total_time, time_up):
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.3))
ax.text(0, 0, "Time's Up!", fontsize=10, va='center', ha='center') # Smaller font size for "Time's Up!"
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=['#6d8c9c', '#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=14, va='center', ha='center') # Adjusted font size for remaining time
ax.set_aspect('equal')
return fig
# 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
# Display current time
display_current_time()
# 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)
# 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
# Buttons to Start and Reset the countdown
start_button, reset_button = st.columns([1, 1])
with start_button:
if st.button("Start Countdown"):
start_countdown()
with reset_button:
if st.button("Reset Countdown"):
reset_countdown()
# Placeholder for displaying the countdown time
progress_placeholder = st.empty()
# Timer countdown logic (runs when countdown has started)
if st.session_state.countdown_started and not st.session_state.time_up:
if st.session_state.remaining_time > 0:
# Update the circular progress chart with time in the center
fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time, time_up=False)
progress_placeholder.pyplot(fig)
# Decrease the remaining time
st.session_state.remaining_time -= 1
time.sleep(1)
else:
# When the countdown finishes, display "Time's Up!" inside the circle
st.session_state.time_up = True
fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time, time_up=True)
progress_placeholder.pyplot(fig)
st.session_state.countdown_started = False
# Ensure continuous display of current time
time.sleep(0.1)