Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
import pytz
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Set page configuration
|
| 8 |
+
st.set_page_config(page_title="Countdown Timer", layout="centered")
|
| 9 |
+
|
| 10 |
+
# Function to display the current time (Seoul Time)
|
| 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: 60px; color: #5785A4;'>{current_time}</h1>", # Big font size for the clock
|
| 18 |
+
unsafe_allow_html=True
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Function to update the progress circle with time inside or display "Time's Up!"
|
| 22 |
+
def update_progress_circle(remaining_time, total_time, time_up):
|
| 23 |
+
fig, ax = plt.subplots(figsize=(2, 2)) # Smaller figure size to fit layout
|
| 24 |
+
|
| 25 |
+
if time_up:
|
| 26 |
+
# Show "Time's Up!" in the center of the circle
|
| 27 |
+
ax.pie([1], colors=['#6d8c9c'], startangle=90, counterclock=False, wedgeprops=dict(width=0.3))
|
| 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:
|
| 60 |
+
st.session_state.remaining_time = st.session_state.start_time
|
| 61 |
+
st.session_state.countdown_started = True
|
| 62 |
+
st.session_state.time_up = False
|
| 63 |
+
|
| 64 |
+
# Function to reset the countdown timer
|
| 65 |
+
def reset_countdown():
|
| 66 |
+
st.session_state.start_time = 0
|
| 67 |
+
st.session_state.remaining_time = 0
|
| 68 |
+
st.session_state.countdown_started = False
|
| 69 |
+
st.session_state.time_up = False
|
| 70 |
+
|
| 71 |
+
# Buttons to Start and Reset the countdown
|
| 72 |
+
start_button, reset_button = st.columns([1, 1])
|
| 73 |
+
with start_button:
|
| 74 |
+
if st.button("Start Countdown"):
|
| 75 |
+
start_countdown()
|
| 76 |
+
|
| 77 |
+
with reset_button:
|
| 78 |
+
if st.button("Reset Countdown"):
|
| 79 |
+
reset_countdown()
|
| 80 |
+
|
| 81 |
+
# Placeholder for displaying the countdown time
|
| 82 |
+
progress_placeholder = st.empty()
|
| 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, time_up=False)
|
| 89 |
+
progress_placeholder.pyplot(fig)
|
| 90 |
+
|
| 91 |
+
# Decrease the remaining time
|
| 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(st.session_state.remaining_time, st.session_state.start_time, time_up=True)
|
| 98 |
+
progress_placeholder.pyplot(fig)
|
| 99 |
+
|
| 100 |
+
st.session_state.countdown_started = False
|
| 101 |
+
|
| 102 |
+
# Ensure continuous display of current time
|
| 103 |
+
time.sleep(0.1)
|