yashm commited on
Commit
8d82a89
·
verified ·
1 Parent(s): fe11b63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -1,63 +1,61 @@
1
  import streamlit as st
2
- from datetime import datetime, timedelta
3
- import pytz
4
  import time
5
 
6
- # Sidebar for user inputs
7
- font_size = st.sidebar.slider('Select font size for the timer:', min_value=20, max_value=100, value=60)
8
- background_color = st.sidebar.color_picker('Select background color:', '#FFFFFF')
9
- text_color = st.sidebar.color_picker('Select text color:', '#000000')
10
 
11
- # Apply user-selected styles
12
- st.markdown(f"""
13
- <style>
14
- .big-font {{
15
- font-size: {font_size}px !important;
16
- font-weight: bold;
17
- color: {text_color} !important;
18
- background-color: {background_color} !important;
19
- }}
20
- </style>
21
- """, unsafe_allow_html=True)
22
 
23
- # Check if countdown and timer_active keys exist in the session state
 
 
24
  if 'countdown' not in st.session_state:
25
- st.session_state.countdown = 0
26
  if 'timer_active' not in st.session_state:
27
- st.session_state.timer_active = False
28
 
29
- # Start the timer
30
  def start_timer():
31
  st.session_state.timer_active = True
32
  st.session_state.countdown_end_time = time.time() + st.session_state.countdown
33
 
34
- # Timer input form
35
- with st.form("Timer"):
36
- input_seconds = st.number_input("Set timer (in seconds):", min_value=10, max_value=3600, value=180, step=10)
37
- submitted = st.form_submit_button("Start Timer")
38
- if submitted:
39
- st.session_state.countdown = input_seconds
40
- start_timer()
41
-
42
- # Update and display the timer
 
 
 
 
 
 
 
43
  if st.session_state.timer_active:
44
  elapsed_time = int(time.time() - (st.session_state.countdown_end_time - st.session_state.countdown))
45
- if elapsed_time < st.session_state.countdown:
46
- st.markdown(f'<div class="big-font">{format_time(st.session_state.countdown - elapsed_time)}</div>', unsafe_allow_html=True)
 
 
 
 
47
  time.sleep(1)
48
  st.experimental_rerun()
49
  else:
50
  st.session_state.timer_active = False
51
- st.markdown('<div class="big-font">00:00</div>', unsafe_allow_html=True)
52
- st.balloons()
53
- # Suggestion: Use Streamlit's st.audio to play a sound notification here
54
-
55
- # Function to convert seconds to minutes:seconds format
56
- def format_time(seconds):
57
- return f"{seconds // 60:02d}:{seconds % 60:02d}"
58
-
59
- # World Clock
60
- st.header("World Clock")
61
- selected_timezone = st.selectbox("Select Timezone", pytz.all_timezones, index=pytz.all_timezones.index('UTC'))
62
- current_time = datetime.now(pytz.timezone(selected_timezone))
63
- st.write(f"Current time in {selected_timezone}: {current_time.strftime('%Y-%m-%d %H:%M:%S')}")
 
1
  import streamlit as st
2
+ from datetime import datetime
 
3
  import time
4
 
5
+ # Function to convert seconds to a minutes:seconds format
6
+ def format_time(seconds):
7
+ """Convert seconds to a minutes:seconds format."""
8
+ return f"{seconds // 60:02d}:{seconds % 60:02d}"
9
 
10
+ # Sidebar for customization options
11
+ font_size = 60 # Fixed font size for the timer
12
+ big_font_size = 100 # Fixed big font size for the timer (while running)
13
+ font_styles = ["Arial", "Helvetica", "Times New Roman", "Courier", "Verdana"] # Example font styles
14
+ selected_font_style = st.sidebar.selectbox('Select font style for the timer:', font_styles) # Font style selection
15
+ background_color = st.sidebar.color_picker('Background color for the timer:', '#FFFFFF')
16
+ font_color = st.sidebar.color_picker('Font color for the timer:', '#000000')
17
+ app_title = st.sidebar.text_input('App title', 'My Streamlit App') # App title input
 
 
 
18
 
19
+ st.title(app_title) # Display the app title
20
+
21
+ # Initialize session state variables for the timer
22
  if 'countdown' not in st.session_state:
23
+ st.session_state['countdown'] = 0
24
  if 'timer_active' not in st.session_state:
25
+ st.session_state['timer_active'] = False
26
 
27
+ # Function to start the timer
28
  def start_timer():
29
  st.session_state.timer_active = True
30
  st.session_state.countdown_end_time = time.time() + st.session_state.countdown
31
 
32
+ # Timer setup in the main page
33
+ st.write("") # For vertical spacing
34
+ col1, col2, col3 = st.columns([1,2,1]) # Create three columns and place the timer in the middle one
35
+ with col2:
36
+ with st.form("Timer"):
37
+ input_seconds = st.number_input("Set timer (in seconds):", min_value=10, max_value=36000, value=180, step=10)
38
+ cols = st.columns(2)
39
+ start_submitted = cols[0].form_submit_button("Start Timer")
40
+ stop_submitted = cols[1].form_submit_button("Stop Timer")
41
+ if start_submitted:
42
+ st.session_state.countdown = input_seconds
43
+ start_timer()
44
+ if stop_submitted:
45
+ st.session_state.timer_active = False
46
+
47
+ # Display the countdown timer
48
  if st.session_state.timer_active:
49
  elapsed_time = int(time.time() - (st.session_state.countdown_end_time - st.session_state.countdown))
50
+ remaining_seconds = max(0, st.session_state.countdown - elapsed_time)
51
+
52
+ current_font_size = big_font_size if st.session_state.timer_active else font_size # Use big_font_size if timer is active
53
+
54
+ if remaining_seconds > 0:
55
+ 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)
56
  time.sleep(1)
57
  st.experimental_rerun()
58
  else:
59
  st.session_state.timer_active = False
60
+ 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)
61
+ st.balloons()