|
import streamlit as st |
|
import time |
|
import pytz |
|
from datetime import datetime |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
st.set_page_config(page_title="Countdown Timer", layout="centered") |
|
|
|
|
|
def display_current_time(): |
|
seoul_tz = pytz.timezone('Asia/Seoul') |
|
current_time = datetime.now(seoul_tz).strftime("%H:%M:%S") |
|
|
|
|
|
st.markdown( |
|
f"<h1 style='text-align: center; font-size: 60px; color: #5785A4;'>{current_time}</h1>", |
|
unsafe_allow_html=True |
|
) |
|
|
|
|
|
def update_progress_circle(remaining_time, total_time, time_up): |
|
fig, ax = plt.subplots(figsize=(2, 2)) |
|
|
|
if time_up: |
|
|
|
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') |
|
else: |
|
|
|
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)) |
|
|
|
|
|
minutes, seconds = divmod(remaining_time, 60) |
|
ax.text(0, 0, f"{int(minutes):02d}:{int(seconds):02d}", fontsize=14, va='center', ha='center') |
|
|
|
ax.set_aspect('equal') |
|
return fig |
|
|
|
|
|
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() |
|
|
|
|
|
st.session_state.start_time = st.number_input("Set Countdown Time (in seconds)", min_value=0, max_value=3600, value=10) |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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() |
|
|
|
|
|
progress_placeholder = st.empty() |
|
|
|
|
|
if st.session_state.countdown_started and not st.session_state.time_up: |
|
if st.session_state.remaining_time > 0: |
|
|
|
fig = update_progress_circle(st.session_state.remaining_time, st.session_state.start_time, time_up=False) |
|
progress_placeholder.pyplot(fig) |
|
|
|
|
|
st.session_state.remaining_time -= 1 |
|
time.sleep(1) |
|
else: |
|
|
|
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 |
|
|
|
|
|
time.sleep(0.1) |
|
|