Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from datetime import datetime | |
| import time | |
| # Function to convert seconds to a minutes:seconds format | |
| def format_time(seconds): | |
| """Convert seconds to a minutes:seconds format.""" | |
| return f"{seconds // 60:02d}:{seconds % 60:02d}" | |
| # Sidebar for customization options | |
| font_size = 60 # Fixed font size for the timer | |
| big_font_size = 100 # Fixed big font size for the timer (while running) | |
| font_styles = ["Arial", "Helvetica", "Times New Roman", "Courier", "Verdana"] # Example font styles | |
| selected_font_style = st.sidebar.selectbox('Select font style for the timer:', font_styles) # Font style selection | |
| background_color = st.sidebar.color_picker('Background color for the timer:', '#FFFFFF') | |
| font_color = st.sidebar.color_picker('Font color for the timer:', '#000000') | |
| app_title = st.sidebar.text_input('App title', 'My Streamlit App') # App title input | |
| st.title(app_title) # Display the app title | |
| # Initialize session state variables for the timer | |
| if 'countdown' not in st.session_state: | |
| st.session_state['countdown'] = 0 | |
| if 'timer_active' not in st.session_state: | |
| st.session_state['timer_active'] = False | |
| # Function to start the timer | |
| def start_timer(): | |
| st.session_state.timer_active = True | |
| st.session_state.countdown_end_time = time.time() + st.session_state.countdown | |
| # Timer setup in the main page | |
| st.write("") # For vertical spacing | |
| col1, col2, col3 = st.columns([1,2,1]) # Create three columns and place the timer in the middle one | |
| with col2: | |
| with st.form("Timer"): | |
| input_seconds = st.number_input("Set timer (in seconds):", min_value=10, max_value=36000, value=180, step=10) | |
| cols = st.columns(2) | |
| start_submitted = cols[0].form_submit_button("Start Timer") | |
| stop_submitted = cols[1].form_submit_button("Stop Timer") | |
| if start_submitted: | |
| st.session_state.countdown = input_seconds | |
| start_timer() | |
| if stop_submitted: | |
| st.session_state.timer_active = False | |
| # Display the countdown timer | |
| if st.session_state.timer_active: | |
| elapsed_time = int(time.time() - (st.session_state.countdown_end_time - st.session_state.countdown)) | |
| remaining_seconds = max(0, st.session_state.countdown - elapsed_time) | |
| current_font_size = big_font_size if st.session_state.timer_active else font_size # Use big_font_size if timer is active | |
| if remaining_seconds > 0: | |
| col2.markdown(f'<div style="font-size: {current_font_size}px; font-family: {selected_font_style}; font-weight: bold; color: {font_color}; background-color: {background_color};">{format_time(remaining_seconds)}</div>', unsafe_allow_html=True) | |
| time.sleep(1) | |
| st.experimental_rerun() | |
| else: | |
| st.session_state.timer_active = False | |
| col2.markdown(f'<div style="font-size: {font_size}px; font-family: {selected_font_style}; font-weight: bold; color: {font_color}; background-color: {background_color};">00:00</div>', unsafe_allow_html=True) | |
| st.balloons() |